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