From 3b7a206ac0d4c77ffa8e59df04c4c17ea6101754 Mon Sep 17 00:00:00 2001 From: staticfanfare Date: Sun, 17 Dec 2023 20:21:26 -0500 Subject: [PATCH] it works nowgit add index.js --- commands/utility/ping.js | 13 ++++++++++ commands/utility/server.js | 10 ++++++++ commands/utility/user.js | 12 +++++++++ deploy-commands.js | 47 +++++++++++++++++++++++++++++++++++ index.js | 51 +++++++++++++++++++++++++++++++++++++- 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 commands/utility/ping.js create mode 100644 commands/utility/server.js create mode 100644 commands/utility/user.js create mode 100644 deploy-commands.js diff --git a/commands/utility/ping.js b/commands/utility/ping.js new file mode 100644 index 0000000..949a2c4 --- /dev/null +++ b/commands/utility/ping.js @@ -0,0 +1,13 @@ +//Boring init stuff +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + //Creates the slash command + data: new SlashCommandBuilder() + .setName('ping') + .setDescription('beeps at you ovo'), + //Executes said slash command + async execute(interaction) { + await interaction.reply('BEEP!'); + }, +}; diff --git a/commands/utility/server.js b/commands/utility/server.js new file mode 100644 index 0000000..b9570f8 --- /dev/null +++ b/commands/utility/server.js @@ -0,0 +1,10 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('server') + .setDescription('Prints server info'), + async execute(interaction) { + await interaction.reply(`You are in **${interaction.guild.name}**, with **${interaction.guild.memberCount}** members, including yourself, **${interaction.user.username}**! ;>`); + }, +}; diff --git a/commands/utility/user.js b/commands/utility/user.js new file mode 100644 index 0000000..c7e9a79 --- /dev/null +++ b/commands/utility/user.js @@ -0,0 +1,12 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('user') + .setDescription('Prints user info'), + async execute(interaction) { + let joinStamp = interaction.member.joinedTimestamp; + joinStamp = Math.floor(joinStamp / 1000); + await interaction.reply(`You are **${interaction.user.username}**. You came here ****.`); + }, +}; diff --git a/deploy-commands.js b/deploy-commands.js new file mode 100644 index 0000000..6ccb4a1 --- /dev/null +++ b/deploy-commands.js @@ -0,0 +1,47 @@ +const { REST, Routes } = require('discord.js'); +const { clientID, guildID, token } = require('./config.json'); +const fs = require('node:fs'); +const path = require('node:path'); + +const commands = []; +// Grab all the command folders from the commands directory you created earlier +const foldersPath = path.join(__dirname, 'commands'); +const commandFolders = fs.readdirSync(foldersPath); + +for (const folder of commandFolders) { + // Grab all the command files from the commands directory you created earlier + const commandsPath = path.join(foldersPath, folder); + const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); + // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment + for (const file of commandFiles) { + const filePath = path.join(commandsPath, file); + const command = require(filePath); + if ('data' in command && 'execute' in command) { + commands.push(command.data.toJSON()); + } else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + } + } +} + +// Construct and prepare an instance of the REST module +const rest = new REST().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); + } +})(); + diff --git a/index.js b/index.js index 37c8cb6..dbf973b 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,60 @@ -const { Client, Events, GatewayIntentBits } = require('discord.js'); +//Boring boring initialization stuff +const fs = require('node:fs'); +const path = require('node:path'); +const { Client, Collection, Events, GatewayIntentBits } = require('discord.js'); const { token } = require('./config.json'); const client = new Client({ intents: [GatewayIntentBits.Guilds] }); +//Handle commands +client.commands = new Collection(); +//Command Loading +const foldersPath = path.join(__dirname, 'commands'); +const commandFolders = fs.readdirSync(foldersPath); + +for(const folder of commandFolders) { + const commandPath = path.join(foldersPath, folder); + const commandFiles = fs.readdirSync(commandPath).filter(file => file.endsWith('.js')); + for (const file of commandFiles) { + const filePath = path.join(commandPath, file); + const command = require(filePath); + if ('data' in command && 'execute' in command) { + client.commands.set(command.data.name, command); + } else { + console.log(`WARN: Command at ${filePath} is missing 'data' and/or 'execute' properties.`); + } + } +} + +//Command Interaction Processing +client.on(Events.InteractionCreate, async interaction => { + if (!interaction.isChatInputCommand()) return; + + const command = interaction.client.commands.get(interaction.commandName); + + if (!command) { + console.error(`ERR: Command "${interaction.commandName}" not found`); + return; + } + + try { + await command.execute(interaction); + } catch (error) { + console.error(error); + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ content: `ERROR!`, ephermal: true}); + } else { + await interaction.reply({ content: `ERROR!`, ephermal: true}); + } + } +}); + +//Log the bot in with the token supplied in config.json client.once(Events.ClientReady, readyClient => { console.log(`Ready! Logged in as ${readyClient.user.tag}`); }); client.login(token); + +