From 3d7666d7a08c21d73ac4715aeb141b6bf7d40d37 Mon Sep 17 00:00:00 2001 From: Francisco Pessano Date: Sat, 12 Nov 2022 00:36:56 -0300 Subject: [PATCH] ping command done --- app.js | 45 +++++++++++++++++++++++++++++++++++++++++---- commands/ping.js | 10 ++++++++++ deploy-commands.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 commands/ping.js create mode 100644 deploy-commands.js diff --git a/app.js b/app.js index 117af43..7130234 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,49 @@ -const { GatewayIntentBits } = require('discord.js'); -const Discord = require('discord.js'); +const fs = require('fs'); +const path = require('path'); +const { Collection, Client, GatewayIntentBits, Events } = require('discord.js'); require('dotenv').config(); -const client = new Discord.Client({ intents: [GatewayIntentBits.Guilds] }); +const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.on('ready', () => { console.log('working'); console.log(client.user); }); -client.login(process.env.BOT_TOKEN); \ No newline at end of file +client.login(process.env.BOT_TOKEN); + +client.commands = new Collection(); + +const commandsPath = path.join(__dirname, 'commands'); +const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); + +for (const file of commandFiles) { + const filePath = path.join(commandsPath, file); + const command = require(filePath); + if ('data' in command && 'execute' in command) { + client.commands.set(command.data.name, command); + } + else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + } +} + +client.on(Events.InteractionCreate, async interaction => { + if (!interaction.isChatInputCommand()) return; + console.log(interaction); + 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); + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + } +}); + diff --git a/commands/ping.js b/commands/ping.js new file mode 100644 index 0000000..bbcd95e --- /dev/null +++ b/commands/ping.js @@ -0,0 +1,10 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('ping') + .setDescription('Replies with Pong!'), + async execute(interaction) { + await interaction.reply('pong'); + }, +}; \ No newline at end of file diff --git a/deploy-commands.js b/deploy-commands.js new file mode 100644 index 0000000..e720396 --- /dev/null +++ b/deploy-commands.js @@ -0,0 +1,30 @@ +const { REST, Routes } = require('discord.js'); +const fs = require('node:fs'); +require('dotenv').config(); + +const commands = []; +const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); + +for (const file of commandFiles) { + const command = require(`./commands/${file}`); + commands.push(command.data.toJSON()); +} + +const rest = new REST({ version: '10' }).setToken(process.env.BOT_TOKEN); + +(async () => { + try { + console.log(`Started refreshing ${commands.length} application (/) commands.`); + + const data = await rest.put( + process.env.NODE_ENV === 'production' ? Routes.applicationCommands(process.env.DISCORD_CLIENT_ID) : + Routes.applicationGuildCommands(process.env.DISCORD_CLIENT_ID, process.env.TEST_DISCORD_SERVER_ID), + { body: commands }, + ); + + console.log(`Successfully reloaded ${data.length} application (/) commands.`); + } + catch (error) { + console.error(error); + } +})(); \ No newline at end of file