diff --git a/commands/ping.js b/commands/ping.js index 47f5e9c..f0475be 100644 --- a/commands/ping.js +++ b/commands/ping.js @@ -1,7 +1,18 @@ +const { SlashCommandBuilder } = require('discord.js'); + +// TODO - Add insults as the response to this command module.exports = { - name: 'ping', - description: 'Ping!', - execute(message) { - message.channel.send('**Pong.**'); + data: new SlashCommandBuilder() + .setName('ping') + .setDescription('Replies with your input!'), + /* + .addStringOption(option => + option.setName('input') + .setDescription('The input to echo back')), + */ + example: "ping", + isPrivileged: false, + async execute(message) { + await message.channel.send('**Pong.**'); } }; \ No newline at end of file diff --git a/index.js b/index.js index 48d0036..bc5bdf0 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const fs = require('fs'); require('dotenv').config(); const libCore = require("./libCore"); const libUtils = require("./libUtils"); +const deployCommands = require("./utilities/deployCommands"); const { DebugBuilder } = require("./utilities/debugBuilder"); const log = new DebugBuilder("server", "index"); @@ -20,15 +21,16 @@ const { //const Discord = require('discord.js');Client, Collection, Intents const { Client, + Events, Collection, - Intents, + GatewayIntentBits, MessageActionRow, MessageButton } = require('discord.js'); //const client = new Discord.Client(); const client = new Client({ - intents: [Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILDS] + intents: [GatewayIntentBits.GuildMessages, GatewayIntentBits.Guilds] }); prefix = process.env.PREFIX @@ -107,12 +109,15 @@ const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(' //const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); - const command = require(filePath); + const command = require(filePath); // Set a new item in the Collection // With the key as the command name and the value as the exported module - client.commands.set(command.name, command); + client.commands.set(command.data.name, command); } +// Deploy commands +deployCommands.deploy(client.guilds.cache.map(guild => guild.id)); + client.on('ready', () => { log.DEBUG(`Discord server up and running with client: ${client.user.tag}`); log.DEBUG(`Starting HTTP Server`); @@ -122,38 +127,26 @@ client.on('ready', () => { log.INFO("HTTP server started!"); }); -client.on('interactionCreate', async interaction => { - //if (!interaction.isCommand()) return; - if (!interaction.isSelectMenu()) return; +client.on(Events.InteractionCreate, async interaction => { + if (!interaction.isChatInputCommand()) return; - let aaa = interaction.values[0]; - await interaction.channel.send({ - content: 'You picked something', - ephemeral: true - }); + const command = interaction.client.commands.get(interaction.commandName); + log.DEBUG("Interaction: ", interaction.client.commands); - try { - //await command.execute(interaction); - } catch (error) { - log.ERROR(error); - //console.error(error); - //await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + if (!command) { + console.error(`No command matching ${interaction.commandName} was found.`); + return; } -}); - -client.on('messageCreate', message => { - if (!message.content.startsWith(prefix) || message.author.bot) return; - - const args = message.content.slice(prefix.length).trim().split(/ +/); - const command = args.shift().toLowerCase(); - - if (!client.commands.has(command)) return; try { - client.commands.get(command).execute(message, args); + await command.execute(interaction); } catch (error) { - log.ERROR(error); - //message.reply('there was an error trying to execute that command!'); + 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 }); + } } }); diff --git a/package.json b/package.json index a746f52..d424c1a 100644 --- a/package.json +++ b/package.json @@ -4,22 +4,21 @@ "description": "Discord RSS News Bot", "main": "index.js", "dependencies": { - "@discordjs/builders": "^0.15.0", - "@discordjs/rest": "^0.5.0", - "axios": "^0.24.0", - "chatgpt": "^1.4.0", - "discord-api-types": "^0.35.0", - "discord.js": "^13.8.1", - "dotenv": "^10.0.0", - "express": "^4.17.1", - "fs": "^0.0.1-security", - "js-doc": "^0.5.0", - "jsonfile": "^6.1.0", - "mathjs": "^10.6.4", - "openai": "^3.1.0", - "parse-files": "^0.1.1", - "rss-parser": "^3.12.0", - "mysql": "2.18.1", + "@discordjs/builders": "~1.4.0", + "@discordjs/rest": "~1.5.0", + "axios": "~1.3.4", + "chatgpt": "~4.7.2", + "discord-api-types": "~0.37.35", + "discord.js": "~14.7.1", + "dotenv": "~16.0.3", + "express": "~4.18.2", + "fs": "~0.0.1-security", + "jsdoc": "~3.6.7", + "jsonfile": "~6.1.0", + "openai": "~3.1.0", + "parse-files": "~0.1.1", + "rss-parser": "~3.12.0", + "mysql": "~2.18.1", "cookie-parser": "~1.4.4", "debug": "~2.6.9", "ejs": "~2.6.1", @@ -30,9 +29,6 @@ "test": "echo \"Error: no test specified\" && exit 1", "start": "node index.js" }, - "author": "John Facey", - "license": "ISC", - "devDependencies": { - "jsdoc": "^3.6.7" - } + "author": "Logan Cusano", + "license": "ISC" } diff --git a/utilities/deployCommands.js b/utilities/deployCommands.js new file mode 100644 index 0000000..75e0a76 --- /dev/null +++ b/utilities/deployCommands.js @@ -0,0 +1,45 @@ +const { REST, Routes } = require('discord.js'); + +require('dotenv').config(); +const token = process.env.TOKEN; +const clientId = process.env.clientId; +const guildId = process.env.guildId; + +const fs = require('node:fs'); +const path = require('node:path'); + +const commands = []; +// Grab all the command files from the commands directory you created earlier +const commandsPath = path.resolve('./commands'); +const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); + +exports.deploy = (guildIDs) => { + if (Array.isArray(guildIDs)) guildIDs = [guildIDs]; + // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment + for (const file of commandFiles) { + const command = require(`${path.resolve(commandsPath, 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! + for (const guildId of guildIDs){ + (async () => { + try { + console.log(`Started refreshing ${commands.length} application (/) commands for guild ID: ${guildId}.`); + // 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 for guild ID: ${guildId}.`); + } catch (error) { + // And of course, make sure you catch and log any errors! + console.error(error); + } + })() + } +}; \ No newline at end of file