pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | import Discord, { |
| 3 | Channel, |
| 4 | CommandInteraction, |
| 5 | Message, |
| 6 | MessageActionRow, |
| 7 | MessageButton, |
| 8 | MessageComponentInteraction, |
| 9 | Role |
| 10 | } from "discord.js"; |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame^] | 11 | import type { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 12 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | import client from "../../utils/client.js"; |
| 14 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
| 15 | import generateKeyValueList from "../../utils/generateKeyValueList.js"; |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame^] | 16 | import { ChannelType } from "discord-api-types/v9"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 18 | |
| 19 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 20 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 21 | .setName("welcome") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 22 | .setDescription( |
| 23 | "Messages and roles sent or given when someone joins the server" |
| 24 | ) |
| 25 | .addStringOption((option) => |
| 26 | option |
| 27 | .setName("message") |
| 28 | .setDescription( |
| 29 | "The message to send when someone joins the server" |
| 30 | ) |
| 31 | .setAutocomplete(true) |
| 32 | ) |
| 33 | .addRoleOption((option) => |
| 34 | option |
| 35 | .setName("role") |
| 36 | .setDescription("The role given when someone joins the server") |
| 37 | ) |
| 38 | .addRoleOption((option) => |
| 39 | option |
| 40 | .setName("ping") |
| 41 | .setDescription("The role pinged when someone joins the server") |
| 42 | ) |
| 43 | .addChannelOption((option) => |
| 44 | option |
| 45 | .setName("channel") |
| 46 | .setDescription( |
| 47 | "The channel the welcome message should be sent to" |
| 48 | ) |
| 49 | .addChannelTypes([ChannelType.GuildText, ChannelType.GuildNews]) |
| 50 | ); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 51 | |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame^] | 52 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 53 | const { renderRole, renderChannel, log, NucleusColors, entry, renderUser } = |
| 54 | client.logger; |
| 55 | await interaction.reply({ |
| 56 | embeds: LoadingEmbed, |
| 57 | fetchReply: true, |
| 58 | ephemeral: true |
| 59 | }); |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 60 | let m: Message; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | if ( |
| 62 | interaction.options.getRole("role") || |
| 63 | interaction.options.getChannel("channel") || |
| 64 | interaction.options.getString("message") |
| 65 | ) { |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 66 | let role: Role; |
| 67 | let ping: Role; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | const message = interaction.options.getString("message"); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 69 | try { |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 70 | role = interaction.options.getRole("role") as Role; |
| 71 | ping = interaction.options.getRole("ping") as Role; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 72 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | return await interaction.editReply({ |
| 74 | embeds: [ |
| 75 | new EmojiEmbed() |
| 76 | .setEmoji("GUILD.ROLES.DELETE") |
| 77 | .setTitle("Welcome Events") |
| 78 | .setDescription( |
| 79 | "The role you provided is not a valid role" |
| 80 | ) |
| 81 | .setStatus("Danger") |
| 82 | ] |
| 83 | }); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 84 | } |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 85 | let channel: Channel; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 86 | try { |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 87 | channel = interaction.options.getChannel("channel") as Channel; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 88 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 89 | return await interaction.editReply({ |
| 90 | embeds: [ |
| 91 | new EmojiEmbed() |
| 92 | .setEmoji("GUILD.ROLES.DELETE") |
| 93 | .setTitle("Welcome Events") |
| 94 | .setDescription( |
| 95 | "The channel you provided is not a valid channel" |
| 96 | ) |
| 97 | .setStatus("Danger") |
| 98 | ] |
| 99 | }); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 100 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 101 | role = role as Discord.Role; |
| 102 | ping = ping as Discord.Role; |
| 103 | channel = channel as Discord.TextChannel; |
| 104 | const options = {}; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | if (role) options.role = renderRole(role); |
| 106 | if (ping) options.ping = renderRole(ping); |
| 107 | if (channel) options.channel = renderChannel(channel); |
| 108 | if (message) options.message = "\n> " + message; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 109 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 110 | .setEmoji("GUILD.ROLES.EDIT") |
| 111 | .setTitle("Welcome Events") |
| 112 | .setDescription(generateKeyValueList(options)) |
| 113 | .setColor("Warning") |
| 114 | .setInverted(true) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 115 | .send(true); |
| 116 | if (confirmation.cancelled) return; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 117 | if (confirmation.success) { |
| 118 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 119 | const toChange = {}; |
| 120 | if (role) toChange["welcome.role"] = role.id; |
| 121 | if (ping) toChange["welcome.ping"] = ping.id; |
| 122 | if (channel) toChange["welcome.channel"] = channel.id; |
| 123 | if (message) toChange["welcome.message"] = message; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 124 | await client.database.guilds.write( |
| 125 | interaction.guild.id, |
| 126 | toChange |
| 127 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 128 | const list = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 129 | memberId: entry( |
| 130 | interaction.user.id, |
| 131 | `\`${interaction.user.id}\`` |
| 132 | ), |
| 133 | changedBy: entry( |
| 134 | interaction.user.id, |
| 135 | renderUser(interaction.user) |
| 136 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 137 | }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 138 | if (role) list.role = entry(role.id, renderRole(role)); |
| 139 | if (ping) list.ping = entry(ping.id, renderRole(ping)); |
| 140 | if (channel) |
| 141 | list.channel = entry(channel.id, renderChannel(channel.id)); |
| 142 | if (message) list.message = entry(message, `\`${message}\``); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 143 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 144 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 145 | type: "welcomeSettingsUpdated", |
| 146 | displayName: "Welcome Settings Changed", |
| 147 | calculateType: "nucleusSettingsUpdated", |
| 148 | color: NucleusColors.green, |
| 149 | emoji: "CONTROL.BLOCKTICK", |
| 150 | timestamp: new Date().getTime() |
| 151 | }, |
| 152 | list: list, |
| 153 | hidden: { |
| 154 | guild: interaction.guild.id |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 155 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 156 | }; |
| 157 | log(data); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 158 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 159 | console.log(e); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 160 | return interaction.editReply({ |
| 161 | embeds: [ |
| 162 | new EmojiEmbed() |
| 163 | .setTitle("Welcome Events") |
| 164 | .setDescription( |
| 165 | "Something went wrong while updating welcome settings" |
| 166 | ) |
| 167 | .setStatus("Danger") |
| 168 | .setEmoji("GUILD.ROLES.DELETE") |
| 169 | ], |
| 170 | components: [] |
| 171 | }); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 172 | } |
| 173 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 174 | return interaction.editReply({ |
| 175 | embeds: [ |
| 176 | new EmojiEmbed() |
| 177 | .setTitle("Welcome Events") |
| 178 | .setDescription("No changes were made") |
| 179 | .setStatus("Success") |
| 180 | .setEmoji("GUILD.ROLES.CREATE") |
| 181 | ], |
| 182 | components: [] |
| 183 | }); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 184 | } |
| 185 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 186 | let lastClicked = null; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 187 | while (true) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 188 | const config = await client.database.guilds.read(interaction.guild.id); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 189 | m = (await interaction.editReply({ |
| 190 | embeds: [ |
| 191 | new EmojiEmbed() |
| 192 | .setTitle("Welcome Events") |
| 193 | .setDescription( |
| 194 | `**Message:** ${ |
| 195 | config.welcome.message |
| 196 | ? `\n> ${config.welcome.message}` |
| 197 | : "*None set*" |
| 198 | }\n` + |
| 199 | `**Role:** ${ |
| 200 | config.welcome.role |
| 201 | ? renderRole( |
| 202 | await interaction.guild.roles.fetch( |
| 203 | config.welcome.role |
| 204 | ) |
| 205 | ) |
| 206 | : "*None set*" |
| 207 | }\n` + |
| 208 | `**Ping:** ${ |
| 209 | config.welcome.ping |
| 210 | ? renderRole( |
| 211 | await interaction.guild.roles.fetch( |
| 212 | config.welcome.ping |
| 213 | ) |
| 214 | ) |
| 215 | : "*None set*" |
| 216 | }\n` + |
| 217 | `**Channel:** ${ |
| 218 | config.welcome.channel |
| 219 | ? config.welcome.channel == "dm" |
| 220 | ? "DM" |
| 221 | : renderChannel( |
| 222 | await interaction.guild.channels.fetch( |
| 223 | config.welcome.channel |
| 224 | ) |
| 225 | ) |
| 226 | : "*None set*" |
| 227 | }` |
| 228 | ) |
| 229 | .setStatus("Success") |
| 230 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 231 | ], |
| 232 | components: [ |
| 233 | new MessageActionRow().addComponents([ |
| 234 | new MessageButton() |
| 235 | .setLabel( |
| 236 | lastClicked == "clear-message" |
| 237 | ? "Click again to confirm" |
| 238 | : "Clear Message" |
| 239 | ) |
| 240 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 241 | .setCustomId("clear-message") |
| 242 | .setDisabled(!config.welcome.message) |
| 243 | .setStyle("DANGER"), |
| 244 | new MessageButton() |
| 245 | .setLabel( |
| 246 | lastClicked == "clear-role" |
| 247 | ? "Click again to confirm" |
| 248 | : "Clear Role" |
| 249 | ) |
| 250 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 251 | .setCustomId("clear-role") |
| 252 | .setDisabled(!config.welcome.role) |
| 253 | .setStyle("DANGER"), |
| 254 | new MessageButton() |
| 255 | .setLabel( |
| 256 | lastClicked == "clear-ping" |
| 257 | ? "Click again to confirm" |
| 258 | : "Clear Ping" |
| 259 | ) |
| 260 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 261 | .setCustomId("clear-ping") |
| 262 | .setDisabled(!config.welcome.ping) |
| 263 | .setStyle("DANGER"), |
| 264 | new MessageButton() |
| 265 | .setLabel( |
| 266 | lastClicked == "clear-channel" |
| 267 | ? "Click again to confirm" |
| 268 | : "Clear Channel" |
| 269 | ) |
| 270 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 271 | .setCustomId("clear-channel") |
| 272 | .setDisabled(!config.welcome.channel) |
| 273 | .setStyle("DANGER"), |
| 274 | new MessageButton() |
| 275 | .setLabel("Set Channel to DM") |
| 276 | .setCustomId("set-channel-dm") |
| 277 | .setDisabled(config.welcome.channel == "dm") |
| 278 | .setStyle("SECONDARY") |
| 279 | ]) |
| 280 | ] |
| 281 | })) as Message; |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 282 | let i: MessageComponentInteraction; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 283 | try { |
| 284 | i = await m.awaitMessageComponent({ time: 300000 }); |
| 285 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 286 | break; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 287 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 288 | i.deferUpdate(); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 289 | if (i.customId == "clear-message") { |
| 290 | if (lastClicked == "clear-message") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 291 | await client.database.guilds.write(interaction.guild.id, { |
| 292 | "welcome.message": null |
| 293 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 294 | lastClicked = null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 295 | } else { |
| 296 | lastClicked = "clear-message"; |
| 297 | } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 298 | } else if (i.customId == "clear-role") { |
| 299 | if (lastClicked == "clear-role") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 300 | await client.database.guilds.write(interaction.guild.id, { |
| 301 | "welcome.role": null |
| 302 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 303 | lastClicked = null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 304 | } else { |
| 305 | lastClicked = "clear-role"; |
| 306 | } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 307 | } else if (i.customId == "clear-ping") { |
| 308 | if (lastClicked == "clear-ping") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 309 | await client.database.guilds.write(interaction.guild.id, { |
| 310 | "welcome.ping": null |
| 311 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 312 | lastClicked = null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 313 | } else { |
| 314 | lastClicked = "clear-ping"; |
| 315 | } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 316 | } else if (i.customId == "clear-channel") { |
| 317 | if (lastClicked == "clear-channel") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 318 | await client.database.guilds.write(interaction.guild.id, { |
| 319 | "welcome.channel": null |
| 320 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 321 | lastClicked = null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 322 | } else { |
| 323 | lastClicked = "clear-channel"; |
| 324 | } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 325 | } else if (i.customId == "set-channel-dm") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 326 | await client.database.guilds.write(interaction.guild.id, { |
| 327 | "welcome.channel": "dm" |
| 328 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 329 | lastClicked = null; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 330 | } |
| 331 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 332 | await interaction.editReply({ |
| 333 | embeds: [m.embeds[0].setFooter({ text: "Message closed" })], |
| 334 | components: [] |
| 335 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 336 | }; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 337 | |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 338 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 339 | const member = interaction.member as Discord.GuildMember; |
| 340 | if (!member.permissions.has("MANAGE_GUILD")) |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame^] | 341 | throw new Error( |
| 342 | "You must have the *Manage Server* permission to use this command" |
| 343 | ); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 344 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 345 | }; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 346 | |
| 347 | export { command }; |
| 348 | export { callback }; |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 349 | export { check }; |