|
| 1 | +using Discord; |
| 2 | +using TaylorBot.Net.Commands.Parsers; |
| 3 | +using TaylorBot.Net.Commands.PostExecution; |
| 4 | +using TaylorBot.Net.Commands.Preconditions; |
| 5 | +using TaylorBot.Net.Core.Embed; |
| 6 | + |
| 7 | +namespace TaylorBot.Net.Commands.Discord.Program.Modules.Commands.Commands; |
| 8 | + |
| 9 | +public class CommandServerPrefixSlashCommand( |
| 10 | + UserHasPermissionOrOwnerPrecondition.Factory userHasPermission, |
| 11 | + CommandMentioner mention, |
| 12 | + InGuildPrecondition.Factory inGuild, |
| 13 | + ICommandPrefixRepository commandPrefixRepository, |
| 14 | + IDisabledGuildCommandRepository disabledGuildCommandRepository) : ISlashCommand<NoOptions> |
| 15 | +{ |
| 16 | + public static string CommandName => "command prefix"; |
| 17 | + |
| 18 | + public ISlashCommandInfo Info => new MessageCommandInfo(CommandName); |
| 19 | + |
| 20 | + public IList<ICommandPrecondition> BuildPreconditions() => [ |
| 21 | + inGuild.Create(botMustBeInGuild: true), |
| 22 | + userHasPermission.Create(GuildPermission.ManageGuild) |
| 23 | + ]; |
| 24 | + |
| 25 | + public ValueTask<Command> GetCommandAsync(RunContext context, NoOptions _) |
| 26 | + { |
| 27 | + return new(new Command( |
| 28 | + new(Info.Name), |
| 29 | + async () => |
| 30 | + { |
| 31 | + var guild = context.Guild; |
| 32 | + ArgumentNullException.ThrowIfNull(guild); |
| 33 | + ArgumentNullException.ThrowIfNull(guild.Fetched); |
| 34 | + |
| 35 | + var prefixResult = await commandPrefixRepository.GetOrInsertGuildPrefixAsync(guild.Fetched); |
| 36 | + var currentPrefix = prefixResult.Prefix; |
| 37 | + |
| 38 | + var prefixDisabledResult = await disabledGuildCommandRepository.IsGuildCommandDisabledAsync(guild, new("all-prefix")); |
| 39 | + var arePrefixCommandsDisabled = prefixDisabledResult.IsDisabled; |
| 40 | + |
| 41 | + var description = |
| 42 | + $""" |
| 43 | + ## Prefix Commands 👴 |
| 44 | + Prefix commands are commands that are used in text channels by typing a prefix followed by the command name 💬 |
| 45 | + The default prefix for TaylorBot is `!`, allowing you to use commands by typing `!help` for example ❗ |
| 46 | + ## This Server's Prefix 🖌️ |
| 47 | + - **Current server prefix:** `{currentPrefix}` |
| 48 | + One issue with prefix commands is that multiple bots can use the same prefix 🪢 |
| 49 | + This means typing `!help` could trigger multiple bots at once, which can be confusing/spammy 😵 |
| 50 | + To solve this, TaylorBot allows you to change the prefix for your server with "<@119572982178906114> setprefix" command ⚙️ |
| 51 | + ## Disabling Prefix Commands 🚫 |
| 52 | + |
| 53 | + """; |
| 54 | + |
| 55 | + InteractionComponent button; |
| 56 | + if (arePrefixCommandsDisabled) |
| 57 | + { |
| 58 | + description += |
| 59 | + $""" |
| 60 | + Prefix commands are currently **disabled** in this server ⛔ |
| 61 | + Users must use slash commands (e.g. {mention.SlashCommand("help")}) instead of prefix commands like `{currentPrefix}help` ✅ |
| 62 | + You can re-enable prefix commands using the button below ⬇️ |
| 63 | + """; |
| 64 | + button = CreateEnablePrefixButton(); |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + description += |
| 69 | + $""" |
| 70 | + While prefix commands used to be the only way to interact with bots on Discord, they have many downsides 🥲 |
| 71 | + Slash commands are now the preferred modern way to interact with bots, with many improvements such as buttons, modals, etc. 💪 |
| 72 | + For example, you can use {mention.SlashCommand("help")} instead of typing `{currentPrefix}help` ⚡ |
| 73 | + |
| 74 | + Some servers may want to disable prefix commands entirely to avoid confusion and encourage users to use slash commands instead 🐣 |
| 75 | + Use the button below to disable prefix commands in this server ⬇️ |
| 76 | + """; |
| 77 | + button = CreateDisablePrefixButton(); |
| 78 | + } |
| 79 | + |
| 80 | + var embed = EmbedFactory.CreateSuccess(description); |
| 81 | + return new MessageResult(new(new(embed), [button])); |
| 82 | + }, |
| 83 | + Preconditions: BuildPreconditions() |
| 84 | + )); |
| 85 | + } |
| 86 | + |
| 87 | + public static InteractionComponent CreateDisablePrefixButton() |
| 88 | + { |
| 89 | + return InteractionComponent.CreateActionRow(InteractionComponent.CreateButton( |
| 90 | + style: InteractionButtonStyle.Danger, |
| 91 | + custom_id: InteractionCustomId.Create(CustomIdNames.CommandPrefixToggle, [new("enable", bool.FalseString)]).RawId, |
| 92 | + label: "Disable prefix commands")); |
| 93 | + } |
| 94 | + |
| 95 | + public static InteractionComponent CreateEnablePrefixButton() |
| 96 | + { |
| 97 | + return InteractionComponent.CreateActionRow(InteractionComponent.CreateButton( |
| 98 | + style: InteractionButtonStyle.Success, |
| 99 | + custom_id: InteractionCustomId.Create(CustomIdNames.CommandPrefixToggle, [new("enable", bool.TrueString)]).RawId, |
| 100 | + label: "Enable prefix commands")); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +public class CommandPrefixToggleHandler( |
| 105 | + IInteractionResponseClient responseClient, |
| 106 | + CommandServerPrefixSlashCommand command, |
| 107 | + IDisabledGuildCommandRepository disabledGuildCommandRepository, |
| 108 | + CommandMentioner mention, |
| 109 | + ICommandPrefixRepository commandPrefixRepository |
| 110 | +) : IButtonHandler |
| 111 | +{ |
| 112 | + public static CustomIdNames CustomIdName => CustomIdNames.CommandPrefixToggle; |
| 113 | + |
| 114 | + public IComponentHandlerInfo Info => new MessageHandlerInfo( |
| 115 | + CustomIdName.ToText(), |
| 116 | + Preconditions: command.BuildPreconditions(), |
| 117 | + RequireOriginalUser: true); |
| 118 | + |
| 119 | + public async Task HandleAsync(DiscordButtonComponent button, RunContext context) |
| 120 | + { |
| 121 | + var guild = context.Guild; |
| 122 | + ArgumentNullException.ThrowIfNull(guild); |
| 123 | + ArgumentNullException.ThrowIfNull(guild.Fetched); |
| 124 | + |
| 125 | + var enable = button.CustomId.ParsedData.TryGetValue("enable", out var enableStr) |
| 126 | + && bool.TryParse(enableStr, out var parsed) && parsed == true; |
| 127 | + |
| 128 | + if (enable) |
| 129 | + { |
| 130 | + await disabledGuildCommandRepository.EnableInAsync(guild.Fetched, "all-prefix"); |
| 131 | + |
| 132 | + var prefixResult = await commandPrefixRepository.GetOrInsertGuildPrefixAsync(guild.Fetched); |
| 133 | + var currentPrefix = prefixResult.Prefix; |
| 134 | + |
| 135 | + var embed = EmbedFactory.CreateSuccessEmbed( |
| 136 | + $""" |
| 137 | + Prefix commands are now enabled in this server ✅ |
| 138 | + Users can use prefix commands like `{currentPrefix}help` again 👴 |
| 139 | + You can disable prefix commands at any time using {mention.SlashCommand("command prefix")} ↩️ |
| 140 | + """); |
| 141 | + await responseClient.EditOriginalResponseAsync(button.Interaction, embed); |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + await disabledGuildCommandRepository.DisableInAsync(guild.Fetched, "all-prefix"); |
| 146 | + |
| 147 | + var embed = EmbedFactory.CreateSuccessEmbed( |
| 148 | + $""" |
| 149 | + Prefix commands are now disabled in this server ✅ |
| 150 | + Users will need to use slash commands instead 💪 |
| 151 | + You can re-enable prefix commands at any time using {mention.SlashCommand("command prefix")} ↩️ |
| 152 | + """); |
| 153 | + await responseClient.EditOriginalResponseAsync(button.Interaction, embed); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments