From c19329f9c46795d242706d4730f66da1c80b20e5 Mon Sep 17 00:00:00 2001 From: Erisa A Date: Fri, 2 Aug 2024 21:43:00 +0100 Subject: [PATCH] Add user-installable commands --- src/Modules/Helpers.cs | 36 ++++++++++++++++++++++++++++++++++++ src/Modules/SlashCommands.cs | 13 ++++++++++--- src/Program.cs | 3 +-- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/Modules/Helpers.cs b/src/Modules/Helpers.cs index 3b18a3b..254c99b 100644 --- a/src/Modules/Helpers.cs +++ b/src/Modules/Helpers.cs @@ -191,5 +191,41 @@ public static string UserAvatarURL(DiscordUser user, string format = "default", return $"https://cdn.discordapp.com/avatars/{user.Id}/{user.AvatarHash}.{format}?size=4096"; } + public static async Task CodeOrHasteBinAsync(string input, string language = "", int charLimit = 1930, bool plain = false) + { + bool inputHasCodeBlock = input.Contains("```"); + if (input.Length > charLimit || inputHasCodeBlock) + { + HasteBinResult hasteResult = await Program.hasteUploader.Post(input); + if (hasteResult.IsSuccess) + { + var hasteUrl = hasteResult.FullUrl; + if (language != "") + { + hasteUrl = hasteUrl + "." + language; + } + + if (plain) + return hasteUrl; + else if (inputHasCodeBlock) + return $"Output contained a code block, so it was uploaded to Hastebin to avoid formatting issues: {hasteUrl}"; + else + return $"Output exceeded character limit: {hasteUrl}"; + } + else + { + Program.discord.Logger.LogError("Error ocurred uploading to Hastebin with status code: {code}\nPayload: {output}", hasteResult.StatusCode, input); + if (plain) + return "Error, check logs."; + + return $"Unknown error occurred during upload to Hastebin.\nPlease try again or contact the bot owner."; + } + } + else + { + return $"```{language}\n{input}\n```"; + } + } + } } diff --git a/src/Modules/SlashCommands.cs b/src/Modules/SlashCommands.cs index 24c86df..8cf1a9e 100644 --- a/src/Modules/SlashCommands.cs +++ b/src/Modules/SlashCommands.cs @@ -124,7 +124,7 @@ public async Task AvatarSlashCommand(InteractionContext ctx, await ctx.RespondAsync(null, embed); } - [ContextMenu(DiscordApplicationCommandType.UserContextMenu, "Show Avatar")] + [ContextMenu(DiscordApplicationCommandType.UserContextMenu, "Show Avatar"), InteractionCommandInstallType(DiscordApplicationIntegrationType.UserInstall, DiscordApplicationIntegrationType.GuildInstall), InteractionCommandAllowedContexts(DiscordInteractionContextType.PrivateChannel, DiscordInteractionContextType.Guild)] public async Task ContextAvatar(ContextMenuContext ctx) { string avatarUrl = ""; @@ -155,16 +155,23 @@ public async Task ContextAvatar(ContextMenuContext ctx) await ctx.RespondAsync(null, embed, ephemeral: true); } - [ContextMenu(DiscordApplicationCommandType.UserContextMenu, "lk hug")] + [ContextMenu(DiscordApplicationCommandType.UserContextMenu, "lk hug"), InteractionCommandInstallType(DiscordApplicationIntegrationType.UserInstall, DiscordApplicationIntegrationType.GuildInstall), InteractionCommandAllowedContexts(DiscordInteractionContextType.PrivateChannel, DiscordInteractionContextType.Guild)] public async Task ContextHug(ContextMenuContext ctx) { await ctx.RespondAsync($"{Program.cfgjson.Emoji.BlobHug} {ctx.TargetUser.Mention} was given a tight hug by {ctx.User.Mention}!"); } - [ContextMenu(DiscordApplicationCommandType.UserContextMenu, "lk pat")] + [ContextMenu(DiscordApplicationCommandType.UserContextMenu, "lk pat"), InteractionCommandInstallType(DiscordApplicationIntegrationType.UserInstall, DiscordApplicationIntegrationType.GuildInstall), InteractionCommandAllowedContexts(DiscordInteractionContextType.PrivateChannel, DiscordInteractionContextType.Guild)] public async Task ContextPat(ContextMenuContext ctx) { await ctx.RespondAsync($"{Program.cfgjson.Emoji.BlobPats} {ctx.TargetUser.Mention} was given a big headpat by {ctx.User.Mention}!"); } + + [ContextMenu(DiscordApplicationCommandType.MessageContextMenu, "Dump message data"), InteractionCommandInstallType(DiscordApplicationIntegrationType.UserInstall, DiscordApplicationIntegrationType.GuildInstall), InteractionCommandAllowedContexts(DiscordInteractionContextType.PrivateChannel, DiscordInteractionContextType.Guild)] + public async Task DumpMessage(ContextMenuContext ctx) + { + var rawMsgData = JsonConvert.SerializeObject(ctx.TargetMessage, Formatting.Indented); + await ctx.RespondAsync(await CodeOrHasteBinAsync(rawMsgData, "json"), ephemeral: true); + } } } diff --git a/src/Program.cs b/src/Program.cs index 320c5a3..923c6c1 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -373,8 +373,7 @@ Task Discord_ThreadMembersUpdated(DiscordClient client, ThreadMembersUpdatedEven var slash = discord.UseSlashCommands(); - foreach (ulong guildId in cfgjson.Guilds) - slash.RegisterCommands(guildId); + slash.RegisterCommands(); await discord.ConnectAsync();