This commit is contained in:
Snake26183
2023-02-28 21:43:53 +00:00
3 changed files with 64 additions and 56 deletions

View File

@@ -46,9 +46,7 @@ public sealed class CommandOrExprTypeReader : NadekoTypeReader<CommandOrExprInfo
public override async ValueTask<TypeReaderResult<CommandOrExprInfo>> ReadAsync(ICommandContext ctx, string input) public override async ValueTask<TypeReaderResult<CommandOrExprInfo>> ReadAsync(ICommandContext ctx, string input)
{ {
input = input.ToUpperInvariant(); if (_exprs.ExpressionExists(ctx.Guild?.Id, input))
if (_exprs.ExpressionExists(ctx.Guild?.Id, input) || _exprs.ExpressionExists(null, input))
return TypeReaderResult.FromSuccess(new CommandOrExprInfo(input, CommandOrExprInfo.Type.Custom)); return TypeReaderResult.FromSuccess(new CommandOrExprInfo(input, CommandOrExprInfo.Type.Custom));
var cmd = await new CommandTypeReader(_commandHandler, _cmds).ReadAsync(ctx, input); var cmd = await new CommandTypeReader(_commandHandler, _cmds).ReadAsync(ctx, input);

View File

@@ -12,10 +12,4 @@ public static class NadekoExpressionExtensions
public static IEnumerable<NadekoExpression> ForId(this DbSet<NadekoExpression> exprs, ulong id) public static IEnumerable<NadekoExpression> ForId(this DbSet<NadekoExpression> exprs, ulong id)
=> exprs.AsNoTracking().AsQueryable().Where(x => x.GuildId == id).ToList(); => exprs.AsNoTracking().AsQueryable().Where(x => x.GuildId == id).ToList();
public static NadekoExpression GetByGuildIdAndInput(
this DbSet<NadekoExpression> exprs,
ulong? guildId,
string input)
=> exprs.FirstOrDefault(x => x.GuildId == guildId && x.Trigger.ToUpper() == input);
} }

View File

@@ -57,8 +57,8 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
// 1. expressions are almost never added (compared to how many times they are being looped through) // 1. expressions are almost never added (compared to how many times they are being looped through)
// 2. only need write locks for this as we'll rebuild+replace the array on every edit // 2. only need write locks for this as we'll rebuild+replace the array on every edit
// 3. there's never many of them (at most a thousand, usually < 100) // 3. there's never many of them (at most a thousand, usually < 100)
private NadekoExpression[] globalExpressions; private NadekoExpression[] globalExpressions = Array.Empty<NadekoExpression>();
private ConcurrentDictionary<ulong, NadekoExpression[]> newguildExpressions; private ConcurrentDictionary<ulong, NadekoExpression[]> newguildExpressions = new();
private readonly DbService _db; private readonly DbService _db;
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
@@ -125,7 +125,7 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
.ToArray()) .ToArray())
.ToConcurrent(); .ToConcurrent();
_disabledGlobalExpressionGuilds = new (await uow.GuildConfigs _disabledGlobalExpressionGuilds = new(await uow.GuildConfigs
.Where(x => x.DisableGlobalExpressions) .Where(x => x.DisableGlobalExpressions)
.Select(x => x.GuildId) .Select(x => x.GuildId)
.ToListAsyncLinqToDB()); .ToListAsyncLinqToDB());
@@ -509,9 +509,25 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
public bool ExpressionExists(ulong? guildId, string input) public bool ExpressionExists(ulong? guildId, string input)
{ {
using var uow = _db.GetDbContext(); input = input.ToLowerInvariant();
var expr = uow.Expressions.GetByGuildIdAndInput(guildId, input);
return expr is not null; var gexprs = globalExpressions;
foreach (var t in gexprs)
{
if (t.Trigger == input)
return true;
}
if (guildId is ulong gid && newguildExpressions.TryGetValue(gid, out var guildExprs))
{
foreach (var t in guildExprs)
{
if (t.Trigger == input)
return true;
}
}
return false;
} }
public string ExportExpressions(ulong? guildId) public string ExportExpressions(ulong? guildId)