diff --git a/src/EllieBot/Bot.cs b/src/EllieBot/Bot.cs index 2972214..224892c 100644 --- a/src/EllieBot/Bot.cs +++ b/src/EllieBot/Bot.cs @@ -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); } diff --git a/src/EllieBot/Db/EllieDbService.cs b/src/EllieBot/Db/EllieDbService.cs index 2ffa59e..7c055a8 100644 --- a/src/EllieBot/Db/EllieDbService.cs +++ b/src/EllieBot/Db/EllieDbService.cs @@ -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); diff --git a/src/EllieBot/EllieBot.csproj b/src/EllieBot/EllieBot.csproj index daa0d2e..8ed4e28 100644 --- a/src/EllieBot/EllieBot.csproj +++ b/src/EllieBot/EllieBot.csproj @@ -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> diff --git a/src/EllieBot/Migrations/Sqlite/20250126062816_cleanup.sql b/src/EllieBot/Migrations/Sqlite/20250126062816_cleanup.sql index da93828..1e361d4 100644 --- a/src/EllieBot/Migrations/Sqlite/20250126062816_cleanup.sql +++ b/src/EllieBot/Migrations/Sqlite/20250126062816_cleanup.sql @@ -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); diff --git a/src/EllieBot/_common/IBot.cs b/src/EllieBot/_common/IBot.cs index 54b3785..0f3102a 100644 --- a/src/EllieBot/_common/IBot.cs +++ b/src/EllieBot/_common/IBot.cs @@ -5,5 +5,4 @@ namespace EllieBot; public interface IBot { - bool IsReady { get; } } \ No newline at end of file diff --git a/src/EllieBot/_common/Services/CommandHandler.cs b/src/EllieBot/_common/Services/CommandHandler.cs index 37be0e7..18db670 100644 --- a/src/EllieBot/_common/Services/CommandHandler.cs +++ b/src/EllieBot/_common/Services/CommandHandler.cs @@ -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)