From f2d39cce0f447a0e9e46778111f80cfc289587b2 Mon Sep 17 00:00:00 2001 From: ShuriZma Date: Thu, 6 Jun 2024 21:21:28 +0200 Subject: [PATCH] add more commands --- package-lock.json | 13 ++++- package.json | 1 + src/bot.ts | 109 +++++++++++++++++++++++++++++++++++-- src/commands/addFact.ts | 41 ++++++++++++++ src/commands/enableFact.ts | 35 ++++++++++++ src/commands/facts.ts | 17 ++++++ src/commands/removeFact.ts | 50 +++++++++++++++++ 7 files changed, 258 insertions(+), 8 deletions(-) create mode 100644 src/commands/addFact.ts create mode 100644 src/commands/enableFact.ts create mode 100644 src/commands/facts.ts create mode 100644 src/commands/removeFact.ts diff --git a/package-lock.json b/package-lock.json index 09bfa00..aeee32d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,14 +1,15 @@ { - "name": "nickskeeeebot", + "name": "beaverFacts", "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "nickskeeeebot", + "name": "beaverFacts", "version": "1.0.0", "license": "ISC", "dependencies": { + "@devraelfreeze/discordjs-pagination": "^2.7.6", "discord.js": "^14.14.1", "mysql2": "^3.10.0", "ts-node": "^10.9.2" @@ -29,6 +30,14 @@ "node": ">=12" } }, + "node_modules/@devraelfreeze/discordjs-pagination": { + "version": "2.7.6", + "resolved": "https://registry.npmjs.org/@devraelfreeze/discordjs-pagination/-/discordjs-pagination-2.7.6.tgz", + "integrity": "sha512-mK0iMCWsk9CJC58AyvUGGxHzBZTJlxaXaboxSvcA85Tg3d4X9rerZj7Q6L8boeTNh5mbfil8IoLYicvz7gai/w==", + "dependencies": { + "discord.js": "^14.9.0" + } + }, "node_modules/@discordjs/builders": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-1.7.0.tgz", diff --git a/package.json b/package.json index bd4f689..f45f340 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { + "@devraelfreeze/discordjs-pagination": "^2.7.6", "discord.js": "^14.14.1", "mysql2": "^3.10.0", "ts-node": "^10.9.2" diff --git a/src/bot.ts b/src/bot.ts index 207a671..dc27752 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -14,7 +14,12 @@ import { } from 'discord.js'; import * as sql from 'mysql2'; import config from "./config"; -import fact from "./commands/fact"; +import Fact from "./commands/fact"; +import AddFact from "./commands/addFact"; +import RemoveFact from "./commands/removeFact"; +import EnableFact from "./commands/enableFact"; +import Facts from "./commands/facts"; +import {pagination} from "@devraelfreeze/discordjs-pagination"; export class Bot { private connection: sql.Connection; @@ -26,7 +31,11 @@ export class Bot { this.registerCommands(); const commands: Collection = new Collection(); - commands.set(fact.data.name, fact); + commands.set(Fact.data.name, Fact); + commands.set(AddFact.data.name, AddFact); + commands.set(RemoveFact.data.name, RemoveFact); + commands.set(EnableFact.data.name, EnableFact); + commands.set(Facts.data.name, Facts); client.once(Events.ClientReady, readyClient => { @@ -51,9 +60,15 @@ export class Bot { } catch (error) { console.error(error); if (interaction.replied || interaction.deferred) { - await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); + await interaction.followUp({ + content: 'There was an error while executing this command!', + ephemeral: true + }); } else { - await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + await interaction.reply({ + content: 'There was an error while executing this command!', + ephemeral: true + }); } } }); @@ -64,7 +79,11 @@ export class Bot { private registerCommands() { const commands = []; commands.push( - fact.data.toJSON(), + Fact.data.toJSON(), + AddFact.data.toJSON(), + RemoveFact.data.toJSON(), + EnableFact.data.toJSON(), + Facts.data.toJSON(), ); @@ -95,7 +114,7 @@ export class Bot { port: config.dbPort, }) - this.connection.query('CREATE TABLE IF NOT EXISTS facts (id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, content TEXT NOT NULL, active TINYINT(1) NOT NULL DEFAULT 1)'); + this.connection.query('CREATE TABLE if NOT EXISTS facts (id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, content TEXT NOT NULL, active TINYINT(1) NOT NULL DEFAULT 1)'); } public getConnection() { @@ -109,4 +128,82 @@ export class Bot { return Bot.instance; } + + public async sendEmbed(msg: Message | ChatInputCommandInteraction) { + const [results] = await this.connection.promise().execute('SELECT * FROM facts;'); + let fields = []; + let embeds = []; + for (let index in results) { + let result = results[index]; + + fields.push({ + name: 'id', + value: result.id.toString(), + inline: true, + }); + + fields.push({ + name: 'content', + value: result.content, + inline: true, + }); + + fields.push({ + name: 'active', + value: result.active ? 'True' : 'False', + inline: true, + }); + + if (fields.length < 27) { + continue; + } + + fields.push({ + name: '⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤', + value: '\u200B', + }); + + const embed = new EmbedBuilder() + .setTitle('Fact Sheet') + .setDescription('This is a list of all facts, their ids and their current status.\u200B⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤') + .setColor(0xffe600) + .setFields(fields); + + embeds.push(embed); + fields = []; + } + + if (fields.length > 0) { + fields.push({ + name: '⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤', + value: '\u200B', + }); + + const embed = new EmbedBuilder() + .setTitle('Fact Sheet') + .setDescription('This is a list of all facts, their ids and their current status.\u200B⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤') + .setColor(0xffe600) + .setFields(fields); + + embeds.push(embed); + } + + const user = msg.member.user; + if (!(user instanceof User)) { + console.log('devrael fucked up', user); + return; + } + + await pagination({ + embeds: embeds, + author: user, + interaction: msg instanceof Message ? null : msg, + message: msg instanceof Message ? msg : null, + fastSkip: true, + ephemeral: true, + pageTravel: true, + }); + + console.log('sendEmbed: sent fact sheet embed'); + } } \ No newline at end of file diff --git a/src/commands/addFact.ts b/src/commands/addFact.ts new file mode 100644 index 0000000..6a0305a --- /dev/null +++ b/src/commands/addFact.ts @@ -0,0 +1,41 @@ +import { + ChatInputCommandInteraction, + PermissionsBitField, + SlashCommandBooleanOption, + SlashCommandBuilder, + SlashCommandStringOption, +} from 'discord.js'; +import {Bot} from "../bot"; + +const factOption = (new SlashCommandStringOption()) + .setName('fact') + .setDescription('The fact.') + .setRequired(true); + +const activeOption = (new SlashCommandBooleanOption()) + .setName('active') + .setDescription('Should the fact be used?') + .setRequired(false); + +export default { + data: new SlashCommandBuilder() + .setName('addfact') + .setDescription('Add a logtastic fact.') + .addStringOption(factOption) + .addBooleanOption(activeOption) + .setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator), + execute: async function (interaction: ChatInputCommandInteraction) { + const bot = Bot.getInstance(); + await bot.getConnection().promise().execute( + 'INSERT INTO facts SET content = ?, active = ?;', + [interaction.options.data.at(0).value, interaction.options.data.at(1)?.value ?? true] + ); + + console.log('Fact has been saved!'); + + return await interaction.reply({ + content: 'Done!', + ephemeral: true, + }); + }, +}; \ No newline at end of file diff --git a/src/commands/enableFact.ts b/src/commands/enableFact.ts new file mode 100644 index 0000000..627c6c7 --- /dev/null +++ b/src/commands/enableFact.ts @@ -0,0 +1,35 @@ +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 enabled.') + .setRequired(true); + +export default { + data: new SlashCommandBuilder() + .setName('enablefact') + .setDescription('Enable a logtastic fact.') + .addIntegerOption(idOption) + .setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator), + execute: async function (interaction: ChatInputCommandInteraction) { + const bot = Bot.getInstance(); + await bot.getConnection().promise().execute( + 'UPDATE facts SET active = 1 WHERE id = ?;', + [interaction.options.data.at(0).value] + ); + + console.log('Fact has been enabled!'); + + return await interaction.reply({ + content: 'Done!', + ephemeral: true, + }); + }, +}; \ No newline at end of file diff --git a/src/commands/facts.ts b/src/commands/facts.ts new file mode 100644 index 0000000..92faed1 --- /dev/null +++ b/src/commands/facts.ts @@ -0,0 +1,17 @@ +import { + ChatInputCommandInteraction, + PermissionsBitField, + SlashCommandBuilder, +} from 'discord.js'; +import {Bot} from "../bot"; + +export default { + data: new SlashCommandBuilder() + .setName('facts') + .setDescription('Enable a logtastic fact.') + .setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator), + execute: async function (interaction: ChatInputCommandInteraction) { + const bot = Bot.getInstance(); + return bot.sendEmbed(interaction); + }, +}; \ No newline at end of file diff --git a/src/commands/removeFact.ts b/src/commands/removeFact.ts new file mode 100644 index 0000000..c609541 --- /dev/null +++ b/src/commands/removeFact.ts @@ -0,0 +1,50 @@ +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 removeOption = (new SlashCommandBooleanOption()) + .setName('remove') + .setDescription('Should the fact be completely deleted?') + .setRequired(false); + +export default { + data: new SlashCommandBuilder() + .setName('removefact') + .setDescription('Remove a (not so) logtastic fact.') + .addIntegerOption(idOption) + .addBooleanOption(removeOption) + .setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator), + execute: async function (interaction: ChatInputCommandInteraction) { + const bot = Bot.getInstance(); + if (interaction.options.data.at(1)?.value ?? false) { + await bot.getConnection().promise().execute( + 'DELETE FROM facts WHERE id = ?;', + [interaction.options.data.at(0).value] + ); + + console.log('Fact has been deleted!'); + } else { + await bot.getConnection().promise().execute( + 'UPDATE facts SET active = 0 WHERE id = ?;', + [interaction.options.data.at(0).value] + ); + + console.log('Fact has been disabled!'); + } + + return await interaction.reply({ + content: 'Done!', + ephemeral: true, + }); + }, +}; \ No newline at end of file