diff --git a/src/commands/ci.ts b/src/commands/ci.ts index 4372998..0c6db03 100644 --- a/src/commands/ci.ts +++ b/src/commands/ci.ts @@ -1,8 +1,9 @@ import * as notification from "./notification"; import * as blocklist from "./blocklist"; import * as info from "./info"; +import * as preserveSettings from "./preserveSettings"; -const array = [notification.builder.toJSON(), blocklist.builder.toJSON(), info.builder.toJSON()]; +const array = [notification.builder.toJSON(), blocklist.builder.toJSON(), info.builder.toJSON(), preserveSettings.builder.toJSON()]; export { array diff --git a/src/commands/index.ts b/src/commands/index.ts index ce8064a..589dcfd 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -2,6 +2,7 @@ import { ChatInputCommandInteraction, Collection, Events, SlashCommandBuilder } import * as notification from "./notification"; import * as blocklist from "./blocklist"; import * as info from "./info"; +import * as preserveSettings from "./preserveSettings"; import client from "../client"; import getDefaultEmbed from "../tools/defaultEmbeds"; @@ -9,6 +10,7 @@ const commands = new Collection { if (!interaction.isChatInputCommand()) return; diff --git a/src/commands/preserveSettings.ts b/src/commands/preserveSettings.ts new file mode 100644 index 0000000..c8cc94a --- /dev/null +++ b/src/commands/preserveSettings.ts @@ -0,0 +1,66 @@ +import { SlashCommandBuilder, ChatInputCommandInteraction, PermissionFlagsBits } from "discord.js"; +import { database, GuildSetting } from "../data"; +import { getGuildSetting } from "../tools/data"; +import getDefaultEmbed, { getSuccessEmbed } from "../tools/defaultEmbeds"; +import { Color, Emoji } from "../tools/design"; +import { getGuildChannel } from "../tools/discord"; + +const builder = new SlashCommandBuilder(); +builder.setName("preservesettings"); +builder.setDescription("Sets if the bot should save the server settings and blocklist if it leaves the server or delete it"); +builder.addStringOption((option) => { + option.addChoices({ + name: "Keep data when bot leaves the server", + value: "keep" + }, { + name: "Delete data when bot leaves the server", + value: "delete" + }); + option.setName("behaviour"); + option.setDescription("How the bot behaves when leaving the server"); + option.setRequired(true); + return option; +}); +builder.setDMPermission(false); +builder.setDefaultMemberPermissions(PermissionFlagsBits.Administrator); + +async function execute(interaction: ChatInputCommandInteraction): Promise { + if (!interaction.inGuild()) throw new Error("Command was executed outside guild context"); + + const settings = await getGuildSetting(interaction.guildId); + const option = interaction.options.getString("behaviour", true).toLowerCase(); + + if (option !== "keep" && option !== "delete") throw new TypeError(`option "behaviour" expected to be of type "keep" | "delete" but was "${option}"`); + + settings.preserveDataOnGuildLeave = option === "keep"; + + await database.getRepository(GuildSetting).save(settings); + + const embed = getSuccessEmbed(); + embed.setDescription(`Preserve settings on server leave is now ${settings.preserveDataOnGuildLeave ? "ENABLED" : "DISABLED"}`); + + interaction.reply({ + embeds: [embed], + ephemeral: true + }).catch(); + + if (!settings.notificationChannelID) return; + const logChannel = await getGuildChannel(interaction.guildId, settings.notificationChannelID); + if (!logChannel || !logChannel.isTextBased()) return; + const logEmbed = getDefaultEmbed(); + logEmbed.setTitle(`${Emoji.SETTINGS} Settings changed`); + logEmbed.setDescription(`Preserve settings on server leave is now ${settings.preserveDataOnGuildLeave ? "ENABLED" : "DISABLED"}`); + logEmbed.addFields({ + name: "This action was performed by", + value: `${interaction.user.tag} (${interaction.user.id})` + }); + + logChannel.send({ + embeds: [logEmbed] + }).catch(); +} + +export { + builder, + execute +} \ No newline at end of file