pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 2 | import Discord, { Channel, CommandInteraction, Message, MessageActionRow, MessageButton, MessageComponentInteraction, Role } from "discord.js"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 3 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 5 | import client from "../../utils/client.js"; |
| 6 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
| 7 | import generateKeyValueList from "../../utils/generateKeyValueList.js"; |
| 8 | import { ChannelType } from "discord-api-types"; |
| 9 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 10 | |
| 11 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 12 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | .setName("welcome") |
| 14 | .setDescription("Messages and roles sent or given when someone joins the server") |
| 15 | .addStringOption(option => option.setName("message").setDescription("The message to send when someone joins the server").setAutocomplete(true)) |
| 16 | .addRoleOption(option => option.setName("role").setDescription("The role given when someone joins the server")) |
| 17 | .addRoleOption(option => option.setName("ping").setDescription("The role pinged when someone joins the server")) |
| 18 | .addChannelOption(option => option.setName("channel").setDescription("The channel the welcome message should be sent to").addChannelTypes([ |
| 19 | ChannelType.GuildText, ChannelType.GuildNews |
| 20 | ])); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 21 | |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 22 | const callback = async (interaction: CommandInteraction): Promise<void | unknown> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | const { renderRole, renderChannel, log, NucleusColors, entry, renderUser } = client.logger; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 24 | await interaction.reply({embeds: LoadingEmbed, fetchReply: true, ephemeral: true}); |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 25 | let m: Message; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 26 | if (interaction.options.getRole("role") || interaction.options.getChannel("channel") || interaction.options.getString("message")) { |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 27 | let role: Role; |
| 28 | let ping: Role; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | const message = interaction.options.getString("message"); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 30 | try { |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 31 | role = interaction.options.getRole("role") as Role; |
| 32 | ping = interaction.options.getRole("ping") as Role; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 33 | } catch { |
| 34 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 35 | .setEmoji("GUILD.ROLES.DELETE") |
| 36 | .setTitle("Welcome Events") |
| 37 | .setDescription("The role you provided is not a valid role") |
| 38 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | ]}); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 40 | } |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 41 | let channel: Channel; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 42 | try { |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 43 | channel = interaction.options.getChannel("channel") as Channel; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 44 | } catch { |
| 45 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 46 | .setEmoji("GUILD.ROLES.DELETE") |
| 47 | .setTitle("Welcome Events") |
| 48 | .setDescription("The channel you provided is not a valid channel") |
| 49 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | ]}); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 51 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 52 | role = role as Discord.Role; |
| 53 | ping = ping as Discord.Role; |
| 54 | channel = channel as Discord.TextChannel; |
| 55 | const options = {}; |
| 56 | if (role) options["role"] = renderRole(role); |
| 57 | if (ping) options["ping"] = renderRole(ping); |
| 58 | if (channel) options["channel"] = renderChannel(channel); |
| 59 | if (message) options["message"] = "\n> " + message; |
| 60 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 61 | .setEmoji("GUILD.ROLES.EDIT") |
| 62 | .setTitle("Welcome Events") |
| 63 | .setDescription(generateKeyValueList(options)) |
| 64 | .setColor("Warning") |
| 65 | .setInverted(true) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | .send(true); |
| 67 | if (confirmation.cancelled) return; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 68 | if (confirmation.success) { |
| 69 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | const toChange = {}; |
| 71 | if (role) toChange["welcome.role"] = role.id; |
| 72 | if (ping) toChange["welcome.ping"] = ping.id; |
| 73 | if (channel) toChange["welcome.channel"] = channel.id; |
| 74 | if (message) toChange["welcome.message"] = message; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 75 | await client.database.guilds.write(interaction.guild.id, toChange); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 76 | const list = { |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 77 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 78 | changedBy: entry(interaction.user.id, renderUser(interaction.user)) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | }; |
| 80 | if (role) list["role"] = entry(role.id, renderRole(role)); |
| 81 | if (ping) list["ping"] = entry(ping.id, renderRole(ping)); |
| 82 | if (channel) list["channel"] = entry(channel.id, renderChannel(channel.id)); |
| 83 | if (message) list["message"] = entry(message, `\`${message}\``); |
| 84 | const data = { |
| 85 | meta:{ |
| 86 | type: "welcomeSettingsUpdated", |
| 87 | displayName: "Welcome Settings Changed", |
| 88 | calculateType: "nucleusSettingsUpdated", |
| 89 | color: NucleusColors.green, |
| 90 | emoji: "CONTROL.BLOCKTICK", |
| 91 | timestamp: new Date().getTime() |
| 92 | }, |
| 93 | list: list, |
| 94 | hidden: { |
| 95 | guild: interaction.guild.id |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 96 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 97 | }; |
| 98 | log(data); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 99 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 100 | console.log(e); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 101 | return interaction.editReply({embeds: [new EmojiEmbed() |
| 102 | .setTitle("Welcome Events") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 103 | .setDescription("Something went wrong while updating welcome settings") |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 104 | .setStatus("Danger") |
| 105 | .setEmoji("GUILD.ROLES.DELETE") |
| 106 | ], components: []}); |
| 107 | } |
| 108 | } else { |
| 109 | return interaction.editReply({embeds: [new EmojiEmbed() |
| 110 | .setTitle("Welcome Events") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 111 | .setDescription("No changes were made") |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 112 | .setStatus("Success") |
| 113 | .setEmoji("GUILD.ROLES.CREATE") |
| 114 | ], components: []}); |
| 115 | } |
| 116 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 117 | let lastClicked = null; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 118 | while (true) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 119 | const config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 120 | m = await interaction.editReply({embeds: [new EmojiEmbed() |
| 121 | .setTitle("Welcome Events") |
| 122 | .setDescription( |
| 123 | `**Message:** ${config.welcome.message ? `\n> ${config.welcome.message}` : "*None set*"}\n` + |
| 124 | `**Role:** ${config.welcome.role ? renderRole(await interaction.guild.roles.fetch(config.welcome.role)) : "*None set*"}\n` + |
| 125 | `**Ping:** ${config.welcome.ping ? renderRole(await interaction.guild.roles.fetch(config.welcome.ping)) : "*None set*"}\n` + |
| 126 | `**Channel:** ${config.welcome.channel ? (config.welcome.channel == "dm" ? "DM" : renderChannel(await interaction.guild.channels.fetch(config.welcome.channel))) : "*None set*"}` |
| 127 | ) |
| 128 | .setStatus("Success") |
| 129 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 130 | ], components: [ |
| 131 | new MessageActionRow().addComponents([ |
| 132 | new MessageButton() |
| 133 | .setLabel(lastClicked == "clear-message" ? "Click again to confirm" : "Clear Message") |
| 134 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 135 | .setCustomId("clear-message") |
| 136 | .setDisabled(!config.welcome.message) |
| 137 | .setStyle("DANGER"), |
| 138 | new MessageButton() |
| 139 | .setLabel(lastClicked == "clear-role" ? "Click again to confirm" : "Clear Role") |
| 140 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 141 | .setCustomId("clear-role") |
| 142 | .setDisabled(!config.welcome.role) |
| 143 | .setStyle("DANGER"), |
| 144 | new MessageButton() |
| 145 | .setLabel(lastClicked == "clear-ping" ? "Click again to confirm" : "Clear Ping") |
| 146 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 147 | .setCustomId("clear-ping") |
| 148 | .setDisabled(!config.welcome.ping) |
| 149 | .setStyle("DANGER"), |
| 150 | new MessageButton() |
| 151 | .setLabel(lastClicked == "clear-channel" ? "Click again to confirm" : "Clear Channel") |
| 152 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 153 | .setCustomId("clear-channel") |
| 154 | .setDisabled(!config.welcome.channel) |
| 155 | .setStyle("DANGER"), |
| 156 | new MessageButton() |
| 157 | .setLabel("Set Channel to DM") |
| 158 | .setCustomId("set-channel-dm") |
| 159 | .setDisabled(config.welcome.channel == "dm") |
| 160 | .setStyle("SECONDARY") |
| 161 | ]) |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 162 | ]}) as Message; |
| 163 | let i: MessageComponentInteraction; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 164 | try { |
| 165 | i = await m.awaitMessageComponent({ time: 300000 }); |
| 166 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 167 | break; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 168 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 169 | i.deferUpdate(); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 170 | if (i.customId == "clear-message") { |
| 171 | if (lastClicked == "clear-message") { |
| 172 | await client.database.guilds.write(interaction.guild.id, {"welcome.message": null}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 173 | lastClicked = null; |
| 174 | } else { lastClicked = "clear-message"; } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 175 | } else if (i.customId == "clear-role") { |
| 176 | if (lastClicked == "clear-role") { |
| 177 | await client.database.guilds.write(interaction.guild.id, {"welcome.role": null}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 178 | lastClicked = null; |
| 179 | } else { lastClicked = "clear-role"; } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 180 | } else if (i.customId == "clear-ping") { |
| 181 | if (lastClicked == "clear-ping") { |
| 182 | await client.database.guilds.write(interaction.guild.id, {"welcome.ping": null}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 183 | lastClicked = null; |
| 184 | } else { lastClicked = "clear-ping"; } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 185 | } else if (i.customId == "clear-channel") { |
| 186 | if (lastClicked == "clear-channel") { |
| 187 | await client.database.guilds.write(interaction.guild.id, {"welcome.channel": null}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 188 | lastClicked = null; |
| 189 | } else { lastClicked = "clear-channel"; } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 190 | } else if (i.customId == "set-channel-dm") { |
| 191 | await client.database.guilds.write(interaction.guild.id, {"welcome.channel": "dm"}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 192 | lastClicked = null; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | await interaction.editReply({embeds: [m.embeds[0].setFooter({text: "Message closed"})], components: []}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 196 | }; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 197 | |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 198 | const check = (interaction: CommandInteraction) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 199 | const member = (interaction.member as Discord.GuildMember); |
| 200 | if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 201 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 202 | }; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 203 | |
| 204 | export { command }; |
| 205 | export { callback }; |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 206 | export { check }; |