From f2ac3ff826b3c16c2887c8c70906eef55140854b Mon Sep 17 00:00:00 2001 From: Pythonic-Rainbow Date: Fri, 19 Jan 2024 11:29:30 +0000 Subject: [PATCH] [Bot] Switched to Interaction framework --- Bot/Bot.csproj | 1 + Bot/Dc/CmdHandlers.cs | 22 ++++++++++++---------- Bot/Dc/Discord.cs | 28 +++++++--------------------- Bot/Program.cs | 2 +- Bot/Sql/Db.cs | 1 + 5 files changed, 22 insertions(+), 32 deletions(-) diff --git a/Bot/Bot.csproj b/Bot/Bot.csproj index f8266be..dec485a 100644 --- a/Bot/Bot.csproj +++ b/Bot/Bot.csproj @@ -27,6 +27,7 @@ + diff --git a/Bot/Dc/CmdHandlers.cs b/Bot/Dc/CmdHandlers.cs index ac0b1c7..dcf2c11 100644 --- a/Bot/Dc/CmdHandlers.cs +++ b/Bot/Dc/CmdHandlers.cs @@ -1,19 +1,20 @@ -using Discord.WebSocket; +using Discord.Interactions; +using Discord.WebSocket; using Hyperstellar.Sql; namespace Hyperstellar.Dc; -internal class CmdHandlers +public class CmdHandlers : InteractionModuleBase { - internal static async Task ShutdownAsync(SocketSlashCommand cmd) + + [SlashCommand("shutdown", "[Admin] Shuts down the bot")] + public async Task ShutdownAsync(bool commit = true) { - if (cmd.User.Id != 264756129916125184) + if (Context.User.Id != 264756129916125184) { return; } - - bool commit = cmd.Data.Options.Count == 0 || (bool)cmd.Data.Options.First().Value; - await cmd.RespondAsync("Ok", ephemeral: true); + await RespondAsync("Ok", ephemeral: true); if (commit) { Db.Commit(); @@ -21,14 +22,15 @@ internal static async Task ShutdownAsync(SocketSlashCommand cmd) Environment.Exit(0); } - internal static async Task CommitAsync(SocketSlashCommand cmd) + [SlashCommand("commit", "[Admin] Commits db")] + public async Task CommitAsync() { - if (cmd.User.Id != 264756129916125184) + if (Context.User.Id != 264756129916125184) { return; } Db.Commit(); - await cmd.RespondAsync("Committed", ephemeral: true); + await RespondAsync("Committed", ephemeral: true); } } diff --git a/Bot/Dc/Discord.cs b/Bot/Dc/Discord.cs index 84f89fc..97597d4 100644 --- a/Bot/Dc/Discord.cs +++ b/Bot/Dc/Discord.cs @@ -1,4 +1,6 @@ -using Discord; +using System.Reflection; +using Discord; +using Discord.Interactions; using Discord.WebSocket; using static Hyperstellar.Coc; @@ -11,6 +13,7 @@ internal sealed class Discord private static SocketTextChannel s_botLog; #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. + private static readonly InteractionService s_interactionSvc = new(s_bot); internal static readonly DiscordSocketClient s_bot = new(); private static Task Log(LogMessage msg) @@ -23,30 +26,12 @@ private static async Task Ready() { s_botLog = (SocketTextChannel)s_bot.GetChannel(Secrets.s_botLogId); _ = Task.Run(BotReadyAsync); - - SlashCommandBuilder guildCmd = new SlashCommandBuilder() - .WithName("shutdown") - .WithDescription("[Admin] Shuts down the bot") - .AddOption("commit", ApplicationCommandOptionType.Boolean, "Commit db?"); - await s_bot.CreateGlobalApplicationCommandAsync(guildCmd.Build()); - - guildCmd = new SlashCommandBuilder() - .WithName("commit") - .WithDescription("[Admin] Commits db"); - await s_bot.CreateGlobalApplicationCommandAsync(guildCmd.Build()); } private static async Task SlashCmdXAsync(SocketSlashCommand cmd) { - switch (cmd.Data.Name) - { - case "shutdown": - await CmdHandlers.ShutdownAsync(cmd); - break; - case "commit": - await CmdHandlers.CommitAsync(cmd); - break; - } + var ctx = new SocketInteractionContext(s_bot, cmd); + await s_interactionSvc.ExecuteCommandAsync(ctx, null); } internal static async Task InitAsync() @@ -54,6 +39,7 @@ internal static async Task InitAsync() s_bot.Log += Log; s_bot.Ready += Ready; s_bot.SlashCommandExecuted += SlashCmdXAsync; + await s_interactionSvc.AddModulesAsync(Assembly.GetEntryAssembly(), null); await s_bot.LoginAsync(TokenType.Bot, Secrets.s_discord); await s_bot.StartAsync(); } diff --git a/Bot/Program.cs b/Bot/Program.cs index 0de38a6..ab20b09 100644 --- a/Bot/Program.cs +++ b/Bot/Program.cs @@ -1,4 +1,4 @@ -namespace Hyperstellar; +namespace Hyperstellar; public class Program { diff --git a/Bot/Sql/Db.cs b/Bot/Sql/Db.cs index d6c3d9d..59e4438 100644 --- a/Bot/Sql/Db.cs +++ b/Bot/Sql/Db.cs @@ -9,6 +9,7 @@ internal sealed class Db internal static void Commit() { s_db.Commit(); + Console.WriteLine("Db committed"); s_db.BeginTransaction(); }