Feature/4 add data delete on guild remove #7

Merged
AstroGD merged 7 commits from feature/4-add-data-delete-on-guild-remove into dev 2022-11-25 18:31:44 +01:00
3 changed files with 70 additions and 1 deletions
Showing only changes of commit fda6166483 - Show all commits

View File

@ -1,8 +1,9 @@
import * as notification from "./notification"; import * as notification from "./notification";
import * as blocklist from "./blocklist"; import * as blocklist from "./blocklist";
import * as info from "./info"; 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 { export {
array array

View File

@ -2,6 +2,7 @@ import { ChatInputCommandInteraction, Collection, Events, SlashCommandBuilder }
import * as notification from "./notification"; import * as notification from "./notification";
import * as blocklist from "./blocklist"; import * as blocklist from "./blocklist";
import * as info from "./info"; import * as info from "./info";
import * as preserveSettings from "./preserveSettings";
import client from "../client"; import client from "../client";
import getDefaultEmbed from "../tools/defaultEmbeds"; 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(notification.builder.name, notification);
commands.set(blocklist.builder.name, blocklist); commands.set(blocklist.builder.name, blocklist);
commands.set(info.builder.name, info); commands.set(info.builder.name, info);
commands.set(preserveSettings.builder.name, preserveSettings);
client.on(Events.InteractionCreate, async (interaction) => { client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return; 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
}