From 0f473aecc65f967235665d3f6dd5f3ed0a401754 Mon Sep 17 00:00:00 2001 From: ShuriZma Date: Fri, 7 Jun 2024 09:07:52 +0200 Subject: [PATCH] add edit command --- src/bot.ts | 3 +++ src/commands/editFact.ts | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/commands/editFact.ts diff --git a/src/bot.ts b/src/bot.ts index bf33cb7..ece10d3 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -18,6 +18,7 @@ import Fact from "./commands/fact"; import AddFact from "./commands/addFact"; import RemoveFact from "./commands/removeFact"; import EnableFact from "./commands/enableFact"; +import EditFact from "./commands/editFact"; import Facts from "./commands/facts"; import {pagination} from "@devraelfreeze/discordjs-pagination"; @@ -35,6 +36,7 @@ export class Bot { commands.set(AddFact.data.name, AddFact); commands.set(RemoveFact.data.name, RemoveFact); commands.set(EnableFact.data.name, EnableFact); + commands.set(EditFact.data.name, EditFact); commands.set(Facts.data.name, Facts); @@ -83,6 +85,7 @@ export class Bot { AddFact.data.toJSON(), RemoveFact.data.toJSON(), EnableFact.data.toJSON(), + EditFact.data.toJSON(), Facts.data.toJSON(), ); diff --git a/src/commands/editFact.ts b/src/commands/editFact.ts new file mode 100644 index 0000000..9f385f4 --- /dev/null +++ b/src/commands/editFact.ts @@ -0,0 +1,52 @@ +import { + ChatInputCommandInteraction, + PermissionsBitField, + SlashCommandBooleanOption, + SlashCommandBuilder, SlashCommandIntegerOption, + SlashCommandStringOption, +} from 'discord.js'; +import {Bot} from "../bot"; + +const idOption = (new SlashCommandIntegerOption()) + .setName('id') + .setDescription('The id of the fact that should be removed.') + .setRequired(true); + +const factOption = (new SlashCommandStringOption()) + .setName('fact') + .setDescription('The fact.') + .setRequired(true); + +const activeOption = (new SlashCommandBooleanOption()) + .setName('active') + .setDescription('Should the fact be enabled?') + .setRequired(false); + +export default { + data: new SlashCommandBuilder() + .setName('editfact') + .setDescription('Edit a logtastic fact.') + .addIntegerOption(idOption) + .addStringOption(factOption) + .addBooleanOption(activeOption) + .setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator), + execute: async function (interaction: ChatInputCommandInteraction) { + const bot = Bot.getInstance(); + await bot.getConnection().promise().execute( + 'UPDATE facts SET content = ?, active = IF(? != NULL, ?, active) WHERE id = ?;', + [ + interaction.options.data.at(1).value, + interaction.options.data.at(2)?.value ?? null, + interaction.options.data.at(2)?.value ?? null, + interaction.options.data.at(0).value, + ] + ); + bot.endConection(); + console.log('Fact has been saved!'); + + return await interaction.reply({ + content: 'Done!', + ephemeral: true, + }); + }, +}; \ No newline at end of file