From 625f1e316663e877ce0e04f8b8b4fb0dccdb3c71 Mon Sep 17 00:00:00 2001 From: 13xforever Date: Fri, 9 Dec 2022 00:00:42 +0500 Subject: [PATCH] move credits to slash commands and update the code slightly --- Clients/GithubClient/GithubClient.csproj | 2 +- .../BaseApplicationCommandModuleCustom.cs | 49 +++++++++++++++++++ CompatBot/Commands/Misc.cs | 25 ---------- .../Commands/{SlashTest.cs => SlashMisc.cs} | 29 ++++++----- CompatBot/Program.cs | 9 ++-- discord-bot-net.sln | 2 + 6 files changed, 72 insertions(+), 44 deletions(-) create mode 100644 CompatBot/Commands/BaseApplicationCommandModuleCustom.cs rename CompatBot/Commands/{SlashTest.cs => SlashMisc.cs} (62%) diff --git a/Clients/GithubClient/GithubClient.csproj b/Clients/GithubClient/GithubClient.csproj index 40f848e1..e64e45b8 100644 --- a/Clients/GithubClient/GithubClient.csproj +++ b/Clients/GithubClient/GithubClient.csproj @@ -6,7 +6,7 @@ - + diff --git a/CompatBot/Commands/BaseApplicationCommandModuleCustom.cs b/CompatBot/Commands/BaseApplicationCommandModuleCustom.cs new file mode 100644 index 00000000..b902ab66 --- /dev/null +++ b/CompatBot/Commands/BaseApplicationCommandModuleCustom.cs @@ -0,0 +1,49 @@ +๏ปฟusing System; +using System.Net; +using System.Threading.Tasks; +using CompatBot.Database.Providers; +using DSharpPlus; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; + +namespace CompatBot.Commands; + +internal class BaseApplicationCommandModuleCustom : ApplicationCommandModule +{ + private DateTimeOffset executionStart; + + public override async Task BeforeSlashExecutionAsync(InteractionContext ctx) + { + executionStart = DateTimeOffset.UtcNow; + + if (ctx.Channel.Name == "media" && ctx.Interaction is { Type: InteractionType.ApplicationCommand, Data.Name: not ("warn" or "report") }) + { + //todo: look what's available in data + Config.Log.Info($"Ignoring slash command from {ctx.User.Username} (<@{ctx.User.Id}>) in #media: {ctx.Interaction.Data}"); + await ctx.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, + new DiscordInteractionResponseBuilder().WithContent($"Only `warn` and `report` are allowed in {ctx.Channel.Mention}").AsEphemeral() + ).ConfigureAwait(false); + Config.TelemetryClient?.TrackRequest(ctx.Interaction.Data.Name, executionStart, DateTimeOffset.UtcNow - executionStart, HttpStatusCode.Forbidden.ToString(), true); + return false; + } + + var disabledCmds = DisabledCommandsProvider.Get(); + if (disabledCmds.Contains(ctx.Interaction.Data.Name) && !disabledCmds.Contains("*")) + { + await ctx.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, + new DiscordInteractionResponseBuilder().WithContent($"Command `{ctx.Interaction.Data.Name}` is currently disabled").AsEphemeral() + ).ConfigureAwait(false); + Config.TelemetryClient?.TrackRequest(ctx.Interaction.Data.Name, executionStart, DateTimeOffset.UtcNow - executionStart, HttpStatusCode.Locked.ToString(), true); + return false; + } + + return await base.BeforeSlashExecutionAsync(ctx).ConfigureAwait(false); + } + + public override Task AfterSlashExecutionAsync(InteractionContext ctx) + { + StatsStorage.IncCmdStat(ctx.Interaction.Data.Name); + Config.TelemetryClient?.TrackRequest(ctx.Interaction.Data.Name, executionStart, DateTimeOffset.UtcNow - executionStart, HttpStatusCode.OK.ToString(), true); + return base.AfterSlashExecutionAsync(ctx); + } +} \ No newline at end of file diff --git a/CompatBot/Commands/Misc.cs b/CompatBot/Commands/Misc.cs index 9705b867..6aad8f68 100644 --- a/CompatBot/Commands/Misc.cs +++ b/CompatBot/Commands/Misc.cs @@ -130,31 +130,6 @@ internal sealed class Misc: BaseCommandModuleCustom private static readonly Regex Instead = new("rate (?.+) instead", RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Singleline); - [Command("credits"), Aliases("about")] - [Description("Author Credit")] - public async Task About(CommandContext ctx) - { - var hcorion = ctx.Client.GetEmoji(":hcorion:", DiscordEmoji.FromUnicode("๐Ÿ")); - var clienthax = ctx.Client.GetEmoji(":gooseknife:", DiscordEmoji.FromUnicode("๐Ÿฑ")); - var embed = new DiscordEmbedBuilder - { - Title = "RPCS3 Compatibility Bot", - Url = "https://github.com/RPCS3/discord-bot", - Color = DiscordColor.Purple, - }.AddField("Made by", - "๐Ÿ’ฎ 13xforever\n" + - "๐Ÿ‡ญ๐Ÿ‡ท Roberto Aniฤ‡ Baniฤ‡ aka nicba1010\n" + - $"{clienthax} clienthax\n" - ) - .AddField("People who ~~broke~~ helped test the bot", - "๐Ÿฑ Juhn\n" + - $"{hcorion} hcorion\n" + - "๐Ÿ™ƒ TGE\n" + - "๐Ÿ’ Maru\n" + - "โ™‹ Tourghool"); - await ctx.Channel.SendMessageAsync(embed: embed.Build()); - } - [Command("roll")] [Description("Generates a random number between 1 and maxValue. Can also roll dices like `2d6`. Default is 1d6")] public Task Roll(CommandContext ctx, [Description("Some positive natural number")] int maxValue = 6, [RemainingText, Description("Optional text")] string? comment = null) diff --git a/CompatBot/Commands/SlashTest.cs b/CompatBot/Commands/SlashMisc.cs similarity index 62% rename from CompatBot/Commands/SlashTest.cs rename to CompatBot/Commands/SlashMisc.cs index 95aafca0..f7a13f3e 100644 --- a/CompatBot/Commands/SlashTest.cs +++ b/CompatBot/Commands/SlashMisc.cs @@ -6,10 +6,9 @@ using System.Threading.Tasks; namespace CompatBot.Commands; -internal sealed class SlashTest: ApplicationCommandModule +internal sealed class SlashMisc: BaseApplicationCommandModuleCustom { - [SlashCommand("credits", "Author Credit")] - // TODO [Aliases("about")] + [SlashCommand("about", "Bot information")] public async Task About(InteractionContext ctx) { var hcorion = ctx.Client.GetEmoji(":hcorion:", DiscordEmoji.FromUnicode("๐Ÿ")); @@ -20,17 +19,21 @@ internal sealed class SlashTest: ApplicationCommandModule Url = "https://github.com/RPCS3/discord-bot", Color = DiscordColor.Purple, }.AddField("Made by", - "๐Ÿ’ฎ 13xforever\n" + - "๐Ÿ‡ญ๐Ÿ‡ท Roberto Aniฤ‡ Baniฤ‡ aka nicba1010\n" + - $"{clienthax} clienthax\n" + $""" + ๐Ÿ’ฎ 13xforever + ๐Ÿ‡ญ๐Ÿ‡ท Roberto Aniฤ‡ Baniฤ‡ aka nicba1010 + {clienthax} clienthax + """ ) .AddField("People who ~~broke~~ helped test the bot", - "๐Ÿฑ Juhn\n" + - $"{hcorion} hcorion\n" + - "๐Ÿ™ƒ TGE\n" + - "๐Ÿ’ Maru\n" + - "โ™‹ Tourghool"); - await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AddEmbed(embed.Build())); + $""" + ๐Ÿฑ Juhn + {hcorion} hcorion + ๐Ÿ™ƒ TGE + ๐Ÿ’ Maru + โ™‹ Tourghool + """ + ); + await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AddEmbed(embed.Build()).AsEphemeral()); } - } \ No newline at end of file diff --git a/CompatBot/Program.cs b/CompatBot/Program.cs index 4bd15a57..a6f9e97f 100644 --- a/CompatBot/Program.cs +++ b/CompatBot/Program.cs @@ -142,10 +142,6 @@ internal static class Program Intents = DiscordIntents.All, }; using var client = new DiscordClient(config); - var slashCommands = client.UseSlashCommands(); - // Only register to rpcs3 guild for now. - slashCommands.RegisterCommands(Config.BotGuildId); - var commands = client.UseCommandsNext(new() { StringPrefixes = new[] {Config.CommandPrefix, Config.AutoRemoveCommandPrefix}, @@ -176,10 +172,13 @@ internal static class Program commands.RegisterCommands(); commands.RegisterCommands(); commands.RegisterCommands(); - if (!string.IsNullOrEmpty(Config.AzureComputerVisionKey)) commands.RegisterCommands(); + var slashCommands = client.UseSlashCommands(); + // Only register to rpcs3 guild for now. + slashCommands.RegisterCommands(Config.BotGuildId); + commands.CommandErrored += UnknownCommandHandler.OnError; client.UseInteractivity(new()); diff --git a/discord-bot-net.sln b/discord-bot-net.sln index 28e5d2f0..e1b6d4fc 100644 --- a/discord-bot-net.sln +++ b/discord-bot-net.sln @@ -30,6 +30,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md SECURITY.md = SECURITY.md .github\workflows\dotnet.yml = .github\workflows\dotnet.yml + get-names_anidb.ps1 = get-names_anidb.ps1 + get-names_anilist.ps1 = get-names_anilist.ps1 EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OneDriveClient", "Clients\OneDriveClient\OneDriveClient.csproj", "{5C4BCF33-2EC6-455F-B026-8A0001B7B7AD}"