From a5196ef862ffa5f17c850337d46aed7d4dfbd437 Mon Sep 17 00:00:00 2001 From: Bradley Date: Tue, 8 Nov 2022 21:05:21 -0500 Subject: [PATCH] Continued Progress on Tutorial Following --- .gitignore | 1 + breadbot.js | 39 +++++++++++++++++++++++++++++++++++++ commands/ping.js | 10 ++++++++++ commands/server.js | 11 +++++++++++ commands/user.js | 12 ++++++++++++ deploy-commands.js | 35 +++++++++++++++++++++++++++++++++ events/interactionCreate.js | 23 ++++++++++++++++++++++ events/ready.js | 9 +++++++++ 8 files changed, 140 insertions(+) create mode 100644 commands/ping.js create mode 100644 commands/server.js create mode 100644 commands/user.js create mode 100644 deploy-commands.js create mode 100644 events/interactionCreate.js create mode 100644 events/ready.js diff --git a/.gitignore b/.gitignore index 4c49bd7..265e634 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .env +config.json diff --git a/breadbot.js b/breadbot.js index e69de29..8f00259 100644 --- a/breadbot.js +++ b/breadbot.js @@ -0,0 +1,39 @@ +const fs = require('node:fs'); +const path = require('node:path'); +const { Client, GatewayIntentBits, Collection } = require('discord.js'); +const { token } = require('./config.json'); + +const client = new Client({ intents: [GatewayIntentBits.Guilds] }); + +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.`); + } +} + +const eventsPath = path.join(__dirname, 'events'); +const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); + +for (const file of eventFiles) { + const filePath = path.join(eventsPath, file); + const event = require(filePath); + if (event.once) { + client.once(event.name, (...args) => event.execute(...args)); + } + else { + client.on(event.name, (...args) => event.execute(...args)); + } +} + +client.login(token); \ No newline at end of file diff --git a/commands/ping.js b/commands/ping.js new file mode 100644 index 0000000..d77b4b0 --- /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/commands/server.js b/commands/server.js new file mode 100644 index 0000000..5053708 --- /dev/null +++ b/commands/server.js @@ -0,0 +1,11 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('server') + .setDescription('Provides information about the server.'), + async execute(interaction) { + // interaction.guild is the object representing the Guild in which the command was run + await interaction.reply(`This server is ${interaction.guild.name} and has ${interaction.guild.memberCount} members.`); + }, +}; \ No newline at end of file diff --git a/commands/user.js b/commands/user.js new file mode 100644 index 0000000..2b47b16 --- /dev/null +++ b/commands/user.js @@ -0,0 +1,12 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('user') + .setDescription('Provides information about the user.'), + async execute(interaction) { + // interaction.user is the object representing the User who ran the command + // interaction.member is the GuildMember object, which represents the user in the specific guild + await interaction.reply(`This command was run by ${interaction.user.username}, who joined on ${interaction.member.joinedAt}.`); + }, +}; \ No newline at end of file diff --git a/deploy-commands.js b/deploy-commands.js new file mode 100644 index 0000000..940f1ea --- /dev/null +++ b/deploy-commands.js @@ -0,0 +1,35 @@ +const { REST, Routes } = require('discord.js'); +const { clientId, guildId, token } = require('./config.json'); +const fs = require('node:fs'); + +const commands = []; +// Grab all the command files from the commands directory you created earlier +const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); + +// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment +for (const file of commandFiles) { + const command = require(`./commands/${file}`); + commands.push(command.data.toJSON()); +} + +// Construct and prepare an instance of the REST module +const rest = new REST({ version: '10' }).setToken(token); + +// and deploy your commands! +(async () => { + try { + console.log(`Started refreshing ${commands.length} application (/) commands.`); + + // The put method is used to fully refresh all commands in the guild with the current set + const data = await rest.put( + Routes.applicationGuildCommands(clientId, guildId), + { body: commands }, + ); + + console.log(`Successfully reloaded ${data.length} application (/) commands.`); + } + catch (error) { + // And of course, make sure you catch and log any errors! + console.error(error); + } +})(); \ No newline at end of file diff --git a/events/interactionCreate.js b/events/interactionCreate.js new file mode 100644 index 0000000..b5504ce --- /dev/null +++ b/events/interactionCreate.js @@ -0,0 +1,23 @@ +const { Events } = require('discord.js'); + +module.exports = { + name: Events.InteractionCreate, + async execute(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 executing ${interaction.commandName}`); + console.error(error); + } + }, +}; \ No newline at end of file diff --git a/events/ready.js b/events/ready.js new file mode 100644 index 0000000..2d254c5 --- /dev/null +++ b/events/ready.js @@ -0,0 +1,9 @@ +const { Events } = require('discord.js'); + +module.exports = { + name: Events.ClientReady, + once: true, + execute(client) { + console.log(`Ready! Logged in as ${client.user.tag}`); + }, +}; \ No newline at end of file