add more commands

This commit is contained in:
ShuriZma 2024-06-06 21:21:28 +02:00
parent 227e56c4fc
commit f2d39cce0f
Signed by: ShuriZma
GPG Key ID: 8D289758EE9B8074
7 changed files with 258 additions and 8 deletions

13
package-lock.json generated
View File

@ -1,14 +1,15 @@
{ {
"name": "nickskeeeebot", "name": "beaverFacts",
"version": "1.0.0", "version": "1.0.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "nickskeeeebot", "name": "beaverFacts",
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@devraelfreeze/discordjs-pagination": "^2.7.6",
"discord.js": "^14.14.1", "discord.js": "^14.14.1",
"mysql2": "^3.10.0", "mysql2": "^3.10.0",
"ts-node": "^10.9.2" "ts-node": "^10.9.2"
@ -29,6 +30,14 @@
"node": ">=12" "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": { "node_modules/@discordjs/builders": {
"version": "1.7.0", "version": "1.7.0",
"resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-1.7.0.tgz", "resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-1.7.0.tgz",

View File

@ -10,6 +10,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@devraelfreeze/discordjs-pagination": "^2.7.6",
"discord.js": "^14.14.1", "discord.js": "^14.14.1",
"mysql2": "^3.10.0", "mysql2": "^3.10.0",
"ts-node": "^10.9.2" "ts-node": "^10.9.2"

View File

@ -14,7 +14,12 @@ import {
} from 'discord.js'; } from 'discord.js';
import * as sql from 'mysql2'; import * as sql from 'mysql2';
import config from "./config"; 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 { export class Bot {
private connection: sql.Connection; private connection: sql.Connection;
@ -26,7 +31,11 @@ export class Bot {
this.registerCommands(); this.registerCommands();
const commands: Collection<any, any> = new Collection(); const commands: Collection<any, any> = 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 => { client.once(Events.ClientReady, readyClient => {
@ -51,9 +60,15 @@ export class Bot {
} catch (error) { } catch (error) {
console.error(error); console.error(error);
if (interaction.replied || interaction.deferred) { 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 { } 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() { private registerCommands() {
const commands = []; const commands = [];
commands.push( 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, 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() { public getConnection() {
@ -109,4 +128,82 @@ export class Bot {
return Bot.instance; return Bot.instance;
} }
public async sendEmbed(msg: Message<boolean> | 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');
}
} }

41
src/commands/addFact.ts Normal file
View File

@ -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,
});
},
};

View File

@ -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,
});
},
};

17
src/commands/facts.ts Normal file
View File

@ -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);
},
};

View File

@ -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,
});
},
};