pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from './../../utils/defaultEmbeds.js'; |
| 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"; |
| 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'; |
| 11 | |
| 12 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 13 | builder |
| 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 | ])) |
| 22 | |
| 23 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 24 | const { renderRole, renderChannel, log, NucleusColors, entry, renderUser } = client.logger |
| 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; |
| 30 | let message = interaction.options.getString("message"); |
| 31 | try { |
| 32 | role = interaction.options.getRole("role") |
| 33 | ping = interaction.options.getRole("ping") |
| 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") |
| 40 | ]}) |
| 41 | } |
| 42 | let channel; |
| 43 | try { |
| 44 | channel = interaction.options.getChannel("channel") |
| 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") |
| 51 | ]}) |
| 52 | } |
| 53 | role = role as Discord.Role |
| 54 | ping = ping as Discord.Role |
| 55 | channel = channel as Discord.TextChannel |
| 56 | let 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 | let confirmation = await new confirmationMessage(interaction) |
| 62 | .setEmoji("GUILD.ROLES.EDIT") |
| 63 | .setTitle("Welcome Events") |
| 64 | .setDescription(generateKeyValueList(options)) |
| 65 | .setColor("Warning") |
| 66 | .setInverted(true) |
| 67 | .send(true) |
| 68 | if (confirmation.cancelled) return |
| 69 | if (confirmation.success) { |
| 70 | try { |
| 71 | let 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 |
| 76 | await client.database.guilds.write(interaction.guild.id, toChange); |
| 77 | let list = { |
| 78 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 79 | changedBy: entry(interaction.user.id, renderUser(interaction.user)) |
| 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 | try { |
| 86 | let data = { |
| 87 | meta:{ |
| 88 | type: 'welcomeSettingsUpdated', |
| 89 | displayName: 'Welcome Settings Changed', |
| 90 | calculateType: 'nucleusSettingsUpdated', |
| 91 | color: NucleusColors.green, |
| 92 | emoji: "CONTROL.BLOCKTICK", |
| 93 | timestamp: new Date().getTime() |
| 94 | }, |
| 95 | list: list, |
| 96 | hidden: { |
| 97 | guild: interaction.guild.id |
| 98 | } |
| 99 | } |
| 100 | log(data); |
| 101 | } catch {} |
| 102 | } catch (e) { |
| 103 | console.log(e) |
| 104 | return interaction.editReply({embeds: [new EmojiEmbed() |
| 105 | .setTitle("Welcome Events") |
| 106 | .setDescription(`Something went wrong while updating welcome settings`) |
| 107 | .setStatus("Danger") |
| 108 | .setEmoji("GUILD.ROLES.DELETE") |
| 109 | ], components: []}); |
| 110 | } |
| 111 | } else { |
| 112 | return interaction.editReply({embeds: [new EmojiEmbed() |
| 113 | .setTitle("Welcome Events") |
| 114 | .setDescription(`No changes were made`) |
| 115 | .setStatus("Success") |
| 116 | .setEmoji("GUILD.ROLES.CREATE") |
| 117 | ], components: []}); |
| 118 | } |
| 119 | } |
| 120 | let lastClicked = null |
| 121 | while (true) { |
| 122 | let config = await client.database.guilds.read(interaction.guild.id) |
| 123 | m = await interaction.editReply({embeds: [new EmojiEmbed() |
| 124 | .setTitle("Welcome Events") |
| 125 | .setDescription( |
| 126 | `**Message:** ${config.welcome.message ? `\n> ${config.welcome.message}` : "*None set*"}\n` + |
| 127 | `**Role:** ${config.welcome.role ? renderRole(await interaction.guild.roles.fetch(config.welcome.role)) : "*None set*"}\n` + |
| 128 | `**Ping:** ${config.welcome.ping ? renderRole(await interaction.guild.roles.fetch(config.welcome.ping)) : "*None set*"}\n` + |
| 129 | `**Channel:** ${config.welcome.channel ? (config.welcome.channel == "dm" ? "DM" : renderChannel(await interaction.guild.channels.fetch(config.welcome.channel))) : "*None set*"}` |
| 130 | ) |
| 131 | .setStatus("Success") |
| 132 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 133 | ], components: [ |
| 134 | new MessageActionRow().addComponents([ |
| 135 | new MessageButton() |
| 136 | .setLabel(lastClicked == "clear-message" ? "Click again to confirm" : "Clear Message") |
| 137 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 138 | .setCustomId("clear-message") |
| 139 | .setDisabled(!config.welcome.message) |
| 140 | .setStyle("DANGER"), |
| 141 | new MessageButton() |
| 142 | .setLabel(lastClicked == "clear-role" ? "Click again to confirm" : "Clear Role") |
| 143 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 144 | .setCustomId("clear-role") |
| 145 | .setDisabled(!config.welcome.role) |
| 146 | .setStyle("DANGER"), |
| 147 | new MessageButton() |
| 148 | .setLabel(lastClicked == "clear-ping" ? "Click again to confirm" : "Clear Ping") |
| 149 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 150 | .setCustomId("clear-ping") |
| 151 | .setDisabled(!config.welcome.ping) |
| 152 | .setStyle("DANGER"), |
| 153 | new MessageButton() |
| 154 | .setLabel(lastClicked == "clear-channel" ? "Click again to confirm" : "Clear Channel") |
| 155 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 156 | .setCustomId("clear-channel") |
| 157 | .setDisabled(!config.welcome.channel) |
| 158 | .setStyle("DANGER"), |
| 159 | new MessageButton() |
| 160 | .setLabel("Set Channel to DM") |
| 161 | .setCustomId("set-channel-dm") |
| 162 | .setDisabled(config.welcome.channel == "dm") |
| 163 | .setStyle("SECONDARY") |
| 164 | ]) |
| 165 | ]}) |
| 166 | let i; |
| 167 | try { |
| 168 | i = await m.awaitMessageComponent({ time: 300000 }); |
| 169 | } catch (e) { |
| 170 | break |
| 171 | } |
| 172 | i.deferUpdate() |
| 173 | if (i.customId == "clear-message") { |
| 174 | if (lastClicked == "clear-message") { |
| 175 | await client.database.guilds.write(interaction.guild.id, {"welcome.message": null}); |
| 176 | lastClicked = null |
| 177 | } else { lastClicked = "clear-message" } |
| 178 | } else if (i.customId == "clear-role") { |
| 179 | if (lastClicked == "clear-role") { |
| 180 | await client.database.guilds.write(interaction.guild.id, {"welcome.role": null}); |
| 181 | lastClicked = null |
| 182 | } else { lastClicked = "clear-role" } |
| 183 | } else if (i.customId == "clear-ping") { |
| 184 | if (lastClicked == "clear-ping") { |
| 185 | await client.database.guilds.write(interaction.guild.id, {"welcome.ping": null}); |
| 186 | lastClicked = null |
| 187 | } else { lastClicked = "clear-ping" } |
| 188 | } else if (i.customId == "clear-channel") { |
| 189 | if (lastClicked == "clear-channel") { |
| 190 | await client.database.guilds.write(interaction.guild.id, {"welcome.channel": null}); |
| 191 | lastClicked = null |
| 192 | } else { lastClicked = "clear-channel" } |
| 193 | } else if (i.customId == "set-channel-dm") { |
| 194 | await client.database.guilds.write(interaction.guild.id, {"welcome.channel": "dm"}); |
| 195 | lastClicked = null |
| 196 | } |
| 197 | } |
| 198 | await interaction.editReply({embeds: [m.embeds[0].setFooter({text: "Message closed"})], components: []}); |
| 199 | } |
| 200 | |
| 201 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
| 202 | let member = (interaction.member as Discord.GuildMember) |
| 203 | if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command" |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | export { command }; |
| 208 | export { callback }; |
| 209 | export { check }; |