blob: fe6a181ebab772eb9405821a93907b39de730c67 [file] [log] [blame]
import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
import Discord, { CommandInteraction, MessageActionRow, MessageButton, TextInputComponent } from "discord.js";
import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
import getEmojiByName from "../../utils/getEmojiByName.js";
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
import { WrappedCheck } from "jshaiku";
import client from "../../utils/client.js";
import { modalInteractionCollector } from "../../utils/dualCollector.js";
import confirmationMessage from "../../utils/confirmationMessage.js";
import keyValueList from "../../utils/generateKeyValueList.js";
const command = (builder: SlashCommandSubcommandBuilder) =>
builder
.setName("commands")
.setDescription("Links and text shown to a user after a moderator action is performed")
.addRoleOption((o) => o.setName("role").setDescription("The role given when a member is muted"));
const callback = async (interaction: CommandInteraction): Promise<unknown> => {
await interaction.reply({
embeds: LoadingEmbed,
ephemeral: true,
fetchReply: true
});
let m;
let clicked = "";
if (interaction.options.getRole("role")) {
const confirmation = await new confirmationMessage(interaction)
.setEmoji("GUILD.ROLES.DELETE")
.setTitle("Moderation Commands")
.setDescription(
keyValueList({
role: `<@&${interaction.options.getRole("role").id}>`
})
)
.setColor("Danger")
.send(true);
if (confirmation.cancelled)
return await interaction.editReply({
embeds: [
new EmojiEmbed()
.setTitle("Moderation Commands")
.setDescription("No changes were made")
.setStatus("Success")
.setEmoji("GUILD.ROLES.CREATE")
]
});
if (confirmation.success) {
await client.database.guilds.write(interaction.guild.id, {
["moderation.mute.role"]: interaction.options.getRole("role").id
});
}
}
while (true) {
const config = await client.database.guilds.read(interaction.guild.id);
const moderation = config.getKey("moderation");
m = await interaction.editReply({
embeds: [
new EmojiEmbed()
.setTitle("Moderation Commands")
.setEmoji("PUNISH.BAN.GREEN")
.setStatus("Success")
.setDescription(
"These links are shown below the message sent in a user's DM when they are punished.\n\n" +
"**Mute Role:** " +
(moderation.mute.role ? `<@&${moderation.mute.role}>` : "*None set*")
)
],
components: [
new MessageActionRow().addComponents([
new MessageButton()
.setLabel("Warn")
.setEmoji(getEmojiByName("PUNISH.WARN.YELLOW", "id"))
.setCustomId("warn")
.setStyle("SECONDARY"),
new MessageButton()
.setLabel("Mute")
.setEmoji(getEmojiByName("PUNISH.MUTE.YELLOW", "id"))
.setCustomId("mute")
.setStyle("SECONDARY"),
new MessageButton()
.setLabel("Nickname")
.setEmoji(getEmojiByName("PUNISH.NICKNAME.GREEN", "id"))
.setCustomId("nickname")
.setStyle("SECONDARY")
]),
new MessageActionRow().addComponents([
new MessageButton()
.setLabel("Kick")
.setEmoji(getEmojiByName("PUNISH.KICK.RED", "id"))
.setCustomId("kick")
.setStyle("SECONDARY"),
new MessageButton()
.setLabel("Softban")
.setEmoji(getEmojiByName("PUNISH.BAN.YELLOW", "id"))
.setCustomId("softban")
.setStyle("SECONDARY"),
new MessageButton()
.setLabel("Ban")
.setEmoji(getEmojiByName("PUNISH.BAN.RED", "id"))
.setCustomId("ban")
.setStyle("SECONDARY")
]),
new MessageActionRow().addComponents([
new MessageButton()
.setLabel(clicked === "clearMuteRole" ? "Click again to confirm" : "Clear mute role")
.setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
.setCustomId("clearMuteRole")
.setStyle("DANGER")
.setDisabled(!moderation.mute.role),
new MessageButton()
.setCustomId("timeout")
.setLabel("Mute timeout " + (moderation.mute.timeout ? "Enabled" : "Disabled"))
.setStyle(moderation.mute.timeout ? "SUCCESS" : "DANGER")
.setEmoji(getEmojiByName("CONTROL." + (moderation.mute.timeout ? "TICK" : "CROSS"), "id"))
])
]
});
let i;
try {
i = await m.awaitMessageComponent({ time: 300000 });
} catch (e) {
return;
}
let chosen = moderation[i.customId] ?? { text: null, url: null };
if (i.component.customId === "clearMuteRole") {
i.deferUpdate();
if (clicked === "clearMuteRole") {
await client.database.guilds.write(interaction.guild.id, {
"moderation.mute.role": null
});
} else {
clicked = "clearMuteRole";
}
continue;
} else {
clicked = "";
}
if (i.component.customId === "timeout") {
await i.deferUpdate();
await client.database.guilds.write(interaction.guild.id, {
"moderation.mute.timeout": !moderation.mute.timeout
});
continue;
} else if (i.customId) {
await i.showModal(
new Discord.Modal()
.setCustomId("modal")
.setTitle(`Options for ${i.customId}`)
.addComponents(
new MessageActionRow<TextInputComponent>().addComponents(
new TextInputComponent()
.setCustomId("name")
.setLabel("Button text")
.setMaxLength(100)
.setRequired(false)
.setStyle("SHORT")
.setValue(chosen.text ?? "")
),
new MessageActionRow<TextInputComponent>().addComponents(
new TextInputComponent()
.setCustomId("url")
.setLabel("URL - Type {id} to insert the user's ID")
.setMaxLength(2000)
.setRequired(false)
.setStyle("SHORT")
.setValue(chosen.link ?? "")
)
)
);
await interaction.editReply({
embeds: [
new EmojiEmbed()
.setTitle("Moderation Links")
.setDescription("Modal opened. If you can't see it, click back and try again.")
.setStatus("Success")
.setEmoji("GUILD.TICKET.OPEN")
],
components: [
new MessageActionRow().addComponents([
new MessageButton()
.setLabel("Back")
.setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
.setStyle("PRIMARY")
.setCustomId("back")
])
]
});
let out;
try {
out = await modalInteractionCollector(
m,
(m) => m.channel.id === interaction.channel.id,
(_) => true
);
} catch (e) {
continue;
}
if (out.fields) {
const buttonText = out.fields.getTextInputValue("name");
const buttonLink = out.fields.getTextInputValue("url").replace(/{id}/gi, "{id}");
const current = chosen;
if (current.text !== buttonText || current.link !== buttonLink) {
chosen = { text: buttonText, link: buttonLink };
await client.database.guilds.write(interaction.guild.id, {
["moderation." + i.customId]: {
text: buttonText,
link: buttonLink
}
});
}
} else {
continue;
}
}
}
};
const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
const member = interaction.member as Discord.GuildMember;
if (!member.permissions.has("MANAGE_GUILD"))
throw new Error("You must have the *Manage Server* permission to use this command");
return true;
};
export { command };
export { callback };
export { check };