Fixed some commandhandler issues, fixed cleanup migration not taking into account alias duplication (how is it even possible?)

This commit is contained in:
Toastie 2025-03-01 19:57:44 +13:00
parent 76bafd076f
commit 231e77452f
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
6 changed files with 19 additions and 32 deletions

View file

@ -16,7 +16,6 @@ public sealed class Bot : IBot
private IContainer Services { get; set; }
public bool IsReady { get; private set; }
public int ShardId { get; }
private readonly IBotCreds _creds;
@ -262,19 +261,20 @@ public sealed class Bot : IBot
Stopwatch.GetElapsedTime(startTime).TotalSeconds);
var commandHandler = Services.GetRequiredService<CommandHandler>();
// start handling messages received in commandhandler
await commandHandler.StartHandling();
foreach (var a in _loadedAssemblies)
{
await _commandService.AddModulesAsync(a, Services);
}
// await _interactionService.AddModulesAsync(typeof(Bot).Assembly, Services);
IsReady = true;
await EnsureBotOwnershipAsync();
await commandHandler.InitializeAsync();
_ = Task.Run(ExecuteReadySubscriptions);
await commandHandler.StartHandling();
Log.Information("Shard {ShardId} ready", Client.ShardId);
}

View file

@ -94,14 +94,13 @@ public sealed class EllieDbService : DbService
.OrderBy(x => x);
var lastApplied = applied.Last();
Log.Information("Last applied migration: {LastApplied}", lastApplied);
// apply all migrations with names greater than the last applied
foreach (var runnable in available)
{
if (string.Compare(lastApplied, runnable, StringComparison.Ordinal) < 0)
{
Log.Warning("Migration {MigrationName} has not been applied yet", runnable);
Log.Warning("Applying migration {MigrationName}", runnable);
var query = await File.ReadAllTextAsync(GetMigrationPath(ctx.Database, runnable));
await ctx.Database.ExecuteSqlRawAsync(query);

View file

@ -4,7 +4,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<Version>6.0.0-alpha1</Version>
<Version>6.0.1</Version>
<!-- Output/build -->
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>

View file

@ -178,7 +178,8 @@ DELETE FROM GcChannelId WHERE GuildConfigId IS NULL OR GuildConfigId NOT IN (SEL
UPDATE GcChannelId
SET GuildId = (SELECT GuildId FROM GuildConfigs WHERE GuildConfigs.Id = GcChannelId.GuildConfigId);
DELETE FROM CommandCooldown WHERE GuildConfigId IS NULL OR GuildConfigId NOT IN (SELECT Id FROM GuildConfigs);
DELETE FROM CommandCooldown WHERE GuildConfigId IS NULL OR GuildConfigId NOT IN (SELECT Id FROM GuildConfigs)
OR (GuildId, CommandName) IN (SELECT GuildId, CommandName FROM CommandCooldown GROUP BY GuildId, CommandName HAVING COUNT(*) > 1);
UPDATE CommandCooldown
SET GuildId = (SELECT GuildId FROM GuildConfigs WHERE GuildConfigs.Id = CommandCooldown.GuildConfigId);

View file

@ -5,5 +5,4 @@ namespace EllieBot;
public interface IBot
{
bool IsReady { get; }
}

View file

@ -8,10 +8,8 @@ using PreconditionResult = Discord.Commands.PreconditionResult;
namespace EllieBot.Services;
public class CommandHandler : IEService, IReadyExecutor, ICommandHandler
public class CommandHandler : IEService, ICommandHandler
{
private const int GLOBAL_COMMANDS_COOLDOWN = 200;
private const float ONE_THOUSANDTH = 1.0f / 1000;
public event Func<IUserMessage, CommandInfo, Task> CommandExecuted = delegate { return Task.CompletedTask; };
@ -57,21 +55,14 @@ public class CommandHandler : IEService, IReadyExecutor, ICommandHandler
_shardData = shardData;
}
public async Task OnReadyAsync()
public async Task InitializeAsync()
{
await using (var uow = _db.GetDbContext())
{
_prefixes = await uow.GetTable<GuildConfig>()
.Where(x => Queries.GuildOnShard(x.GuildId, _shardData.TotalShards, _shardData.ShardId))
.Where(x => x.Prefix != null)
.ToListAsyncLinqToDB()
.Fmap(x => x.ToDictionary(x => x.GuildId, x => x.Prefix).ToConcurrent());
}
// clear users on short cooldown every GLOBAL_COMMANDS_COOLDOWN miliseconds
using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(GLOBAL_COMMANDS_COOLDOWN));
while (await timer.WaitForNextTickAsync())
UsersOnShortCooldown.Clear();
await using var uow = _db.GetDbContext();
_prefixes = await uow.GetTable<GuildConfig>()
.Where(x => Queries.GuildOnShard(x.GuildId, _shardData.TotalShards, _shardData.ShardId))
.Where(x => x.Prefix != null)
.ToListAsyncLinqToDB()
.Fmap(x => x.ToDictionary(x => x.GuildId, x => x.Prefix).ToConcurrent());
}
public string GetPrefix(IGuild guild)
@ -218,9 +209,6 @@ public class CommandHandler : IEService, IReadyExecutor, ICommandHandler
private Task MessageReceivedHandler(SocketMessage msg)
{
if (!_bot.IsReady)
return Task.CompletedTask;
if (_bc.IgnoreOtherBots)
{
if (msg.Author.IsBot)