blob: 34ac292ce980477ccc5a3e978e80966f1db7f6c1 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
pineafan41d93562022-07-30 22:10:15 +01002import Discord, { CommandInteraction, MessageActionRow, MessageButton, MessageSelectMenu } from "discord.js";
3import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
4import { WrappedCheck } from "jshaiku";
5import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +01006import client from "../../utils/client.js";
7import confirmationMessage from "../../utils/confirmationMessage.js";
8import generateKeyValueList from "../../utils/generateKeyValueList.js";
9import { ChannelType } from "discord-api-types";
10import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan41d93562022-07-30 22:10:15 +010011
12const command = (builder: SlashCommandSubcommandBuilder) =>
13 builder
pineafan63fc5e22022-08-04 22:04:10 +010014 .setName("welcome")
15 .setDescription("Messages and roles sent or given when someone joins the server")
16 .addStringOption(option => option.setName("message").setDescription("The message to send when someone joins the server").setAutocomplete(true))
17 .addRoleOption(option => option.setName("role").setDescription("The role given when someone joins the server"))
18 .addRoleOption(option => option.setName("ping").setDescription("The role pinged when someone joins the server"))
19 .addChannelOption(option => option.setName("channel").setDescription("The channel the welcome message should be sent to").addChannelTypes([
20 ChannelType.GuildText, ChannelType.GuildNews
21 ]));
pineafan41d93562022-07-30 22:10:15 +010022
23const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafan63fc5e22022-08-04 22:04:10 +010024 const { renderRole, renderChannel, log, NucleusColors, entry, renderUser } = client.logger;
pineafan41d93562022-07-30 22:10:15 +010025 await interaction.reply({embeds: LoadingEmbed, fetchReply: true, ephemeral: true});
26 let m;
27 if (interaction.options.getRole("role") || interaction.options.getChannel("channel") || interaction.options.getString("message")) {
28 let role;
29 let ping;
pineafan63fc5e22022-08-04 22:04:10 +010030 const message = interaction.options.getString("message");
pineafan41d93562022-07-30 22:10:15 +010031 try {
pineafan63fc5e22022-08-04 22:04:10 +010032 role = interaction.options.getRole("role");
33 ping = interaction.options.getRole("ping");
pineafan41d93562022-07-30 22:10:15 +010034 } catch {
35 return await interaction.editReply({embeds: [new EmojiEmbed()
36 .setEmoji("GUILD.ROLES.DELETE")
37 .setTitle("Welcome Events")
38 .setDescription("The role you provided is not a valid role")
39 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010040 ]});
pineafan41d93562022-07-30 22:10:15 +010041 }
42 let channel;
43 try {
pineafan63fc5e22022-08-04 22:04:10 +010044 channel = interaction.options.getChannel("channel");
pineafan41d93562022-07-30 22:10:15 +010045 } catch {
46 return await interaction.editReply({embeds: [new EmojiEmbed()
47 .setEmoji("GUILD.ROLES.DELETE")
48 .setTitle("Welcome Events")
49 .setDescription("The channel you provided is not a valid channel")
50 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010051 ]});
pineafan41d93562022-07-30 22:10:15 +010052 }
pineafan63fc5e22022-08-04 22:04:10 +010053 role = role as Discord.Role;
54 ping = ping as Discord.Role;
55 channel = channel as Discord.TextChannel;
56 const options = {};
57 if (role) options["role"] = renderRole(role);
58 if (ping) options["ping"] = renderRole(ping);
59 if (channel) options["channel"] = renderChannel(channel);
60 if (message) options["message"] = "\n> " + message;
61 const confirmation = await new confirmationMessage(interaction)
pineafan41d93562022-07-30 22:10:15 +010062 .setEmoji("GUILD.ROLES.EDIT")
63 .setTitle("Welcome Events")
64 .setDescription(generateKeyValueList(options))
65 .setColor("Warning")
66 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010067 .send(true);
68 if (confirmation.cancelled) return;
pineafan41d93562022-07-30 22:10:15 +010069 if (confirmation.success) {
70 try {
pineafan63fc5e22022-08-04 22:04:10 +010071 const toChange = {};
72 if (role) toChange["welcome.role"] = role.id;
73 if (ping) toChange["welcome.ping"] = ping.id;
74 if (channel) toChange["welcome.channel"] = channel.id;
75 if (message) toChange["welcome.message"] = message;
pineafan41d93562022-07-30 22:10:15 +010076 await client.database.guilds.write(interaction.guild.id, toChange);
pineafan63fc5e22022-08-04 22:04:10 +010077 const list = {
pineafan41d93562022-07-30 22:10:15 +010078 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
79 changedBy: entry(interaction.user.id, renderUser(interaction.user))
pineafan63fc5e22022-08-04 22:04:10 +010080 };
81 if (role) list["role"] = entry(role.id, renderRole(role));
82 if (ping) list["ping"] = entry(ping.id, renderRole(ping));
83 if (channel) list["channel"] = entry(channel.id, renderChannel(channel.id));
84 if (message) list["message"] = entry(message, `\`${message}\``);
85 const data = {
86 meta:{
87 type: "welcomeSettingsUpdated",
88 displayName: "Welcome Settings Changed",
89 calculateType: "nucleusSettingsUpdated",
90 color: NucleusColors.green,
91 emoji: "CONTROL.BLOCKTICK",
92 timestamp: new Date().getTime()
93 },
94 list: list,
95 hidden: {
96 guild: interaction.guild.id
pineafan41d93562022-07-30 22:10:15 +010097 }
pineafan63fc5e22022-08-04 22:04:10 +010098 };
99 log(data);
pineafan41d93562022-07-30 22:10:15 +0100100 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100101 console.log(e);
pineafan41d93562022-07-30 22:10:15 +0100102 return interaction.editReply({embeds: [new EmojiEmbed()
103 .setTitle("Welcome Events")
pineafan63fc5e22022-08-04 22:04:10 +0100104 .setDescription("Something went wrong while updating welcome settings")
pineafan41d93562022-07-30 22:10:15 +0100105 .setStatus("Danger")
106 .setEmoji("GUILD.ROLES.DELETE")
107 ], components: []});
108 }
109 } else {
110 return interaction.editReply({embeds: [new EmojiEmbed()
111 .setTitle("Welcome Events")
pineafan63fc5e22022-08-04 22:04:10 +0100112 .setDescription("No changes were made")
pineafan41d93562022-07-30 22:10:15 +0100113 .setStatus("Success")
114 .setEmoji("GUILD.ROLES.CREATE")
115 ], components: []});
116 }
117 }
pineafan63fc5e22022-08-04 22:04:10 +0100118 let lastClicked = null;
pineafan41d93562022-07-30 22:10:15 +0100119 while (true) {
pineafan63fc5e22022-08-04 22:04:10 +0100120 const config = await client.database.guilds.read(interaction.guild.id);
pineafan41d93562022-07-30 22:10:15 +0100121 m = await interaction.editReply({embeds: [new EmojiEmbed()
122 .setTitle("Welcome Events")
123 .setDescription(
124 `**Message:** ${config.welcome.message ? `\n> ${config.welcome.message}` : "*None set*"}\n` +
125 `**Role:** ${config.welcome.role ? renderRole(await interaction.guild.roles.fetch(config.welcome.role)) : "*None set*"}\n` +
126 `**Ping:** ${config.welcome.ping ? renderRole(await interaction.guild.roles.fetch(config.welcome.ping)) : "*None set*"}\n` +
127 `**Channel:** ${config.welcome.channel ? (config.welcome.channel == "dm" ? "DM" : renderChannel(await interaction.guild.channels.fetch(config.welcome.channel))) : "*None set*"}`
128 )
129 .setStatus("Success")
130 .setEmoji("CHANNEL.TEXT.CREATE")
131 ], components: [
132 new MessageActionRow().addComponents([
133 new MessageButton()
134 .setLabel(lastClicked == "clear-message" ? "Click again to confirm" : "Clear Message")
135 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
136 .setCustomId("clear-message")
137 .setDisabled(!config.welcome.message)
138 .setStyle("DANGER"),
139 new MessageButton()
140 .setLabel(lastClicked == "clear-role" ? "Click again to confirm" : "Clear Role")
141 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
142 .setCustomId("clear-role")
143 .setDisabled(!config.welcome.role)
144 .setStyle("DANGER"),
145 new MessageButton()
146 .setLabel(lastClicked == "clear-ping" ? "Click again to confirm" : "Clear Ping")
147 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
148 .setCustomId("clear-ping")
149 .setDisabled(!config.welcome.ping)
150 .setStyle("DANGER"),
151 new MessageButton()
152 .setLabel(lastClicked == "clear-channel" ? "Click again to confirm" : "Clear Channel")
153 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
154 .setCustomId("clear-channel")
155 .setDisabled(!config.welcome.channel)
156 .setStyle("DANGER"),
157 new MessageButton()
158 .setLabel("Set Channel to DM")
159 .setCustomId("set-channel-dm")
160 .setDisabled(config.welcome.channel == "dm")
161 .setStyle("SECONDARY")
162 ])
pineafan63fc5e22022-08-04 22:04:10 +0100163 ]});
pineafan41d93562022-07-30 22:10:15 +0100164 let i;
165 try {
166 i = await m.awaitMessageComponent({ time: 300000 });
167 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100168 break;
pineafan41d93562022-07-30 22:10:15 +0100169 }
pineafan63fc5e22022-08-04 22:04:10 +0100170 i.deferUpdate();
pineafan41d93562022-07-30 22:10:15 +0100171 if (i.customId == "clear-message") {
172 if (lastClicked == "clear-message") {
173 await client.database.guilds.write(interaction.guild.id, {"welcome.message": null});
pineafan63fc5e22022-08-04 22:04:10 +0100174 lastClicked = null;
175 } else { lastClicked = "clear-message"; }
pineafan41d93562022-07-30 22:10:15 +0100176 } else if (i.customId == "clear-role") {
177 if (lastClicked == "clear-role") {
178 await client.database.guilds.write(interaction.guild.id, {"welcome.role": null});
pineafan63fc5e22022-08-04 22:04:10 +0100179 lastClicked = null;
180 } else { lastClicked = "clear-role"; }
pineafan41d93562022-07-30 22:10:15 +0100181 } else if (i.customId == "clear-ping") {
182 if (lastClicked == "clear-ping") {
183 await client.database.guilds.write(interaction.guild.id, {"welcome.ping": null});
pineafan63fc5e22022-08-04 22:04:10 +0100184 lastClicked = null;
185 } else { lastClicked = "clear-ping"; }
pineafan41d93562022-07-30 22:10:15 +0100186 } else if (i.customId == "clear-channel") {
187 if (lastClicked == "clear-channel") {
188 await client.database.guilds.write(interaction.guild.id, {"welcome.channel": null});
pineafan63fc5e22022-08-04 22:04:10 +0100189 lastClicked = null;
190 } else { lastClicked = "clear-channel"; }
pineafan41d93562022-07-30 22:10:15 +0100191 } else if (i.customId == "set-channel-dm") {
192 await client.database.guilds.write(interaction.guild.id, {"welcome.channel": "dm"});
pineafan63fc5e22022-08-04 22:04:10 +0100193 lastClicked = null;
pineafan41d93562022-07-30 22:10:15 +0100194 }
195 }
196 await interaction.editReply({embeds: [m.embeds[0].setFooter({text: "Message closed"})], components: []});
pineafan63fc5e22022-08-04 22:04:10 +0100197};
pineafan41d93562022-07-30 22:10:15 +0100198
199const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100200 const member = (interaction.member as Discord.GuildMember);
201 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command";
pineafan41d93562022-07-30 22:10:15 +0100202 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100203};
pineafan41d93562022-07-30 22:10:15 +0100204
205export { command };
206export { callback };
207export { check };