Commands strings are now in data/command_strings.json. Database path no longer has ./ prefix and filename -> Data Source. Fixed permissions migration due to new EF behaviour. Although commands will now error if they don't have their entry in the command strings - needs fixing.

This commit is contained in:
Master Kwoth 2017-09-17 07:28:48 +02:00
parent 9f2d9d6710
commit bdc6974451
14 changed files with 2107 additions and 27 deletions

View File

@ -3,11 +3,13 @@ using System.Runtime.CompilerServices;
using Discord.Commands; using Discord.Commands;
using NadekoBot.Services.Impl; using NadekoBot.Services.Impl;
//todo what if it doesn't exist
namespace NadekoBot.Common.Attributes namespace NadekoBot.Common.Attributes
{ {
public class Aliases : AliasAttribute public class Aliases : AliasAttribute
{ {
public Aliases([CallerMemberName] string memberName = "") : base(Localization.LoadCommandString(memberName.ToLowerInvariant() + "_cmd").Split(' ').Skip(1).ToArray()) public Aliases([CallerMemberName] string memberName = "") : base(Localization.LoadCommand(memberName.ToLowerInvariant()).Cmd.Split(' ').Skip(1).ToArray())
{ {
} }
} }

View File

@ -6,7 +6,7 @@ namespace NadekoBot.Common.Attributes
{ {
public class Description : SummaryAttribute public class Description : SummaryAttribute
{ {
public Description([CallerMemberName] string memberName="") : base(Localization.LoadCommandString(memberName.ToLowerInvariant() + "_desc")) public Description([CallerMemberName] string memberName="") : base(Localization.LoadCommand(memberName.ToLowerInvariant()).Desc)
{ {
} }

View File

@ -6,7 +6,7 @@ namespace NadekoBot.Common.Attributes
{ {
public class NadekoCommand : CommandAttribute public class NadekoCommand : CommandAttribute
{ {
public NadekoCommand([CallerMemberName] string memberName="") : base(Localization.LoadCommandString(memberName.ToLowerInvariant() + "_cmd").Split(' ')[0]) public NadekoCommand([CallerMemberName] string memberName="") : base(Localization.LoadCommand(memberName.ToLowerInvariant()).Cmd)
{ {
} }

View File

@ -6,7 +6,7 @@ namespace NadekoBot.Common.Attributes
{ {
public class Usage : RemarksAttribute public class Usage : RemarksAttribute
{ {
public Usage([CallerMemberName] string memberName="") : base(Localization.LoadCommandString(memberName.ToLowerInvariant()+"_usage")) public Usage([CallerMemberName] string memberName="") : base(Localization.LoadCommand(memberName.ToLowerInvariant()).Usage)
{ {
} }

View File

@ -0,0 +1,9 @@
namespace NadekoBot.Common
{
public class CommandData
{
public string Cmd { get; set; }
public string Usage { get; set; }
public string Desc { get; set; }
}
}

View File

@ -38,7 +38,7 @@ namespace NadekoBot.Modules.Games
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
public async Task _8Ball([Remainder] string question = null) public async Task EightBall([Remainder] string question = null)
{ {
if (string.IsNullOrWhiteSpace(question)) if (string.IsNullOrWhiteSpace(question))
return; return;

View File

@ -135,11 +135,11 @@ namespace NadekoBot.Modules.Permissions.Services
{ {
var oldPrefixes = new[] { ".", ";", "!!", "!m", "!", "+", "-", "$", ">" }; var oldPrefixes = new[] { ".", ";", "!!", "!m", "!", "+", "-", "$", ">" };
uow._context.Database.ExecuteSqlCommand( uow._context.Database.ExecuteSqlCommand(
$@"UPDATE {nameof(Permissionv2)} @"UPDATE Permissionv2
SET secondaryTargetName=trim(substr(secondaryTargetName, 3)) SET secondaryTargetName=trim(substr(secondaryTargetName, 3))
WHERE secondaryTargetName LIKE '!!%' OR secondaryTargetName LIKE '!m%'; WHERE secondaryTargetName LIKE '!!%' OR secondaryTargetName LIKE '!m%';
UPDATE {nameof(Permissionv2)} UPDATE Permissionv2
SET secondaryTargetName=substr(secondaryTargetName, 2) SET secondaryTargetName=substr(secondaryTargetName, 2)
WHERE secondaryTargetName LIKE '.%' OR WHERE secondaryTargetName LIKE '.%' OR
secondaryTargetName LIKE '~%' OR secondaryTargetName LIKE '~%' OR

View File

@ -80,10 +80,4 @@
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" /> <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Update="Resources\CommandStrings.Designer.cs">
<SubType>Designer</SubType>
</Compile>
</ItemGroup>
</Project> </Project>

View File

@ -5,6 +5,8 @@ using NadekoBot.Services.Database.Models;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using System; using System;
using Microsoft.EntityFrameworkCore.Design; using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Data.Sqlite;
using System.IO;
namespace NadekoBot.Services.Database namespace NadekoBot.Services.Database
{ {
@ -13,7 +15,9 @@ namespace NadekoBot.Services.Database
public NadekoContext CreateDbContext(string[] args) public NadekoContext CreateDbContext(string[] args)
{ {
var optionsBuilder = new DbContextOptionsBuilder<NadekoContext>(); var optionsBuilder = new DbContextOptionsBuilder<NadekoContext>();
optionsBuilder.UseSqlite("Filename=./data/NadekoBot.db"); var builder = new SqliteConnectionStringBuilder("Data Source=data/NadekoBot.db");
builder.DataSource = Path.Combine(AppContext.BaseDirectory, builder.DataSource);
optionsBuilder.UseSqlite(builder.ToString());
var ctx = new NadekoContext(optionsBuilder.Options); var ctx = new NadekoContext(optionsBuilder.Options);
ctx.Database.SetCommandTimeout(60); ctx.Database.SetCommandTimeout(60);
return ctx; return ctx;

View File

@ -1,5 +1,8 @@
using Microsoft.EntityFrameworkCore; using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database; using NadekoBot.Services.Database;
using System;
using System.IO;
using System.Linq; using System.Linq;
namespace NadekoBot.Services namespace NadekoBot.Services
@ -9,17 +12,17 @@ namespace NadekoBot.Services
private readonly DbContextOptions<NadekoContext> options; private readonly DbContextOptions<NadekoContext> options;
private readonly DbContextOptions<NadekoContext> migrateOptions; private readonly DbContextOptions<NadekoContext> migrateOptions;
private readonly string _connectionString;
public DbService(IBotCredentials creds) public DbService(IBotCredentials creds)
{ {
_connectionString = creds.Db.ConnectionString; var builder = new SqliteConnectionStringBuilder(creds.Db.ConnectionString);
builder.DataSource = Path.Combine(AppContext.BaseDirectory, builder.DataSource);
var optionsBuilder = new DbContextOptionsBuilder<NadekoContext>(); var optionsBuilder = new DbContextOptionsBuilder<NadekoContext>();
optionsBuilder.UseSqlite(creds.Db.ConnectionString); optionsBuilder.UseSqlite(builder.ToString());
options = optionsBuilder.Options; options = optionsBuilder.Options;
optionsBuilder = new DbContextOptionsBuilder<NadekoContext>(); optionsBuilder = new DbContextOptionsBuilder<NadekoContext>();
optionsBuilder.UseSqlite(creds.Db.ConnectionString, x => x.SuppressForeignKeyEnforcement()); optionsBuilder.UseSqlite(builder.ToString(), x => x.SuppressForeignKeyEnforcement());
migrateOptions = optionsBuilder.Options; migrateOptions = optionsBuilder.Options;
} }

View File

@ -101,7 +101,7 @@ namespace NadekoBot.Services.Impl
? "sqlite" ? "sqlite"
: dbSection["Type"], : dbSection["Type"],
string.IsNullOrWhiteSpace(dbSection["ConnectionString"]) string.IsNullOrWhiteSpace(dbSection["ConnectionString"])
? "Filename=./data/NadekoBot.db" ? "Data Source=data/NadekoBot.db"
: dbSection["ConnectionString"]); : dbSection["ConnectionString"]);
} }
catch (Exception ex) catch (Exception ex)
@ -125,7 +125,7 @@ namespace NadekoBot.Services.Impl
public string SoundCloudClientId { get; set; } = ""; public string SoundCloudClientId { get; set; } = "";
public string CleverbotApiKey { get; } = ""; public string CleverbotApiKey { get; } = "";
public string CarbonKey { get; set; } = ""; public string CarbonKey { get; set; } = "";
public DBConfig Db { get; set; } = new DBConfig("sqlite", "Filename=./data/NadekoBot.db"); public DBConfig Db { get; set; } = new DBConfig("sqlite", "Data Source=data/NadekoBot.db");
public int TotalShards { get; set; } = 1; public int TotalShards { get; set; } = 1;
public string PatreonAccessToken { get; set; } = ""; public string PatreonAccessToken { get; set; } = "";
public string PatreonCampaignId { get; set; } = "334038"; public string PatreonCampaignId { get; set; } = "334038";

View File

@ -5,6 +5,9 @@ using System.Linq;
using Discord; using Discord;
using NLog; using NLog;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
using NadekoBot.Common;
using Newtonsoft.Json;
using System.IO;
namespace NadekoBot.Services.Impl namespace NadekoBot.Services.Impl
{ {
@ -16,6 +19,14 @@ namespace NadekoBot.Services.Impl
public ConcurrentDictionary<ulong, CultureInfo> GuildCultureInfos { get; } public ConcurrentDictionary<ulong, CultureInfo> GuildCultureInfos { get; }
public CultureInfo DefaultCultureInfo { get; private set; } = CultureInfo.CurrentCulture; public CultureInfo DefaultCultureInfo { get; private set; } = CultureInfo.CurrentCulture;
private static readonly Dictionary<string, CommandData> _commandData;
static Localization()
{
_commandData = JsonConvert.DeserializeObject<Dictionary<string, CommandData>>(
File.ReadAllText("./data/command_strings.json"));
}
private Localization() { } private Localization() { }
public Localization(IBotConfigProvider bcp, IEnumerable<GuildConfig> gcs, DbService db) public Localization(IBotConfigProvider bcp, IEnumerable<GuildConfig> gcs, DbService db)
{ {
@ -117,10 +128,10 @@ namespace NadekoBot.Services.Impl
return info ?? DefaultCultureInfo; return info ?? DefaultCultureInfo;
} }
public static string LoadCommandString(string key) public static CommandData LoadCommand(string key)
{ {
string toReturn = Resources.CommandStrings.ResourceManager.GetString(key); _commandData.TryGetValue(key, out var toReturn);
return string.IsNullOrWhiteSpace(toReturn) ? key : toReturn; return toReturn;
} }
} }
} }

View File

@ -13,7 +13,7 @@
"CarbonKey": "", "CarbonKey": "",
"Db": { "Db": {
"Type": "sqlite", "Type": "sqlite",
"ConnectionString": "Filename=./data/NadekoBot.db" "ConnectionString": "Data Source=data/NadekoBot.db"
}, },
"TotalShards": 1, "TotalShards": 1,
"PatreonAccessToken": "", "PatreonAccessToken": "",

File diff suppressed because it is too large Load Diff