blob: 188e4b982089884f5713a83963d019271f2c5dc7 [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)
pineafan41d93562022-07-30 22:10:15 +010098 .setEmoji("GUILD.ROLES.EDIT")
99 .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;
pineafan41d93562022-07-30 22:10:15 +0100175 while (true) {
pineafan4e425942022-08-08 22:01:47 +0100176 const config = await client.database.guilds.read(interaction.guild!.id);
Skyler Grey75ea9172022-08-06 10:22:23 +0100177 m = (await interaction.editReply({
178 embeds: [
179 new EmojiEmbed()
180 .setTitle("Welcome Events")
181 .setDescription(
Skyler Grey11236ba2022-08-08 21:13:33 +0100182 `**Message:** ${config.welcome.message ? `\n> ${config.welcome.message}` : "*None set*"}\n` +
Skyler Grey75ea9172022-08-06 10:22:23 +0100183 `**Role:** ${
184 config.welcome.role
pineafan4e425942022-08-08 22:01:47 +0100185 ? renderRole(await interaction.guild!.roles.fetch(config.welcome.role))
Skyler Grey75ea9172022-08-06 10:22:23 +0100186 : "*None set*"
187 }\n` +
188 `**Ping:** ${
189 config.welcome.ping
pineafan4e425942022-08-08 22:01:47 +0100190 ? renderRole(await interaction.guild!.roles.fetch(config.welcome.ping))
Skyler Grey75ea9172022-08-06 10:22:23 +0100191 : "*None set*"
192 }\n` +
193 `**Channel:** ${
194 config.welcome.channel
195 ? config.welcome.channel == "dm"
196 ? "DM"
pineafan4e425942022-08-08 22:01:47 +0100197 : renderChannel(await interaction.guild!.channels.fetch(config.welcome.channel))
Skyler Grey75ea9172022-08-06 10:22:23 +0100198 : "*None set*"
199 }`
200 )
201 .setStatus("Success")
202 .setEmoji("CHANNEL.TEXT.CREATE")
203 ],
204 components: [
205 new MessageActionRow().addComponents([
206 new MessageButton()
Skyler Grey11236ba2022-08-08 21:13:33 +0100207 .setLabel(lastClicked == "clear-message" ? "Click again to confirm" : "Clear Message")
Skyler Grey75ea9172022-08-06 10:22:23 +0100208 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
209 .setCustomId("clear-message")
210 .setDisabled(!config.welcome.message)
211 .setStyle("DANGER"),
212 new MessageButton()
Skyler Grey11236ba2022-08-08 21:13:33 +0100213 .setLabel(lastClicked == "clear-role" ? "Click again to confirm" : "Clear Role")
Skyler Grey75ea9172022-08-06 10:22:23 +0100214 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
215 .setCustomId("clear-role")
216 .setDisabled(!config.welcome.role)
217 .setStyle("DANGER"),
218 new MessageButton()
Skyler Grey11236ba2022-08-08 21:13:33 +0100219 .setLabel(lastClicked == "clear-ping" ? "Click again to confirm" : "Clear Ping")
Skyler Grey75ea9172022-08-06 10:22:23 +0100220 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
221 .setCustomId("clear-ping")
222 .setDisabled(!config.welcome.ping)
223 .setStyle("DANGER"),
224 new MessageButton()
Skyler Grey11236ba2022-08-08 21:13:33 +0100225 .setLabel(lastClicked == "clear-channel" ? "Click again to confirm" : "Clear Channel")
Skyler Grey75ea9172022-08-06 10:22:23 +0100226 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
227 .setCustomId("clear-channel")
228 .setDisabled(!config.welcome.channel)
229 .setStyle("DANGER"),
230 new MessageButton()
231 .setLabel("Set Channel to DM")
232 .setCustomId("set-channel-dm")
233 .setDisabled(config.welcome.channel == "dm")
234 .setStyle("SECONDARY")
235 ])
236 ]
237 })) as Message;
Skyler Grey1a67e182022-08-04 23:05:44 +0100238 let i: MessageComponentInteraction;
pineafan41d93562022-07-30 22:10:15 +0100239 try {
240 i = await m.awaitMessageComponent({ time: 300000 });
241 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100242 break;
pineafan41d93562022-07-30 22:10:15 +0100243 }
pineafan63fc5e22022-08-04 22:04:10 +0100244 i.deferUpdate();
pineafan41d93562022-07-30 22:10:15 +0100245 if (i.customId == "clear-message") {
246 if (lastClicked == "clear-message") {
pineafan4e425942022-08-08 22:01:47 +0100247 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +0100248 "welcome.message": null
249 });
pineafan63fc5e22022-08-04 22:04:10 +0100250 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100251 } else {
252 lastClicked = "clear-message";
253 }
pineafan41d93562022-07-30 22:10:15 +0100254 } else if (i.customId == "clear-role") {
255 if (lastClicked == "clear-role") {
pineafan4e425942022-08-08 22:01:47 +0100256 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +0100257 "welcome.role": null
258 });
pineafan63fc5e22022-08-04 22:04:10 +0100259 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100260 } else {
261 lastClicked = "clear-role";
262 }
pineafan41d93562022-07-30 22:10:15 +0100263 } else if (i.customId == "clear-ping") {
264 if (lastClicked == "clear-ping") {
pineafan4e425942022-08-08 22:01:47 +0100265 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +0100266 "welcome.ping": null
267 });
pineafan63fc5e22022-08-04 22:04:10 +0100268 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100269 } else {
270 lastClicked = "clear-ping";
271 }
pineafan41d93562022-07-30 22:10:15 +0100272 } else if (i.customId == "clear-channel") {
273 if (lastClicked == "clear-channel") {
pineafan4e425942022-08-08 22:01:47 +0100274 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +0100275 "welcome.channel": null
276 });
pineafan63fc5e22022-08-04 22:04:10 +0100277 lastClicked = null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100278 } else {
279 lastClicked = "clear-channel";
280 }
pineafan41d93562022-07-30 22:10:15 +0100281 } else if (i.customId == "set-channel-dm") {
pineafan4e425942022-08-08 22:01:47 +0100282 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +0100283 "welcome.channel": "dm"
284 });
pineafan63fc5e22022-08-04 22:04:10 +0100285 lastClicked = null;
pineafan41d93562022-07-30 22:10:15 +0100286 }
287 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100288 await interaction.editReply({
pineafan4e425942022-08-08 22:01:47 +0100289 embeds: [m.embeds[0]!.setFooter({ text: "Message closed" })],
Skyler Grey75ea9172022-08-06 10:22:23 +0100290 components: []
291 });
pineafan63fc5e22022-08-04 22:04:10 +0100292};
pineafan41d93562022-07-30 22:10:15 +0100293
Skyler Grey1a67e182022-08-04 23:05:44 +0100294const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100295 const member = interaction.member as Discord.GuildMember;
296 if (!member.permissions.has("MANAGE_GUILD"))
Skyler Grey11236ba2022-08-08 21:13:33 +0100297 throw new Error("You must have the *Manage Server* permission to use this command");
pineafan41d93562022-07-30 22:10:15 +0100298 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100299};
pineafan41d93562022-07-30 22:10:15 +0100300
301export { command };
302export { callback };
Skyler Grey1a67e182022-08-04 23:05:44 +0100303export { check };