blob: 16857acc911322454bc3f121592f94fe04192491 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
Skyler Grey75ea9172022-08-06 10:22:23 +01002import Discord, {
3 Channel,
4 CommandInteraction,
5 Message,
6 MessageActionRow,
7 MessageButton,
8 MessageComponentInteraction,
9 Role
10} from "discord.js";
Skyler Greyc634e2b2022-08-06 17:50:48 +010011import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan41d93562022-07-30 22:10:15 +010012import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +010013import client from "../../utils/client.js";
14import confirmationMessage from "../../utils/confirmationMessage.js";
15import generateKeyValueList from "../../utils/generateKeyValueList.js";
Skyler Greyc634e2b2022-08-06 17:50:48 +010016import { ChannelType } from "discord-api-types/v9";
pineafan63fc5e22022-08-04 22:04:10 +010017import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan41d93562022-07-30 22:10:15 +010018
19const command = (builder: SlashCommandSubcommandBuilder) =>
20 builder
pineafan63fc5e22022-08-04 22:04:10 +010021 .setName("welcome")
Skyler Grey11236ba2022-08-08 21:13:33 +010022 .setDescription("Messages and roles sent or given when someone joins the server")
Skyler Grey75ea9172022-08-06 10:22:23 +010023 .addStringOption((option) =>
24 option
25 .setName("message")
Skyler Grey11236ba2022-08-08 21:13:33 +010026 .setDescription("The message to send when someone joins the server")
Skyler Grey75ea9172022-08-06 10:22:23 +010027 .setAutocomplete(true)
28 )
29 .addRoleOption((option) =>
Skyler Grey11236ba2022-08-08 21:13:33 +010030 option.setName("role").setDescription("The role given when someone joins the server")
Skyler Grey75ea9172022-08-06 10:22:23 +010031 )
32 .addRoleOption((option) =>
Skyler Grey11236ba2022-08-08 21:13:33 +010033 option.setName("ping").setDescription("The role pinged when someone joins the server")
Skyler Grey75ea9172022-08-06 10:22:23 +010034 )
35 .addChannelOption((option) =>
36 option
37 .setName("channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010038 .setDescription("The channel the welcome message should be sent to")
Skyler Grey75ea9172022-08-06 10:22:23 +010039 .addChannelTypes([ChannelType.GuildText, ChannelType.GuildNews])
40 );
pineafan41d93562022-07-30 22:10:15 +010041
Skyler Greyc634e2b2022-08-06 17:50:48 +010042const callback = async (interaction: CommandInteraction): Promise<unknown> => {
Skyler Grey11236ba2022-08-08 21:13:33 +010043 const { renderRole, renderChannel, log, NucleusColors, entry, renderUser } = client.logger;
Skyler Grey75ea9172022-08-06 10:22:23 +010044 await interaction.reply({
45 embeds: LoadingEmbed,
46 fetchReply: true,
47 ephemeral: true
48 });
Skyler Grey1a67e182022-08-04 23:05:44 +010049 let m: Message;
Skyler Grey75ea9172022-08-06 10:22:23 +010050 if (
51 interaction.options.getRole("role") ||
52 interaction.options.getChannel("channel") ||
53 interaction.options.getString("message")
54 ) {
pineafan4e425942022-08-08 22:01:47 +010055 let role: Role | null;
56 let ping: Role | null;
57 let channel: Channel | null;
58 const message: string | null = interaction.options.getString("message");
pineafan41d93562022-07-30 22:10:15 +010059 try {
pineafan4e425942022-08-08 22:01:47 +010060 role = interaction.options.getRole("role") as Role | null;
61 ping = interaction.options.getRole("ping") as Role | null;
pineafan41d93562022-07-30 22:10:15 +010062 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010063 return await interaction.editReply({
64 embeds: [
65 new EmojiEmbed()
66 .setEmoji("GUILD.ROLES.DELETE")
67 .setTitle("Welcome Events")
Skyler Grey11236ba2022-08-08 21:13:33 +010068 .setDescription("The role you provided is not a valid role")
Skyler Grey75ea9172022-08-06 10:22:23 +010069 .setStatus("Danger")
70 ]
71 });
pineafan41d93562022-07-30 22:10:15 +010072 }
pineafan41d93562022-07-30 22:10:15 +010073 try {
pineafan4e425942022-08-08 22:01:47 +010074 channel = interaction.options.getChannel("channel") as Channel | null;
pineafan41d93562022-07-30 22:10:15 +010075 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010076 return await interaction.editReply({
77 embeds: [
78 new EmojiEmbed()
79 .setEmoji("GUILD.ROLES.DELETE")
80 .setTitle("Welcome Events")
Skyler Grey11236ba2022-08-08 21:13:33 +010081 .setDescription("The channel you provided is not a valid channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010082 .setStatus("Danger")
83 ]
84 });
pineafan41d93562022-07-30 22:10:15 +010085 }
pineafan4e425942022-08-08 22:01:47 +010086 const options: {
87 role?: string;
88 ping?: string;
89 channel?: string;
90 message?: string;
91 } = {};
92
Skyler Grey75ea9172022-08-06 10:22:23 +010093 if (role) options.role = renderRole(role);
94 if (ping) options.ping = renderRole(ping);
95 if (channel) options.channel = renderChannel(channel);
96 if (message) options.message = "\n> " + message;
pineafan63fc5e22022-08-04 22:04:10 +010097 const confirmation = await new confirmationMessage(interaction)
pineafan62ce1922022-08-25 20:34:45 +010098 .setEmoji("GUILD.ROLES.EDIT", "GUILD.ROLES.DELETE")
pineafan41d93562022-07-30 22:10:15 +010099 .setTitle("Welcome Events")
100 .setDescription(generateKeyValueList(options))
101 .setColor("Warning")
102 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +0100103 .send(true);
104 if (confirmation.cancelled) return;
pineafan41d93562022-07-30 22:10:15 +0100105 if (confirmation.success) {
106 try {
pineafan4e425942022-08-08 22:01:47 +0100107 const toChange: {
108 "welcome.role"?: string;
109 "welcome.ping"?: string;
110 "welcome.channel"?: string;
111 "welcome.message"?: string;
112 } = {};
pineafan63fc5e22022-08-04 22:04:10 +0100113 if (role) toChange["welcome.role"] = role.id;
114 if (ping) toChange["welcome.ping"] = ping.id;
115 if (channel) toChange["welcome.channel"] = channel.id;
116 if (message) toChange["welcome.message"] = message;
pineafan4e425942022-08-08 22:01:47 +0100117 await client.database.guilds.write(interaction.guild!.id, toChange);
118 const list: {
119 memberId: ReturnType<typeof entry>;
120 changedBy: ReturnType<typeof entry>;
121 role?: ReturnType<typeof entry>;
122 ping?: ReturnType<typeof entry>;
123 channel?: ReturnType<typeof entry>;
124 message?: ReturnType<typeof entry>;
125 } = {
Skyler Grey11236ba2022-08-08 21:13:33 +0100126 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
127 changedBy: entry(interaction.user.id, renderUser(interaction.user))
pineafan63fc5e22022-08-04 22:04:10 +0100128 };
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 if (role) list.role = entry(role.id, renderRole(role));
130 if (ping) list.ping = entry(ping.id, renderRole(ping));
Skyler Grey11236ba2022-08-08 21:13:33 +0100131 if (channel) list.channel = entry(channel.id, renderChannel(channel.id));
Skyler Grey75ea9172022-08-06 10:22:23 +0100132 if (message) list.message = entry(message, `\`${message}\``);
pineafan63fc5e22022-08-04 22:04:10 +0100133 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100135 type: "welcomeSettingsUpdated",
136 displayName: "Welcome Settings Changed",
137 calculateType: "nucleusSettingsUpdated",
138 color: NucleusColors.green,
139 emoji: "CONTROL.BLOCKTICK",
140 timestamp: new Date().getTime()
141 },
142 list: list,
143 hidden: {
pineafan4e425942022-08-08 22:01:47 +0100144 guild: interaction.guild!.id
pineafan41d93562022-07-30 22:10:15 +0100145 }
pineafan63fc5e22022-08-04 22:04:10 +0100146 };
147 log(data);
pineafan41d93562022-07-30 22:10:15 +0100148 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100149 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +0100150 return interaction.editReply({
151 embeds: [
152 new EmojiEmbed()
153 .setTitle("Welcome Events")
Skyler Grey11236ba2022-08-08 21:13:33 +0100154 .setDescription("Something went wrong while updating welcome settings")
Skyler Grey75ea9172022-08-06 10:22:23 +0100155 .setStatus("Danger")
156 .setEmoji("GUILD.ROLES.DELETE")
157 ],
158 components: []
159 });
pineafan41d93562022-07-30 22:10:15 +0100160 }
161 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100162 return interaction.editReply({
163 embeds: [
164 new EmojiEmbed()
165 .setTitle("Welcome Events")
166 .setDescription("No changes were made")
167 .setStatus("Success")
168 .setEmoji("GUILD.ROLES.CREATE")
169 ],
170 components: []
171 });
pineafan41d93562022-07-30 22:10:15 +0100172 }
173 }
pineafan63fc5e22022-08-04 22:04:10 +0100174 let lastClicked = null;
Skyler Greyad002172022-08-16 18:48:26 +0100175 let timedOut = false;
176 do {
pineafan4e425942022-08-08 22:01:47 +0100177 const config = await client.database.guilds.read(interaction.guild!.id);
Skyler Grey75ea9172022-08-06 10:22:23 +0100178 m = (await interaction.editReply({
179 embeds: [
180 new EmojiEmbed()
181 .setTitle("Welcome Events")
182 .setDescription(
Skyler Grey11236ba2022-08-08 21:13:33 +0100183 `**Message:** ${config.welcome.message ? `\n> ${config.welcome.message}` : "*None set*"}\n` +
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 `**Role:** ${
185 config.welcome.role
pineafan4e425942022-08-08 22:01:47 +0100186 ? renderRole(await interaction.guild!.roles.fetch(config.welcome.role))
Skyler Grey75ea9172022-08-06 10:22:23 +0100187 : "*None set*"
188 }\n` +
189 `**Ping:** ${
190 config.welcome.ping
pineafan4e425942022-08-08 22:01:47 +0100191 ? renderRole(await interaction.guild!.roles.fetch(config.welcome.ping))
Skyler Grey75ea9172022-08-06 10:22:23 +0100192 : "*None set*"
193 }\n` +
194 `**Channel:** ${
195 config.welcome.channel
196 ? config.welcome.channel == "dm"
197 ? "DM"
pineafan4e425942022-08-08 22:01:47 +0100198 : renderChannel(await interaction.guild!.channels.fetch(config.welcome.channel))
Skyler Grey75ea9172022-08-06 10:22:23 +0100199 : "*None set*"
200 }`
201 )
202 .setStatus("Success")
203 .setEmoji("CHANNEL.TEXT.CREATE")
204 ],
205 components: [
206 new MessageActionRow().addComponents([
207 new MessageButton()
Skyler Grey11236ba2022-08-08 21:13:33 +0100208 .setLabel(lastClicked == "clear-message" ? "Click again to confirm" : "Clear Message")
Skyler Grey75ea9172022-08-06 10:22:23 +0100209 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
210 .setCustomId("clear-message")
211 .setDisabled(!config.welcome.message)
212 .setStyle("DANGER"),
213 new MessageButton()
Skyler Grey11236ba2022-08-08 21:13:33 +0100214 .setLabel(lastClicked == "clear-role" ? "Click again to confirm" : "Clear Role")
Skyler Grey75ea9172022-08-06 10:22:23 +0100215 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
216 .setCustomId("clear-role")
217 .setDisabled(!config.welcome.role)
218 .setStyle("DANGER"),
219 new MessageButton()
Skyler Grey11236ba2022-08-08 21:13:33 +0100220 .setLabel(lastClicked == "clear-ping" ? "Click again to confirm" : "Clear Ping")
Skyler Grey75ea9172022-08-06 10:22:23 +0100221 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
222 .setCustomId("clear-ping")
223 .setDisabled(!config.welcome.ping)
224 .setStyle("DANGER"),
225 new MessageButton()
Skyler Grey11236ba2022-08-08 21:13:33 +0100226 .setLabel(lastClicked == "clear-channel" ? "Click again to confirm" : "Clear Channel")
Skyler Grey75ea9172022-08-06 10:22:23 +0100227 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
228 .setCustomId("clear-channel")
229 .setDisabled(!config.welcome.channel)
230 .setStyle("DANGER"),
231 new MessageButton()
232 .setLabel("Set Channel to DM")
233 .setCustomId("set-channel-dm")
234 .setDisabled(config.welcome.channel == "dm")
235 .setStyle("SECONDARY")
236 ])
237 ]
238 })) as Message;
Skyler Grey1a67e182022-08-04 23:05:44 +0100239 let i: MessageComponentInteraction;
pineafan41d93562022-07-30 22:10:15 +0100240 try {
241 i = await m.awaitMessageComponent({ time: 300000 });
242 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100243 timedOut = true;
244 continue;
pineafan41d93562022-07-30 22:10:15 +0100245 }
pineafan63fc5e22022-08-04 22:04:10 +0100246 i.deferUpdate();
pineafan41d93562022-07-30 22:10:15 +0100247 if (i.customId == "clear-message") {
248 if (lastClicked == "clear-message") {
pineafan4e425942022-08-08 22:01:47 +0100249 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +0100250 "welcome.message": null
251 });
pineafan63fc5e22022-08-04 22:04:10 +0100252 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100253 } else {
254 lastClicked = "clear-message";
255 }
pineafan41d93562022-07-30 22:10:15 +0100256 } else if (i.customId == "clear-role") {
257 if (lastClicked == "clear-role") {
pineafan4e425942022-08-08 22:01:47 +0100258 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +0100259 "welcome.role": null
260 });
pineafan63fc5e22022-08-04 22:04:10 +0100261 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100262 } else {
263 lastClicked = "clear-role";
264 }
pineafan41d93562022-07-30 22:10:15 +0100265 } else if (i.customId == "clear-ping") {
266 if (lastClicked == "clear-ping") {
pineafan4e425942022-08-08 22:01:47 +0100267 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +0100268 "welcome.ping": null
269 });
pineafan63fc5e22022-08-04 22:04:10 +0100270 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100271 } else {
272 lastClicked = "clear-ping";
273 }
pineafan41d93562022-07-30 22:10:15 +0100274 } else if (i.customId == "clear-channel") {
275 if (lastClicked == "clear-channel") {
pineafan4e425942022-08-08 22:01:47 +0100276 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +0100277 "welcome.channel": null
278 });
pineafan63fc5e22022-08-04 22:04:10 +0100279 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100280 } else {
281 lastClicked = "clear-channel";
282 }
pineafan41d93562022-07-30 22:10:15 +0100283 } else if (i.customId == "set-channel-dm") {
pineafan4e425942022-08-08 22:01:47 +0100284 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +0100285 "welcome.channel": "dm"
286 });
pineafan63fc5e22022-08-04 22:04:10 +0100287 lastClicked = null;
pineafan41d93562022-07-30 22:10:15 +0100288 }
Skyler Greyad002172022-08-16 18:48:26 +0100289 } while (!timedOut);
Skyler Grey75ea9172022-08-06 10:22:23 +0100290 await interaction.editReply({
Skyler Greyad002172022-08-16 18:48:26 +0100291 embeds: [m.embeds[0]!.setFooter({ text: "Message timed out" })],
Skyler Grey75ea9172022-08-06 10:22:23 +0100292 components: []
293 });
pineafan63fc5e22022-08-04 22:04:10 +0100294};
pineafan41d93562022-07-30 22:10:15 +0100295
Skyler Grey1a67e182022-08-04 23:05:44 +0100296const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100297 const member = interaction.member as Discord.GuildMember;
298 if (!member.permissions.has("MANAGE_GUILD"))
Skyler Grey11236ba2022-08-08 21:13:33 +0100299 throw new Error("You must have the *Manage Server* permission to use this command");
pineafan41d93562022-07-30 22:10:15 +0100300 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100301};
pineafan41d93562022-07-30 22:10:15 +0100302
303export { command };
304export { callback };
Skyler Grey1a67e182022-08-04 23:05:44 +0100305export { check };