feat!: Updated command folder structure for new command handler

This commit is contained in:
Marius Begby committed 2023-07-17 10:40:33 +02:00
1 parent 3da8275359
commit 421734be26
19 files changed
+122 -120

No files matched your search

+2 -2
View File
@@ -21,8 +21,8 @@
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./src/index.js",
"start-pretty": "node ./src/index.js | pino-pretty",
"deploy": "node ./src/utils/deploy-commands.js && node ./src/utils/deploy-system-commands.js",
"deploy-pretty": "node ./src/utils/deploy-commands.js | pino-pretty && node ./src/utils/deploy-system-commands.js | pino-pretty",
"deploy": "node ./src/utils/deploy-slash-commands.js",
"deploy-pretty": "node ./src/utils/deploy-slash-commands.js | pino-pretty",
"eslint": "eslint ./"
},
"dependencies": {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -8,6 +8,7 @@ const {
} = require(path.resolve('./config.json'));
module.exports = {
isSystemCommand: true,
data: new SlashCommandBuilder()
.setName('guilds')
.setDescription('Show list of guilds where bot is added.')
@@ -11,6 +11,7 @@ const osu = require('node-os-utils');
const packageJson = require(path.resolve('./package.json'));
module.exports = {
isSystemCommand: true,
data: new SlashCommandBuilder()
.setName('status')
.setDescription('Show bot status.')
+33 -21
View File
@@ -18,27 +18,6 @@ const client = new Discord.Client({
]
});
// Setup commands collection and load commands
// todo: extract this logic to a separate file
client.commands = new Discord.Collection();
const commandFiles = fs
.readdirSync(path.resolve('./src/commands'))
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(path.resolve(`./src/commands/${file}`));
client.commands.set(command.data.name, command);
}
const systemCommandFiles = fs
.readdirSync(path.resolve('./src/system-commands'))
.filter((file) => file.endsWith('.js'));
for (const file of systemCommandFiles) {
const systemCommand = require(path.resolve(
`./src/system-commands/${file}`
));
client.commands.set(systemCommand.data.name, systemCommand);
}
const player = new Player(client, {
useLegacyFFmpeg: false,
ytdlOptions: {
@@ -67,6 +46,39 @@ onBeforeCreateStream(async (track) => {
return null;
});
// Setup commands collection and load commands
// todo: extract this logic to a separate file
client.commands = new Discord.Collection();
const commandFolders = fs.readdirSync(path.resolve('./src/commands'));
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(path.resolve(`./src/commands/${folder}`))
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(path.resolve(
`./src/commands/${folder}/${file}`
));
client.commands.set(command.data.name, command);
}
}
const eventFolders = fs.readdirSync(path.resolve('./src/events'));
for (const folder of eventFolders) {
const eventFiles = fs
.readdirSync(path.resolve(`./src/events/${folder}`))
.filter((file) => file.endsWith('.js'));
for (const file of eventFiles) {
const event = require(path.resolve(`./src/events/${folder}/${file}`));
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
}
if (process.env.NODE_ENV === 'development') {
player.on('debug', (message) => {
logger.debug(message);
-44
View File
@@ -1,44 +0,0 @@
const path = require('node:path');
const fs = require('node:fs');
const logger = require(path.resolve('./src/services/logger.js'));
const { REST, Routes } = require('discord.js');
require('dotenv').config();
const commands = [];
const commandFiles = fs
.readdirSync(path.resolve('./src/commands'))
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(path.resolve(`./src/commands/${file}`));
commands.push(command.data.toJSON());
}
const rest = new REST({ version: '10' }).setToken(
process.env.DISCORD_BOT_TOKEN
);
(async () => {
try {
logger.info('DEPLOYING BOT SLASH COMMANDS');
logger.info(
commands.map((commands) => {
return commands.name;
}),
'Bot commands found:'
);
logger.info('Started refreshing application (/) bot commands.');
await rest.put(
Routes.applicationCommands(process.env.DISCORD_CLIENT_ID),
{
body: commands
}
);
logger.info('Successfully refreshed application (/) bot commands.');
} catch (error) {
logger.error(error, 'Failed to refresh application (/) bot commands.');
}
})();
+85
View File
@@ -0,0 +1,85 @@
const path = require('node:path');
const fs = require('node:fs');
const logger = require(path.resolve('./src/services/logger.js'));
const { REST, Routes } = require('discord.js');
const { systemServerGuildIds } = require(path.resolve('./config.json'));
require('dotenv').config();
const slashCommands = [];
const systemCommands = [];
const commandFolders = fs.readdirSync(path.resolve('./src/commands'));
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(path.resolve(`./src/commands/${folder}`))
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(path.resolve(
`./src/commands/${folder}/${file}`
));
command.isSystemCommand
? systemCommands.push(command.data.toJSON())
: slashCommands.push(command.data.toJSON());
}
}
const rest = new REST({ version: '10' }).setToken(
process.env.DISCORD_BOT_TOKEN
);
(async () => {
try {
logger.info('DEPLOYING BOT SLASH COMMANDS');
logger.info(
slashCommands.map((command) => {
return command.name;
}),
'Bot commands found:'
);
logger.info('Started refreshing application (/) bot commands.');
await rest.put(
Routes.applicationCommands(process.env.DISCORD_CLIENT_ID),
{
body: slashCommands
}
);
logger.info('Successfully refreshed application (/) bot commands.');
} catch (error) {
logger.error(error, 'Failed to refresh application (/) bot commands.');
}
try {
logger.info('DEPLOYING SYSTEM SLASH COMMANDS');
logger.info(
systemCommands.map((systemCommand) => {
return systemCommand.name;
}),
'System commands found:'
);
logger.info('Started refreshing application (/) system commands.');
for (const systemServerGuildId of systemServerGuildIds) {
logger.info(
`Refreshing system commands for guild id ${systemServerGuildId}.`
);
await rest.put(
Routes.applicationGuildCommands(
process.env.DISCORD_CLIENT_ID,
systemServerGuildId
),
{
body: systemCommands
}
);
}
logger.info('Successfully refreshed application (/) system commands.');
} catch (error) {
logger.error(
error,
'Failed to refresh application (/) system commands.'
);
}
})();
-53
View File
@@ -1,53 +0,0 @@
const path = require('node:path');
const fs = require('node:fs');
const logger = require(path.resolve('./src/services/logger.js'));
const { REST, Routes } = require('discord.js');
const { systemServerGuildIds } = require(path.resolve('./config.json'));
require('dotenv').config();
const systemCommands = [];
const systemCommandFiles = fs
.readdirSync(path.resolve('./src/system-commands'))
.filter((file) => file.endsWith('.js'));
for (const file of systemCommandFiles) {
const systemCommand = require(path.resolve(`./src/system-commands/${file}`));
systemCommands.push(systemCommand.data.toJSON());
}
const rest = new REST({ version: '10' }).setToken(
process.env.DISCORD_BOT_TOKEN
);
(async () => {
try {
logger.info('DEPLOYING SYSTEM SLASH COMMANDS');
logger.info(
systemCommands.map((systemCommand) => {
return systemCommand.name;
}),
'System commands found:'
);
logger.info('Started refreshing application (/) system commands.');
for (const systemServerGuildId of systemServerGuildIds) {
await rest.put(
Routes.applicationGuildCommands(
process.env.DISCORD_CLIENT_ID,
systemServerGuildId
),
{
body: systemCommands
}
);
}
logger.info('Successfully refreshed application (/) system commands.');
} catch (error) {
logger.error(
error,
'Failed to refresh application (/) system commands.'
);
}
})();