blob: 3eb0ec56ea56c0f343f388201af0e3d483fdbd17 [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";
pineafan41d93562022-07-30 22:10:15 +010011import { 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";
16import { ChannelType } from "discord-api-types";
17import 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 Grey75ea9172022-08-06 10:22:23 +010052const callback = async (
53 interaction: CommandInteraction
54): Promise<void | unknown> => {
55 const { renderRole, renderChannel, log, NucleusColors, entry, renderUser } =
56 client.logger;
57 await interaction.reply({
58 embeds: LoadingEmbed,
59 fetchReply: true,
60 ephemeral: true
61 });
Skyler Grey1a67e182022-08-04 23:05:44 +010062 let m: Message;
Skyler Grey75ea9172022-08-06 10:22:23 +010063 if (
64 interaction.options.getRole("role") ||
65 interaction.options.getChannel("channel") ||
66 interaction.options.getString("message")
67 ) {
Skyler Grey1a67e182022-08-04 23:05:44 +010068 let role: Role;
69 let ping: Role;
pineafan63fc5e22022-08-04 22:04:10 +010070 const message = interaction.options.getString("message");
pineafan41d93562022-07-30 22:10:15 +010071 try {
Skyler Grey1a67e182022-08-04 23:05:44 +010072 role = interaction.options.getRole("role") as Role;
73 ping = interaction.options.getRole("ping") as Role;
pineafan41d93562022-07-30 22:10:15 +010074 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010075 return await interaction.editReply({
76 embeds: [
77 new EmojiEmbed()
78 .setEmoji("GUILD.ROLES.DELETE")
79 .setTitle("Welcome Events")
80 .setDescription(
81 "The role you provided is not a valid role"
82 )
83 .setStatus("Danger")
84 ]
85 });
pineafan41d93562022-07-30 22:10:15 +010086 }
Skyler Grey1a67e182022-08-04 23:05:44 +010087 let channel: Channel;
pineafan41d93562022-07-30 22:10:15 +010088 try {
Skyler Grey1a67e182022-08-04 23:05:44 +010089 channel = interaction.options.getChannel("channel") as Channel;
pineafan41d93562022-07-30 22:10:15 +010090 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010091 return await interaction.editReply({
92 embeds: [
93 new EmojiEmbed()
94 .setEmoji("GUILD.ROLES.DELETE")
95 .setTitle("Welcome Events")
96 .setDescription(
97 "The channel you provided is not a valid channel"
98 )
99 .setStatus("Danger")
100 ]
101 });
pineafan41d93562022-07-30 22:10:15 +0100102 }
pineafan63fc5e22022-08-04 22:04:10 +0100103 role = role as Discord.Role;
104 ping = ping as Discord.Role;
105 channel = channel as Discord.TextChannel;
106 const options = {};
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 if (role) options.role = renderRole(role);
108 if (ping) options.ping = renderRole(ping);
109 if (channel) options.channel = renderChannel(channel);
110 if (message) options.message = "\n> " + message;
pineafan63fc5e22022-08-04 22:04:10 +0100111 const confirmation = await new confirmationMessage(interaction)
pineafan41d93562022-07-30 22:10:15 +0100112 .setEmoji("GUILD.ROLES.EDIT")
113 .setTitle("Welcome Events")
114 .setDescription(generateKeyValueList(options))
115 .setColor("Warning")
116 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +0100117 .send(true);
118 if (confirmation.cancelled) return;
pineafan41d93562022-07-30 22:10:15 +0100119 if (confirmation.success) {
120 try {
pineafan63fc5e22022-08-04 22:04:10 +0100121 const toChange = {};
122 if (role) toChange["welcome.role"] = role.id;
123 if (ping) toChange["welcome.ping"] = ping.id;
124 if (channel) toChange["welcome.channel"] = channel.id;
125 if (message) toChange["welcome.message"] = message;
Skyler Grey75ea9172022-08-06 10:22:23 +0100126 await client.database.guilds.write(
127 interaction.guild.id,
128 toChange
129 );
pineafan63fc5e22022-08-04 22:04:10 +0100130 const list = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 memberId: entry(
132 interaction.user.id,
133 `\`${interaction.user.id}\``
134 ),
135 changedBy: entry(
136 interaction.user.id,
137 renderUser(interaction.user)
138 )
pineafan63fc5e22022-08-04 22:04:10 +0100139 };
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 if (role) list.role = entry(role.id, renderRole(role));
141 if (ping) list.ping = entry(ping.id, renderRole(ping));
142 if (channel)
143 list.channel = entry(channel.id, renderChannel(channel.id));
144 if (message) list.message = entry(message, `\`${message}\``);
pineafan63fc5e22022-08-04 22:04:10 +0100145 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100147 type: "welcomeSettingsUpdated",
148 displayName: "Welcome Settings Changed",
149 calculateType: "nucleusSettingsUpdated",
150 color: NucleusColors.green,
151 emoji: "CONTROL.BLOCKTICK",
152 timestamp: new Date().getTime()
153 },
154 list: list,
155 hidden: {
156 guild: interaction.guild.id
pineafan41d93562022-07-30 22:10:15 +0100157 }
pineafan63fc5e22022-08-04 22:04:10 +0100158 };
159 log(data);
pineafan41d93562022-07-30 22:10:15 +0100160 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100161 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +0100162 return interaction.editReply({
163 embeds: [
164 new EmojiEmbed()
165 .setTitle("Welcome Events")
166 .setDescription(
167 "Something went wrong while updating welcome settings"
168 )
169 .setStatus("Danger")
170 .setEmoji("GUILD.ROLES.DELETE")
171 ],
172 components: []
173 });
pineafan41d93562022-07-30 22:10:15 +0100174 }
175 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100176 return interaction.editReply({
177 embeds: [
178 new EmojiEmbed()
179 .setTitle("Welcome Events")
180 .setDescription("No changes were made")
181 .setStatus("Success")
182 .setEmoji("GUILD.ROLES.CREATE")
183 ],
184 components: []
185 });
pineafan41d93562022-07-30 22:10:15 +0100186 }
187 }
pineafan63fc5e22022-08-04 22:04:10 +0100188 let lastClicked = null;
pineafan41d93562022-07-30 22:10:15 +0100189 while (true) {
pineafan63fc5e22022-08-04 22:04:10 +0100190 const config = await client.database.guilds.read(interaction.guild.id);
Skyler Grey75ea9172022-08-06 10:22:23 +0100191 m = (await interaction.editReply({
192 embeds: [
193 new EmojiEmbed()
194 .setTitle("Welcome Events")
195 .setDescription(
196 `**Message:** ${
197 config.welcome.message
198 ? `\n> ${config.welcome.message}`
199 : "*None set*"
200 }\n` +
201 `**Role:** ${
202 config.welcome.role
203 ? renderRole(
204 await interaction.guild.roles.fetch(
205 config.welcome.role
206 )
207 )
208 : "*None set*"
209 }\n` +
210 `**Ping:** ${
211 config.welcome.ping
212 ? renderRole(
213 await interaction.guild.roles.fetch(
214 config.welcome.ping
215 )
216 )
217 : "*None set*"
218 }\n` +
219 `**Channel:** ${
220 config.welcome.channel
221 ? config.welcome.channel == "dm"
222 ? "DM"
223 : renderChannel(
224 await interaction.guild.channels.fetch(
225 config.welcome.channel
226 )
227 )
228 : "*None set*"
229 }`
230 )
231 .setStatus("Success")
232 .setEmoji("CHANNEL.TEXT.CREATE")
233 ],
234 components: [
235 new MessageActionRow().addComponents([
236 new MessageButton()
237 .setLabel(
238 lastClicked == "clear-message"
239 ? "Click again to confirm"
240 : "Clear Message"
241 )
242 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
243 .setCustomId("clear-message")
244 .setDisabled(!config.welcome.message)
245 .setStyle("DANGER"),
246 new MessageButton()
247 .setLabel(
248 lastClicked == "clear-role"
249 ? "Click again to confirm"
250 : "Clear Role"
251 )
252 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
253 .setCustomId("clear-role")
254 .setDisabled(!config.welcome.role)
255 .setStyle("DANGER"),
256 new MessageButton()
257 .setLabel(
258 lastClicked == "clear-ping"
259 ? "Click again to confirm"
260 : "Clear Ping"
261 )
262 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
263 .setCustomId("clear-ping")
264 .setDisabled(!config.welcome.ping)
265 .setStyle("DANGER"),
266 new MessageButton()
267 .setLabel(
268 lastClicked == "clear-channel"
269 ? "Click again to confirm"
270 : "Clear Channel"
271 )
272 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
273 .setCustomId("clear-channel")
274 .setDisabled(!config.welcome.channel)
275 .setStyle("DANGER"),
276 new MessageButton()
277 .setLabel("Set Channel to DM")
278 .setCustomId("set-channel-dm")
279 .setDisabled(config.welcome.channel == "dm")
280 .setStyle("SECONDARY")
281 ])
282 ]
283 })) as Message;
Skyler Grey1a67e182022-08-04 23:05:44 +0100284 let i: MessageComponentInteraction;
pineafan41d93562022-07-30 22:10:15 +0100285 try {
286 i = await m.awaitMessageComponent({ time: 300000 });
287 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100288 break;
pineafan41d93562022-07-30 22:10:15 +0100289 }
pineafan63fc5e22022-08-04 22:04:10 +0100290 i.deferUpdate();
pineafan41d93562022-07-30 22:10:15 +0100291 if (i.customId == "clear-message") {
292 if (lastClicked == "clear-message") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100293 await client.database.guilds.write(interaction.guild.id, {
294 "welcome.message": null
295 });
pineafan63fc5e22022-08-04 22:04:10 +0100296 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100297 } else {
298 lastClicked = "clear-message";
299 }
pineafan41d93562022-07-30 22:10:15 +0100300 } else if (i.customId == "clear-role") {
301 if (lastClicked == "clear-role") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100302 await client.database.guilds.write(interaction.guild.id, {
303 "welcome.role": null
304 });
pineafan63fc5e22022-08-04 22:04:10 +0100305 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100306 } else {
307 lastClicked = "clear-role";
308 }
pineafan41d93562022-07-30 22:10:15 +0100309 } else if (i.customId == "clear-ping") {
310 if (lastClicked == "clear-ping") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100311 await client.database.guilds.write(interaction.guild.id, {
312 "welcome.ping": null
313 });
pineafan63fc5e22022-08-04 22:04:10 +0100314 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100315 } else {
316 lastClicked = "clear-ping";
317 }
pineafan41d93562022-07-30 22:10:15 +0100318 } else if (i.customId == "clear-channel") {
319 if (lastClicked == "clear-channel") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100320 await client.database.guilds.write(interaction.guild.id, {
321 "welcome.channel": null
322 });
pineafan63fc5e22022-08-04 22:04:10 +0100323 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100324 } else {
325 lastClicked = "clear-channel";
326 }
pineafan41d93562022-07-30 22:10:15 +0100327 } else if (i.customId == "set-channel-dm") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100328 await client.database.guilds.write(interaction.guild.id, {
329 "welcome.channel": "dm"
330 });
pineafan63fc5e22022-08-04 22:04:10 +0100331 lastClicked = null;
pineafan41d93562022-07-30 22:10:15 +0100332 }
333 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100334 await interaction.editReply({
335 embeds: [m.embeds[0].setFooter({ text: "Message closed" })],
336 components: []
337 });
pineafan63fc5e22022-08-04 22:04:10 +0100338};
pineafan41d93562022-07-30 22:10:15 +0100339
Skyler Grey1a67e182022-08-04 23:05:44 +0100340const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100341 const member = interaction.member as Discord.GuildMember;
342 if (!member.permissions.has("MANAGE_GUILD"))
343 throw "You must have the *Manage Server* permission to use this command";
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 };