blob: f22f409be71dad07f2c32ed5652a850ff024c58e [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
Skyler Grey1a67e182022-08-04 23:05:44 +01002import Discord, { Channel, CommandInteraction, Message, MessageActionRow, MessageButton, MessageComponentInteraction, Role } from "discord.js";
pineafan41d93562022-07-30 22:10:15 +01003import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan41d93562022-07-30 22:10:15 +01004import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +01005import client from "../../utils/client.js";
6import confirmationMessage from "../../utils/confirmationMessage.js";
7import generateKeyValueList from "../../utils/generateKeyValueList.js";
8import { ChannelType } from "discord-api-types";
9import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan41d93562022-07-30 22:10:15 +010010
11const command = (builder: SlashCommandSubcommandBuilder) =>
12 builder
pineafan63fc5e22022-08-04 22:04:10 +010013 .setName("welcome")
14 .setDescription("Messages and roles sent or given when someone joins the server")
15 .addStringOption(option => option.setName("message").setDescription("The message to send when someone joins the server").setAutocomplete(true))
16 .addRoleOption(option => option.setName("role").setDescription("The role given when someone joins the server"))
17 .addRoleOption(option => option.setName("ping").setDescription("The role pinged when someone joins the server"))
18 .addChannelOption(option => option.setName("channel").setDescription("The channel the welcome message should be sent to").addChannelTypes([
19 ChannelType.GuildText, ChannelType.GuildNews
20 ]));
pineafan41d93562022-07-30 22:10:15 +010021
Skyler Grey1a67e182022-08-04 23:05:44 +010022const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010023 const { renderRole, renderChannel, log, NucleusColors, entry, renderUser } = client.logger;
pineafan41d93562022-07-30 22:10:15 +010024 await interaction.reply({embeds: LoadingEmbed, fetchReply: true, ephemeral: true});
Skyler Grey1a67e182022-08-04 23:05:44 +010025 let m: Message;
pineafan41d93562022-07-30 22:10:15 +010026 if (interaction.options.getRole("role") || interaction.options.getChannel("channel") || interaction.options.getString("message")) {
Skyler Grey1a67e182022-08-04 23:05:44 +010027 let role: Role;
28 let ping: Role;
pineafan63fc5e22022-08-04 22:04:10 +010029 const message = interaction.options.getString("message");
pineafan41d93562022-07-30 22:10:15 +010030 try {
Skyler Grey1a67e182022-08-04 23:05:44 +010031 role = interaction.options.getRole("role") as Role;
32 ping = interaction.options.getRole("ping") as Role;
pineafan41d93562022-07-30 22:10:15 +010033 } catch {
34 return await interaction.editReply({embeds: [new EmojiEmbed()
35 .setEmoji("GUILD.ROLES.DELETE")
36 .setTitle("Welcome Events")
37 .setDescription("The role you provided is not a valid role")
38 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010039 ]});
pineafan41d93562022-07-30 22:10:15 +010040 }
Skyler Grey1a67e182022-08-04 23:05:44 +010041 let channel: Channel;
pineafan41d93562022-07-30 22:10:15 +010042 try {
Skyler Grey1a67e182022-08-04 23:05:44 +010043 channel = interaction.options.getChannel("channel") as Channel;
pineafan41d93562022-07-30 22:10:15 +010044 } catch {
45 return await interaction.editReply({embeds: [new EmojiEmbed()
46 .setEmoji("GUILD.ROLES.DELETE")
47 .setTitle("Welcome Events")
48 .setDescription("The channel you provided is not a valid channel")
49 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010050 ]});
pineafan41d93562022-07-30 22:10:15 +010051 }
pineafan63fc5e22022-08-04 22:04:10 +010052 role = role as Discord.Role;
53 ping = ping as Discord.Role;
54 channel = channel as Discord.TextChannel;
55 const options = {};
56 if (role) options["role"] = renderRole(role);
57 if (ping) options["ping"] = renderRole(ping);
58 if (channel) options["channel"] = renderChannel(channel);
59 if (message) options["message"] = "\n> " + message;
60 const confirmation = await new confirmationMessage(interaction)
pineafan41d93562022-07-30 22:10:15 +010061 .setEmoji("GUILD.ROLES.EDIT")
62 .setTitle("Welcome Events")
63 .setDescription(generateKeyValueList(options))
64 .setColor("Warning")
65 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010066 .send(true);
67 if (confirmation.cancelled) return;
pineafan41d93562022-07-30 22:10:15 +010068 if (confirmation.success) {
69 try {
pineafan63fc5e22022-08-04 22:04:10 +010070 const toChange = {};
71 if (role) toChange["welcome.role"] = role.id;
72 if (ping) toChange["welcome.ping"] = ping.id;
73 if (channel) toChange["welcome.channel"] = channel.id;
74 if (message) toChange["welcome.message"] = message;
pineafan41d93562022-07-30 22:10:15 +010075 await client.database.guilds.write(interaction.guild.id, toChange);
pineafan63fc5e22022-08-04 22:04:10 +010076 const list = {
pineafan41d93562022-07-30 22:10:15 +010077 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
78 changedBy: entry(interaction.user.id, renderUser(interaction.user))
pineafan63fc5e22022-08-04 22:04:10 +010079 };
80 if (role) list["role"] = entry(role.id, renderRole(role));
81 if (ping) list["ping"] = entry(ping.id, renderRole(ping));
82 if (channel) list["channel"] = entry(channel.id, renderChannel(channel.id));
83 if (message) list["message"] = entry(message, `\`${message}\``);
84 const data = {
85 meta:{
86 type: "welcomeSettingsUpdated",
87 displayName: "Welcome Settings Changed",
88 calculateType: "nucleusSettingsUpdated",
89 color: NucleusColors.green,
90 emoji: "CONTROL.BLOCKTICK",
91 timestamp: new Date().getTime()
92 },
93 list: list,
94 hidden: {
95 guild: interaction.guild.id
pineafan41d93562022-07-30 22:10:15 +010096 }
pineafan63fc5e22022-08-04 22:04:10 +010097 };
98 log(data);
pineafan41d93562022-07-30 22:10:15 +010099 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100100 console.log(e);
pineafan41d93562022-07-30 22:10:15 +0100101 return interaction.editReply({embeds: [new EmojiEmbed()
102 .setTitle("Welcome Events")
pineafan63fc5e22022-08-04 22:04:10 +0100103 .setDescription("Something went wrong while updating welcome settings")
pineafan41d93562022-07-30 22:10:15 +0100104 .setStatus("Danger")
105 .setEmoji("GUILD.ROLES.DELETE")
106 ], components: []});
107 }
108 } else {
109 return interaction.editReply({embeds: [new EmojiEmbed()
110 .setTitle("Welcome Events")
pineafan63fc5e22022-08-04 22:04:10 +0100111 .setDescription("No changes were made")
pineafan41d93562022-07-30 22:10:15 +0100112 .setStatus("Success")
113 .setEmoji("GUILD.ROLES.CREATE")
114 ], components: []});
115 }
116 }
pineafan63fc5e22022-08-04 22:04:10 +0100117 let lastClicked = null;
pineafan41d93562022-07-30 22:10:15 +0100118 while (true) {
pineafan63fc5e22022-08-04 22:04:10 +0100119 const config = await client.database.guilds.read(interaction.guild.id);
pineafan41d93562022-07-30 22:10:15 +0100120 m = await interaction.editReply({embeds: [new EmojiEmbed()
121 .setTitle("Welcome Events")
122 .setDescription(
123 `**Message:** ${config.welcome.message ? `\n> ${config.welcome.message}` : "*None set*"}\n` +
124 `**Role:** ${config.welcome.role ? renderRole(await interaction.guild.roles.fetch(config.welcome.role)) : "*None set*"}\n` +
125 `**Ping:** ${config.welcome.ping ? renderRole(await interaction.guild.roles.fetch(config.welcome.ping)) : "*None set*"}\n` +
126 `**Channel:** ${config.welcome.channel ? (config.welcome.channel == "dm" ? "DM" : renderChannel(await interaction.guild.channels.fetch(config.welcome.channel))) : "*None set*"}`
127 )
128 .setStatus("Success")
129 .setEmoji("CHANNEL.TEXT.CREATE")
130 ], components: [
131 new MessageActionRow().addComponents([
132 new MessageButton()
133 .setLabel(lastClicked == "clear-message" ? "Click again to confirm" : "Clear Message")
134 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
135 .setCustomId("clear-message")
136 .setDisabled(!config.welcome.message)
137 .setStyle("DANGER"),
138 new MessageButton()
139 .setLabel(lastClicked == "clear-role" ? "Click again to confirm" : "Clear Role")
140 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
141 .setCustomId("clear-role")
142 .setDisabled(!config.welcome.role)
143 .setStyle("DANGER"),
144 new MessageButton()
145 .setLabel(lastClicked == "clear-ping" ? "Click again to confirm" : "Clear Ping")
146 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
147 .setCustomId("clear-ping")
148 .setDisabled(!config.welcome.ping)
149 .setStyle("DANGER"),
150 new MessageButton()
151 .setLabel(lastClicked == "clear-channel" ? "Click again to confirm" : "Clear Channel")
152 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
153 .setCustomId("clear-channel")
154 .setDisabled(!config.welcome.channel)
155 .setStyle("DANGER"),
156 new MessageButton()
157 .setLabel("Set Channel to DM")
158 .setCustomId("set-channel-dm")
159 .setDisabled(config.welcome.channel == "dm")
160 .setStyle("SECONDARY")
161 ])
Skyler Grey1a67e182022-08-04 23:05:44 +0100162 ]}) as Message;
163 let i: MessageComponentInteraction;
pineafan41d93562022-07-30 22:10:15 +0100164 try {
165 i = await m.awaitMessageComponent({ time: 300000 });
166 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100167 break;
pineafan41d93562022-07-30 22:10:15 +0100168 }
pineafan63fc5e22022-08-04 22:04:10 +0100169 i.deferUpdate();
pineafan41d93562022-07-30 22:10:15 +0100170 if (i.customId == "clear-message") {
171 if (lastClicked == "clear-message") {
172 await client.database.guilds.write(interaction.guild.id, {"welcome.message": null});
pineafan63fc5e22022-08-04 22:04:10 +0100173 lastClicked = null;
174 } else { lastClicked = "clear-message"; }
pineafan41d93562022-07-30 22:10:15 +0100175 } else if (i.customId == "clear-role") {
176 if (lastClicked == "clear-role") {
177 await client.database.guilds.write(interaction.guild.id, {"welcome.role": null});
pineafan63fc5e22022-08-04 22:04:10 +0100178 lastClicked = null;
179 } else { lastClicked = "clear-role"; }
pineafan41d93562022-07-30 22:10:15 +0100180 } else if (i.customId == "clear-ping") {
181 if (lastClicked == "clear-ping") {
182 await client.database.guilds.write(interaction.guild.id, {"welcome.ping": null});
pineafan63fc5e22022-08-04 22:04:10 +0100183 lastClicked = null;
184 } else { lastClicked = "clear-ping"; }
pineafan41d93562022-07-30 22:10:15 +0100185 } else if (i.customId == "clear-channel") {
186 if (lastClicked == "clear-channel") {
187 await client.database.guilds.write(interaction.guild.id, {"welcome.channel": null});
pineafan63fc5e22022-08-04 22:04:10 +0100188 lastClicked = null;
189 } else { lastClicked = "clear-channel"; }
pineafan41d93562022-07-30 22:10:15 +0100190 } else if (i.customId == "set-channel-dm") {
191 await client.database.guilds.write(interaction.guild.id, {"welcome.channel": "dm"});
pineafan63fc5e22022-08-04 22:04:10 +0100192 lastClicked = null;
pineafan41d93562022-07-30 22:10:15 +0100193 }
194 }
195 await interaction.editReply({embeds: [m.embeds[0].setFooter({text: "Message closed"})], components: []});
pineafan63fc5e22022-08-04 22:04:10 +0100196};
pineafan41d93562022-07-30 22:10:15 +0100197
Skyler Grey1a67e182022-08-04 23:05:44 +0100198const check = (interaction: CommandInteraction) => {
pineafan63fc5e22022-08-04 22:04:10 +0100199 const member = (interaction.member as Discord.GuildMember);
200 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command";
pineafan41d93562022-07-30 22:10:15 +0100201 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100202};
pineafan41d93562022-07-30 22:10:15 +0100203
204export { command };
205export { callback };
Skyler Grey1a67e182022-08-04 23:05:44 +0100206export { check };