blob: 00b18265834adfbc12427cd53b6d8a22bb0a33f2 [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 Grey75ea9172022-08-06 10:22:23 +010022 .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 );
pineafan41d93562022-07-30 22:10:15 +010051
Skyler Greyc634e2b2022-08-06 17:50:48 +010052const callback = async (interaction: CommandInteraction): Promise<unknown> => {
Skyler Grey75ea9172022-08-06 10:22:23 +010053 const { renderRole, renderChannel, log, NucleusColors, entry, renderUser } =
54 client.logger;
55 await interaction.reply({
56 embeds: LoadingEmbed,
57 fetchReply: true,
58 ephemeral: true
59 });
Skyler Grey1a67e182022-08-04 23:05:44 +010060 let m: Message;
Skyler Grey75ea9172022-08-06 10:22:23 +010061 if (
62 interaction.options.getRole("role") ||
63 interaction.options.getChannel("channel") ||
64 interaction.options.getString("message")
65 ) {
Skyler Grey1a67e182022-08-04 23:05:44 +010066 let role: Role;
67 let ping: Role;
pineafan63fc5e22022-08-04 22:04:10 +010068 const message = interaction.options.getString("message");
pineafan41d93562022-07-30 22:10:15 +010069 try {
Skyler Grey1a67e182022-08-04 23:05:44 +010070 role = interaction.options.getRole("role") as Role;
71 ping = interaction.options.getRole("ping") as Role;
pineafan41d93562022-07-30 22:10:15 +010072 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010073 return await interaction.editReply({
74 embeds: [
75 new EmojiEmbed()
76 .setEmoji("GUILD.ROLES.DELETE")
77 .setTitle("Welcome Events")
78 .setDescription(
79 "The role you provided is not a valid role"
80 )
81 .setStatus("Danger")
82 ]
83 });
pineafan41d93562022-07-30 22:10:15 +010084 }
Skyler Grey1a67e182022-08-04 23:05:44 +010085 let channel: Channel;
pineafan41d93562022-07-30 22:10:15 +010086 try {
Skyler Grey1a67e182022-08-04 23:05:44 +010087 channel = interaction.options.getChannel("channel") as Channel;
pineafan41d93562022-07-30 22:10:15 +010088 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010089 return await interaction.editReply({
90 embeds: [
91 new EmojiEmbed()
92 .setEmoji("GUILD.ROLES.DELETE")
93 .setTitle("Welcome Events")
94 .setDescription(
95 "The channel you provided is not a valid channel"
96 )
97 .setStatus("Danger")
98 ]
99 });
pineafan41d93562022-07-30 22:10:15 +0100100 }
pineafan63fc5e22022-08-04 22:04:10 +0100101 role = role as Discord.Role;
102 ping = ping as Discord.Role;
103 channel = channel as Discord.TextChannel;
104 const options = {};
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 if (role) options.role = renderRole(role);
106 if (ping) options.ping = renderRole(ping);
107 if (channel) options.channel = renderChannel(channel);
108 if (message) options.message = "\n> " + message;
pineafan63fc5e22022-08-04 22:04:10 +0100109 const confirmation = await new confirmationMessage(interaction)
pineafan41d93562022-07-30 22:10:15 +0100110 .setEmoji("GUILD.ROLES.EDIT")
111 .setTitle("Welcome Events")
112 .setDescription(generateKeyValueList(options))
113 .setColor("Warning")
114 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +0100115 .send(true);
116 if (confirmation.cancelled) return;
pineafan41d93562022-07-30 22:10:15 +0100117 if (confirmation.success) {
118 try {
pineafan63fc5e22022-08-04 22:04:10 +0100119 const toChange = {};
120 if (role) toChange["welcome.role"] = role.id;
121 if (ping) toChange["welcome.ping"] = ping.id;
122 if (channel) toChange["welcome.channel"] = channel.id;
123 if (message) toChange["welcome.message"] = message;
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 await client.database.guilds.write(
125 interaction.guild.id,
126 toChange
127 );
pineafan63fc5e22022-08-04 22:04:10 +0100128 const list = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 memberId: entry(
130 interaction.user.id,
131 `\`${interaction.user.id}\``
132 ),
133 changedBy: entry(
134 interaction.user.id,
135 renderUser(interaction.user)
136 )
pineafan63fc5e22022-08-04 22:04:10 +0100137 };
Skyler Grey75ea9172022-08-06 10:22:23 +0100138 if (role) list.role = entry(role.id, renderRole(role));
139 if (ping) list.ping = entry(ping.id, renderRole(ping));
140 if (channel)
141 list.channel = entry(channel.id, renderChannel(channel.id));
142 if (message) list.message = entry(message, `\`${message}\``);
pineafan63fc5e22022-08-04 22:04:10 +0100143 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100145 type: "welcomeSettingsUpdated",
146 displayName: "Welcome Settings Changed",
147 calculateType: "nucleusSettingsUpdated",
148 color: NucleusColors.green,
149 emoji: "CONTROL.BLOCKTICK",
150 timestamp: new Date().getTime()
151 },
152 list: list,
153 hidden: {
154 guild: interaction.guild.id
pineafan41d93562022-07-30 22:10:15 +0100155 }
pineafan63fc5e22022-08-04 22:04:10 +0100156 };
157 log(data);
pineafan41d93562022-07-30 22:10:15 +0100158 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100159 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +0100160 return interaction.editReply({
161 embeds: [
162 new EmojiEmbed()
163 .setTitle("Welcome Events")
164 .setDescription(
165 "Something went wrong while updating welcome settings"
166 )
167 .setStatus("Danger")
168 .setEmoji("GUILD.ROLES.DELETE")
169 ],
170 components: []
171 });
pineafan41d93562022-07-30 22:10:15 +0100172 }
173 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100174 return interaction.editReply({
175 embeds: [
176 new EmojiEmbed()
177 .setTitle("Welcome Events")
178 .setDescription("No changes were made")
179 .setStatus("Success")
180 .setEmoji("GUILD.ROLES.CREATE")
181 ],
182 components: []
183 });
pineafan41d93562022-07-30 22:10:15 +0100184 }
185 }
pineafan63fc5e22022-08-04 22:04:10 +0100186 let lastClicked = null;
pineafan41d93562022-07-30 22:10:15 +0100187 while (true) {
pineafan63fc5e22022-08-04 22:04:10 +0100188 const config = await client.database.guilds.read(interaction.guild.id);
Skyler Grey75ea9172022-08-06 10:22:23 +0100189 m = (await interaction.editReply({
190 embeds: [
191 new EmojiEmbed()
192 .setTitle("Welcome Events")
193 .setDescription(
194 `**Message:** ${
195 config.welcome.message
196 ? `\n> ${config.welcome.message}`
197 : "*None set*"
198 }\n` +
199 `**Role:** ${
200 config.welcome.role
201 ? renderRole(
202 await interaction.guild.roles.fetch(
203 config.welcome.role
204 )
205 )
206 : "*None set*"
207 }\n` +
208 `**Ping:** ${
209 config.welcome.ping
210 ? renderRole(
211 await interaction.guild.roles.fetch(
212 config.welcome.ping
213 )
214 )
215 : "*None set*"
216 }\n` +
217 `**Channel:** ${
218 config.welcome.channel
219 ? config.welcome.channel == "dm"
220 ? "DM"
221 : renderChannel(
222 await interaction.guild.channels.fetch(
223 config.welcome.channel
224 )
225 )
226 : "*None set*"
227 }`
228 )
229 .setStatus("Success")
230 .setEmoji("CHANNEL.TEXT.CREATE")
231 ],
232 components: [
233 new MessageActionRow().addComponents([
234 new MessageButton()
235 .setLabel(
236 lastClicked == "clear-message"
237 ? "Click again to confirm"
238 : "Clear Message"
239 )
240 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
241 .setCustomId("clear-message")
242 .setDisabled(!config.welcome.message)
243 .setStyle("DANGER"),
244 new MessageButton()
245 .setLabel(
246 lastClicked == "clear-role"
247 ? "Click again to confirm"
248 : "Clear Role"
249 )
250 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
251 .setCustomId("clear-role")
252 .setDisabled(!config.welcome.role)
253 .setStyle("DANGER"),
254 new MessageButton()
255 .setLabel(
256 lastClicked == "clear-ping"
257 ? "Click again to confirm"
258 : "Clear Ping"
259 )
260 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
261 .setCustomId("clear-ping")
262 .setDisabled(!config.welcome.ping)
263 .setStyle("DANGER"),
264 new MessageButton()
265 .setLabel(
266 lastClicked == "clear-channel"
267 ? "Click again to confirm"
268 : "Clear Channel"
269 )
270 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
271 .setCustomId("clear-channel")
272 .setDisabled(!config.welcome.channel)
273 .setStyle("DANGER"),
274 new MessageButton()
275 .setLabel("Set Channel to DM")
276 .setCustomId("set-channel-dm")
277 .setDisabled(config.welcome.channel == "dm")
278 .setStyle("SECONDARY")
279 ])
280 ]
281 })) as Message;
Skyler Grey1a67e182022-08-04 23:05:44 +0100282 let i: MessageComponentInteraction;
pineafan41d93562022-07-30 22:10:15 +0100283 try {
284 i = await m.awaitMessageComponent({ time: 300000 });
285 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100286 break;
pineafan41d93562022-07-30 22:10:15 +0100287 }
pineafan63fc5e22022-08-04 22:04:10 +0100288 i.deferUpdate();
pineafan41d93562022-07-30 22:10:15 +0100289 if (i.customId == "clear-message") {
290 if (lastClicked == "clear-message") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100291 await client.database.guilds.write(interaction.guild.id, {
292 "welcome.message": null
293 });
pineafan63fc5e22022-08-04 22:04:10 +0100294 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100295 } else {
296 lastClicked = "clear-message";
297 }
pineafan41d93562022-07-30 22:10:15 +0100298 } else if (i.customId == "clear-role") {
299 if (lastClicked == "clear-role") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100300 await client.database.guilds.write(interaction.guild.id, {
301 "welcome.role": null
302 });
pineafan63fc5e22022-08-04 22:04:10 +0100303 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100304 } else {
305 lastClicked = "clear-role";
306 }
pineafan41d93562022-07-30 22:10:15 +0100307 } else if (i.customId == "clear-ping") {
308 if (lastClicked == "clear-ping") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100309 await client.database.guilds.write(interaction.guild.id, {
310 "welcome.ping": null
311 });
pineafan63fc5e22022-08-04 22:04:10 +0100312 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100313 } else {
314 lastClicked = "clear-ping";
315 }
pineafan41d93562022-07-30 22:10:15 +0100316 } else if (i.customId == "clear-channel") {
317 if (lastClicked == "clear-channel") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100318 await client.database.guilds.write(interaction.guild.id, {
319 "welcome.channel": null
320 });
pineafan63fc5e22022-08-04 22:04:10 +0100321 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100322 } else {
323 lastClicked = "clear-channel";
324 }
pineafan41d93562022-07-30 22:10:15 +0100325 } else if (i.customId == "set-channel-dm") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100326 await client.database.guilds.write(interaction.guild.id, {
327 "welcome.channel": "dm"
328 });
pineafan63fc5e22022-08-04 22:04:10 +0100329 lastClicked = null;
pineafan41d93562022-07-30 22:10:15 +0100330 }
331 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100332 await interaction.editReply({
333 embeds: [m.embeds[0].setFooter({ text: "Message closed" })],
334 components: []
335 });
pineafan63fc5e22022-08-04 22:04:10 +0100336};
pineafan41d93562022-07-30 22:10:15 +0100337
Skyler Grey1a67e182022-08-04 23:05:44 +0100338const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100339 const member = interaction.member as Discord.GuildMember;
340 if (!member.permissions.has("MANAGE_GUILD"))
Skyler Greyc634e2b2022-08-06 17:50:48 +0100341 throw new Error(
342 "You must have the *Manage Server* permission to use this command"
343 );
pineafan41d93562022-07-30 22:10:15 +0100344 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100345};
pineafan41d93562022-07-30 22:10:15 +0100346
347export { command };
348export { callback };
Skyler Grey1a67e182022-08-04 23:05:44 +0100349export { check };