From 5d5c3bfa94a155984c43459bbf4f0c587d9160fe Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 26 Aug 2022 03:07:35 +0000 Subject: [PATCH] Add boilerplate --- .eslintrc.json | 4 ++- .gitignore | 2 ++ commands/ping.js | 16 +++++++++++ config/config.json.example | 5 ++++ index.js | 56 ++++++++++++++++++++++++++++++++++++++ lib/getCommands.js | 25 +++++++++++++++++ package.json | 4 ++- registerCommands.js | 22 +++++++++++++++ unregisterCommands.js | 17 ++++++++++++ 9 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 commands/ping.js create mode 100644 config/config.json.example create mode 100644 index.js create mode 100644 lib/getCommands.js create mode 100644 registerCommands.js create mode 100644 unregisterCommands.js diff --git a/.eslintrc.json b/.eslintrc.json index 0754f0b..e73fff9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -12,5 +12,7 @@ "parserOptions": { "ecmaVersion": "latest" }, - "rules": {} + "rules": { + "max-len": "off" + } } diff --git a/.gitignore b/.gitignore index 8ec466e..ea7416c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .eslintcache node_modules .env +.*.swp +config.json diff --git a/commands/ping.js b/commands/ping.js new file mode 100644 index 0000000..7d0b886 --- /dev/null +++ b/commands/ping.js @@ -0,0 +1,16 @@ +'use strict'; + +const { SlashCommandBuilder } = require('discord.js'); + +const data = new SlashCommandBuilder() + .setName('ping') + .setDescription('Replies with pong!'); + +const execute = async (interaction) => { + await interaction.reply('Pong!'); +}; + +module.exports = { + data, + execute +}; diff --git a/config/config.json.example b/config/config.json.example new file mode 100644 index 0000000..ca025aa --- /dev/null +++ b/config/config.json.example @@ -0,0 +1,5 @@ +{ + "clientToken": "", + "clientId": "", + "reregisterOnStartup": true +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..d249b18 --- /dev/null +++ b/index.js @@ -0,0 +1,56 @@ +'use strict'; + +const path = require('path'); + +const { Client, GatewayIntentBits } = require('discord.js'); + +const config = require(path.join(__dirname, 'config', 'config.json')); + +const getCommands = require(path.join(__dirname, 'lib', 'getCommands.js')); +const registerCommands = require(path.join(__dirname, 'registerCommands.js')); +const unregisterCommands = require(path.join(__dirname, 'unregisterCommands.js')); + +const client = new Client({ intents: [ GatewayIntentBits.Guilds ] }); + +client.commands = getCommands(); + +client.on('interactionCreate', async interaction => { + if (!interaction.isChatInputCommand()) + return; + + const command = client.commands.get(interaction.commandName); + if (!command) + 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 }); + } +}); + +client.once('ready', () => { + console.log('\nLogged in!\n'); +}); + +async function main() { + console.log('Discovering commands...'); + + console.log('- Commands: ' + + client.commands.map(({ data }) => data.name).join(', ') + ); + + if (config.reregisterOnStartup) { + console.log('- Unregistering commands'); + await unregisterCommands(); + + console.log('- Registering new commands'); + await registerCommands(); + } + + client.login(config.clientToken); +} + +main().catch(console.error); + diff --git a/lib/getCommands.js b/lib/getCommands.js new file mode 100644 index 0000000..9634960 --- /dev/null +++ b/lib/getCommands.js @@ -0,0 +1,25 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +const { Collection } = require('discord.js'); + +module.exports = () => { + const commands = new Collection(); + + fs.readdirSync(path.join(__dirname, '..', 'commands')) + .filter(file => file.endsWith('.js')) + .forEach(file => { + const command = require(path.join( + __dirname, + '..', + 'commands', + file + )); + + commands.set(command.data.name, command); + }); + + return commands; +}; diff --git a/package.json b/package.json index 7047e40..a94bb17 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "main": "index.js", "license": "MIT", "dependencies": { + "@discordjs/rest": "^1.1.0", "discord.js": "^14.3.0", "dotenv": "^16.0.1" }, @@ -20,6 +21,7 @@ "prepare": "husky install", "lint": "yarn lint:check", "lint:check": "eslint .", - "lint:fix": "eslint --fix ." + "lint:fix": "eslint --fix .", + "register": "node registerCommands.js" } } diff --git a/registerCommands.js b/registerCommands.js new file mode 100644 index 0000000..1de67c2 --- /dev/null +++ b/registerCommands.js @@ -0,0 +1,22 @@ +'use strict'; + +const path = require('path'); + +const { Routes } = require('discord.js'); +const { REST } = require('@discordjs/rest'); + +const config = require(path.join(__dirname, 'config', 'config.json')); +const getCommands = require(path.join(__dirname, 'lib', 'getCommands.js')); + +module.exports = () => { + const commands = getCommands().map(({ data }) => data.toJSON()); + + const rest = new REST({ version: '10' }).setToken(config.clientToken); + + return rest.put(Routes.applicationCommands(config.clientId), { body: commands }); +}; + +if (require.main === module) { + console.log(getCommands()); + module.exports(); +} diff --git a/unregisterCommands.js b/unregisterCommands.js new file mode 100644 index 0000000..de1626d --- /dev/null +++ b/unregisterCommands.js @@ -0,0 +1,17 @@ +'use strict'; + +const path = require('path'); + +const { Routes } = require('discord.js'); +const { REST } = require('@discordjs/rest'); + +const config = require(path.join(__dirname, 'config', 'config.json')); + +module.exports = () => { + const rest = new REST({ version: '10' }).setToken(config.clientToken); + + return rest.put(Routes.applicationCommands(config.clientId), { body: [] }); +}; + +if (require.main === module) + module.exports();