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