Add boilerplate

This commit is contained in:
2022-08-26 03:07:35 +00:00
parent 82f724e6ad
commit 5d5c3bfa94
9 changed files with 149 additions and 2 deletions

View File

@@ -12,5 +12,7 @@
"parserOptions": { "parserOptions": {
"ecmaVersion": "latest" "ecmaVersion": "latest"
}, },
"rules": {} "rules": {
"max-len": "off"
}
} }

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
.eslintcache .eslintcache
node_modules node_modules
.env .env
.*.swp
config.json

16
commands/ping.js Normal file
View File

@@ -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
};

View File

@@ -0,0 +1,5 @@
{
"clientToken": "",
"clientId": "",
"reregisterOnStartup": true
}

56
index.js Normal file
View File

@@ -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);

25
lib/getCommands.js Normal file
View File

@@ -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;
};

View File

@@ -4,6 +4,7 @@
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@discordjs/rest": "^1.1.0",
"discord.js": "^14.3.0", "discord.js": "^14.3.0",
"dotenv": "^16.0.1" "dotenv": "^16.0.1"
}, },
@@ -20,6 +21,7 @@
"prepare": "husky install", "prepare": "husky install",
"lint": "yarn lint:check", "lint": "yarn lint:check",
"lint:check": "eslint .", "lint:check": "eslint .",
"lint:fix": "eslint --fix ." "lint:fix": "eslint --fix .",
"register": "node registerCommands.js"
} }
} }

22
registerCommands.js Normal file
View File

@@ -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();
}

17
unregisterCommands.js Normal file
View File

@@ -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();