/preservesettings command

This commit is contained in:
Lukas | AstroGD 2022-11-25 17:44:21 +01:00
parent df2548dba6
commit fda6166483
Signed by: AstroGD
GPG Key ID: 82A5E6C236C535AA
3 changed files with 70 additions and 1 deletions

View File

@ -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

View File

@ -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<string, { builder: SlashCommandBuilder, execute:
commands.set(notification.builder.name, notification);
commands.set(blocklist.builder.name, blocklist);
commands.set(info.builder.name, info);
commands.set(preserveSettings.builder.name, preserveSettings);
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;

View File

@ -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<void> {
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
}