From 4b5caa6059a2291dd7d560a3b708d9058394f426 Mon Sep 17 00:00:00 2001 From: tresabhi Date: Tue, 18 Apr 2023 23:34:05 +0000 Subject: [PATCH] feat: commands --- config.template.json | 4 ++- src/commands/verify.js | 9 ++++++ src/index.js | 70 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/commands/verify.js diff --git a/config.template.json b/config.template.json index 6104eb00d..5b0d61035 100644 --- a/config.template.json +++ b/config.template.json @@ -1,3 +1,5 @@ { - "token": "" + "token": "", + "clientId": "", + "guildId": "" } diff --git a/src/commands/verify.js b/src/commands/verify.js new file mode 100644 index 000000000..8680abbf8 --- /dev/null +++ b/src/commands/verify.js @@ -0,0 +1,9 @@ +import { SlashCommandBuilder } from 'discord.js'; + +export const data = new SlashCommandBuilder() + .setName('verify') + .setDescription('Verifies the user with Blitz'); + +export async function execute(interaction) { + await interaction.reply('uwu i done nothin here uwu'); +} diff --git a/src/index.js b/src/index.js index a077b29ed..595ed70f0 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,13 @@ import { faker } from '@faker-js/faker'; -import { Client, EmbedBuilder, GatewayIntentBits } from 'discord.js'; +import { + Client, + Collection, + EmbedBuilder, + GatewayIntentBits, + REST, + Routes, +} from 'discord.js'; +import { readdirSync } from 'fs'; import config from '../config.json' assert { type: 'json' }; const VERIFY_CHANNEL = '1097677884266643527'; @@ -9,7 +17,9 @@ const client = new Client({ }); client.on('error', console.error); -client.on('ready', (c) => console.log(`Logged in as ${client.user.tag}!`)); +client.on('ready', (c) => { + console.log(`Logged in as ${c.user.tag}!`); +}); client.on('guildMemberAdd', (member) => { member.guild.channels.cache.get(VERIFY_CHANNEL).send({ @@ -26,4 +36,60 @@ client.on('guildMemberAdd', (member) => { }); }); +client.commands = new Collection(); + +client.on('interactionCreate', async (interaction) => { + if (!interaction.isChatInputCommand()) return; + + const command = interaction.client.commands.get(interaction.commandName); + + if (!command) { + console.error(`No command matching ${interaction.commandName} was found.`); + return; + } + + try { + await command.execute(interaction); + } catch (error) { + console.error(error); + if (interaction.replied || interaction.deferred) { + 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, + }); + } + } +}); + +const commands = []; +const foldersPath = 'src/commands/'; +const commandFolders = readdirSync(foldersPath); + +for (const file of commandFolders) { + const command = await import(`./commands/${file}`); + commands.push(command.data.toJSON()); + client.commands.set(command.data.name, command); +} + +// Construct and prepare an instance of the REST module +const rest = new REST().setToken(config.token); + +try { + console.log(`Started refreshing ${commands.length} command(s).`); + + const data = await rest.put( + Routes.applicationGuildCommands(config.clientId, config.guildId), + { body: commands }, + ); + + console.log(`Successfully reloaded ${data.length} command(s).`); +} catch (error) { + console.error(error); +} + client.login(config.token);