Add boilerplate
This commit is contained in:
@@ -12,5 +12,7 @@
|
||||
"parserOptions": {
|
||||
"ecmaVersion": "latest"
|
||||
},
|
||||
"rules": {}
|
||||
"rules": {
|
||||
"max-len": "off"
|
||||
}
|
||||
}
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
.eslintcache
|
||||
node_modules
|
||||
.env
|
||||
.*.swp
|
||||
config.json
|
||||
|
||||
16
commands/ping.js
Normal file
16
commands/ping.js
Normal 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
|
||||
};
|
||||
5
config/config.json.example
Normal file
5
config/config.json.example
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"clientToken": "",
|
||||
"clientId": "",
|
||||
"reregisterOnStartup": true
|
||||
}
|
||||
56
index.js
Normal file
56
index.js
Normal 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
25
lib/getCommands.js
Normal 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;
|
||||
};
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
22
registerCommands.js
Normal file
22
registerCommands.js
Normal 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
17
unregisterCommands.js
Normal 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();
|
||||
Reference in New Issue
Block a user