PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../utils/defaults.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | import Discord, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 3 | CommandInteraction, |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 4 | AutocompleteInteraction, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 5 | ActionRowBuilder, |
| 6 | ButtonBuilder, |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 7 | ButtonStyle, |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 8 | APIMessageComponentEmoji, |
| 9 | ChannelSelectMenuBuilder, |
| 10 | RoleSelectMenuBuilder, |
| 11 | RoleSelectMenuInteraction, |
| 12 | ChannelSelectMenuInteraction, |
| 13 | ButtonInteraction, |
| 14 | ModalBuilder, |
| 15 | TextInputBuilder, |
| 16 | TextInputStyle, |
| 17 | ModalSubmitInteraction, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 18 | } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 19 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 20 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 21 | import client from "../../utils/client.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 22 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 23 | import convertCurlyBracketString from "../../utils/convertCurlyBracketString.js"; |
| 24 | import { modalInteractionCollector } from "../../utils/dualCollector.js"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 25 | |
| 26 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 27 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | .setName("welcome") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 29 | .setDescription("Messages and roles sent or given when someone joins the server") |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 30 | |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 31 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
| 32 | const { renderChannel } = client.logger; |
| 33 | const m = await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 34 | embeds: LoadingEmbed, |
| 35 | fetchReply: true, |
| 36 | ephemeral: true |
| 37 | }); |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 38 | let closed = false; |
| 39 | let config = await client.database.guilds.read(interaction.guild!.id); |
| 40 | let data = Object.assign({}, config.welcome); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 41 | do { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 42 | const buttons = new ActionRowBuilder<ButtonBuilder>() |
| 43 | .addComponents( |
| 44 | new ButtonBuilder() |
| 45 | .setCustomId("switch") |
| 46 | .setLabel(data.enabled ? "Enabled" : "Disabled") |
| 47 | .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger) |
| 48 | .setEmoji(getEmojiByName(data.enabled ? "CONTROL.TICK" : "CONTROL.CROSS", "id") as APIMessageComponentEmoji), |
| 49 | new ButtonBuilder() |
| 50 | .setCustomId("message") |
| 51 | .setLabel((data.message ? "Change" : "Set") + "Message") |
| 52 | .setStyle(ButtonStyle.Primary) |
| 53 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
| 54 | new ButtonBuilder() |
| 55 | .setCustomId("channelDM") |
| 56 | .setLabel("Send in DMs") |
| 57 | .setStyle(ButtonStyle.Primary) |
| 58 | .setDisabled(data.channel === "dm"), |
| 59 | new ButtonBuilder() |
| 60 | .setCustomId("role") |
| 61 | .setLabel("Clear Role") |
| 62 | .setStyle(ButtonStyle.Danger) |
| 63 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id") as APIMessageComponentEmoji), |
| 64 | new ButtonBuilder() |
| 65 | .setCustomId("save") |
| 66 | .setLabel("Save") |
| 67 | .setStyle(ButtonStyle.Success) |
| 68 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji) |
| 69 | .setDisabled( |
| 70 | data.enabled === config.welcome.enabled && |
| 71 | data.message === config.welcome.message && |
| 72 | data.role === config.welcome.role && |
| 73 | data.ping === config.welcome.ping && |
| 74 | data.channel === config.welcome.channel |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 75 | ) |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 76 | ); |
| 77 | |
| 78 | const channelMenu = new ActionRowBuilder<ChannelSelectMenuBuilder>() |
| 79 | .addComponents( |
| 80 | new ChannelSelectMenuBuilder() |
| 81 | .setCustomId("channel") |
| 82 | .setPlaceholder("Select a channel to send welcome messages to") |
| 83 | ); |
| 84 | const roleMenu = new ActionRowBuilder<RoleSelectMenuBuilder>() |
| 85 | .addComponents( |
| 86 | new RoleSelectMenuBuilder() |
| 87 | .setCustomId("roleToGive") |
| 88 | .setPlaceholder("Select a role to give to the member when they join the server") |
| 89 | ); |
| 90 | const pingMenu = new ActionRowBuilder<RoleSelectMenuBuilder>() |
| 91 | .addComponents( |
| 92 | new RoleSelectMenuBuilder() |
| 93 | .setCustomId("roleToPing") |
| 94 | .setPlaceholder("Select a role to ping when a member joins the server") |
| 95 | ); |
| 96 | |
| 97 | const embed = new EmojiEmbed() |
| 98 | .setTitle("Welcome Settings") |
| 99 | .setStatus("Success") |
| 100 | .setDescription( |
| 101 | `${getEmojiByName(data.enabled ? "CONTROL.TICK" : "CONTROL.CROSS")} Welcome messages and roles are ${data.enabled ? "enabled" : "disabled"}\n` + |
| 102 | `**Welcome message:** ${data.message ? |
| 103 | `\n> ` + |
| 104 | await convertCurlyBracketString( |
| 105 | data.message, |
| 106 | interaction.user.id, |
| 107 | interaction.user.username, |
| 108 | interaction.guild!.name, |
| 109 | interaction.guild!.members |
| 110 | ) |
| 111 | : "*None*"}\n` + |
| 112 | `**Send message in:** ` + (data.channel ? (data.channel == "dm" ? "DMs" : renderChannel(data.channel)) : `*None set*`) + `\n` + |
| 113 | `**Role to ping:** ` + (data.ping ? `<@&${data.ping}>` : `*None set*`) + `\n` + |
| 114 | `**Role given on join:** ` + (data.role ? `<@&${data.role}>` : `*None set*`) |
| 115 | ) |
| 116 | |
| 117 | await interaction.editReply({ |
| 118 | embeds: [embed], |
| 119 | components: [buttons, channelMenu, roleMenu, pingMenu] |
| 120 | }); |
| 121 | |
| 122 | let i: RoleSelectMenuInteraction | ChannelSelectMenuInteraction | ButtonInteraction; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 123 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 124 | i = await m.awaitMessageComponent({ |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 125 | filter: (interaction) => interaction.user.id === interaction.user.id, |
| 126 | time: 300000 |
| 127 | }) as RoleSelectMenuInteraction | ChannelSelectMenuInteraction | ButtonInteraction; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 128 | } catch (e) { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 129 | closed = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 130 | continue; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 131 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 132 | |
| 133 | if(i.isButton()) { |
| 134 | switch(i.customId) { |
| 135 | case "switch": { |
| 136 | await i.deferUpdate(); |
| 137 | data.enabled = !data.enabled; |
| 138 | break; |
| 139 | } |
| 140 | case "message": { |
| 141 | const modal = new ModalBuilder() |
| 142 | .setCustomId("modal") |
| 143 | .setTitle("Welcome Message") |
| 144 | .addComponents( |
| 145 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 146 | new TextInputBuilder() |
| 147 | .setCustomId("ex1") |
| 148 | .setLabel("Server Info (1/3)") |
| 149 | .setPlaceholder( |
| 150 | `{serverName} - This server's name\n\n` + |
| 151 | `These placeholders will be replaced with the server's name, etc..` |
| 152 | ) |
| 153 | .setMaxLength(1) |
| 154 | .setRequired(false) |
| 155 | .setStyle(TextInputStyle.Paragraph) |
| 156 | ), |
| 157 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 158 | new TextInputBuilder() |
| 159 | .setCustomId("ex2") |
| 160 | .setLabel("Member Counts (2/3) - {MemberCount:...}") |
| 161 | .setPlaceholder( |
| 162 | `{:all} - Total member count\n` + |
| 163 | `{:humans} - Total non-bot users\n` + |
| 164 | `{:bots} - Number of bots\n` |
| 165 | ) |
| 166 | .setMaxLength(1) |
| 167 | .setRequired(false) |
| 168 | .setStyle(TextInputStyle.Paragraph) |
| 169 | ), |
| 170 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 171 | new TextInputBuilder() |
| 172 | .setCustomId("ex3") |
| 173 | .setLabel("Member who joined (3/3) - {member:...}") |
| 174 | .setPlaceholder( |
| 175 | `{:name} - The members name\n` |
| 176 | ) |
| 177 | .setMaxLength(1) |
| 178 | .setRequired(false) |
| 179 | .setStyle(TextInputStyle.Paragraph) |
| 180 | ), |
| 181 | new ActionRowBuilder<TextInputBuilder>() |
| 182 | .addComponents( |
| 183 | new TextInputBuilder() |
| 184 | .setCustomId("message") |
| 185 | .setPlaceholder("Enter a message to send when someone joins the server") |
| 186 | .setValue(data.message ?? "") |
| 187 | .setLabel("Message") |
| 188 | .setStyle(TextInputStyle.Paragraph) |
| 189 | ) |
| 190 | ) |
| 191 | const button = new ActionRowBuilder<ButtonBuilder>() |
| 192 | .addComponents( |
| 193 | new ButtonBuilder() |
| 194 | .setCustomId("back") |
| 195 | .setLabel("Back") |
| 196 | .setStyle(ButtonStyle.Secondary) |
| 197 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 198 | ) |
| 199 | await i.showModal(modal) |
| 200 | await i.editReply({ |
| 201 | embeds: [ |
| 202 | new EmojiEmbed() |
| 203 | .setTitle("Welcome Settings") |
| 204 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 205 | .setStatus("Success") |
| 206 | ], |
| 207 | components: [button] |
| 208 | }); |
| 209 | |
| 210 | let out: ModalSubmitInteraction | null; |
| 211 | try { |
| 212 | out = await modalInteractionCollector(m, interaction.user) as ModalSubmitInteraction | null; |
| 213 | } catch (e) { |
| 214 | console.error(e); |
| 215 | out = null; |
| 216 | } |
| 217 | if(!out) break; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 218 | data.message = out.fields.getTextInputValue("message"); |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | case "save": { |
| 222 | await i.deferUpdate(); |
| 223 | await client.database.guilds.write(interaction.guild!.id, {"welcome": data}); |
| 224 | config = await client.database.guilds.read(interaction.guild!.id); |
| 225 | data = Object.assign({}, config.welcome); |
| 226 | break; |
| 227 | } |
| 228 | case "channelDM": { |
| 229 | await i.deferUpdate(); |
| 230 | data.channel = "dm"; |
| 231 | break; |
| 232 | } |
| 233 | case "role": { |
| 234 | await i.deferUpdate(); |
| 235 | data.role = null; |
| 236 | break; |
| 237 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 238 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 239 | } else if (i.isRoleSelectMenu()) { |
| 240 | await i.deferUpdate(); |
| 241 | switch(i.customId) { |
| 242 | case "roleToGive": { |
| 243 | data.role = i.values[0]!; |
| 244 | break |
| 245 | } |
| 246 | case "roleToPing": { |
| 247 | data.ping = i.values[0]!; |
| 248 | break |
| 249 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 250 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 251 | } else { |
| 252 | await i.deferUpdate(); |
| 253 | data.channel = i.values[0]!; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 254 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 255 | |
| 256 | } while (!closed); |
| 257 | await interaction.deleteReply() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 258 | }; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 259 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 260 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 261 | const member = interaction.member as Discord.GuildMember; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 262 | if (!member.permissions.has("ManageGuild")) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 263 | return "You must have the *Manage Server* permission to use this command"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 264 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 265 | }; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 266 | |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 267 | const autocomplete = async (interaction: AutocompleteInteraction): Promise<string[]> => { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 268 | const validReplacements = ["serverName", "memberCount:all", "memberCount:bots", "memberCount:humans"] |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 269 | if (!interaction.guild) return []; |
| 270 | const prompt = interaction.options.getString("message"); |
| 271 | const autocompletions = []; |
| 272 | if ( prompt === null ) { |
| 273 | for (const replacement of validReplacements) { |
| 274 | autocompletions.push(`{${replacement}}`); |
| 275 | }; |
| 276 | return autocompletions; |
| 277 | }; |
| 278 | const beforeLastOpenBracket = prompt.match(/(.*){[^{}]{0,15}$/); |
| 279 | const afterLastOpenBracket = prompt.match(/{[^{}]{0,15}$/); |
| 280 | if (beforeLastOpenBracket !== null) { |
| 281 | if (afterLastOpenBracket !== null) { |
| 282 | for (const replacement of validReplacements) { |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 283 | if (replacement.startsWith(afterLastOpenBracket[0]!.slice(1))) { |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 284 | autocompletions.push(`${beforeLastOpenBracket[1]}{${replacement}}`); |
| 285 | } |
| 286 | } |
| 287 | } else { |
| 288 | for (const replacement of validReplacements) { |
| 289 | autocompletions.push(`${beforeLastOpenBracket[1]}{${replacement}}`); |
| 290 | } |
| 291 | } |
| 292 | } else { |
| 293 | for (const replacement of validReplacements) { |
| 294 | autocompletions.push(`${prompt} {${replacement}}`); |
| 295 | } |
| 296 | } |
| 297 | return autocompletions; |
| 298 | }; |
| 299 | |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 300 | export { command, callback, check, autocomplete }; |