Added command executing system.

Rebuild, again, the command handling system, now to support the event handling.

Also added more organized console logs.
This commit is contained in:
Guz
2021-08-26 16:59:00 -03:00
parent 01c884d97b
commit 671052f1c1
5 changed files with 121 additions and 109 deletions

View File

@@ -1,63 +1,87 @@
const { SlashCommandBuilder } = require('@discordjs/builders');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
const {
ownerId,
} = require('../../config');
const { SlashCommandBuilder } = require('@discordjs/builders');
const config = require('../../config');
module.exports = {
data: new SlashCommandBuilder()
.setName('reload')
.setDescription('Reloads a command'),
.setDescription('Reloads a command')
.addStringOption(option =>
option.setName('command')
.setDescription('The command name to be reloaded')
.setRequired(true)),
permissions: [
{
id: '870383205306494997',
type: 'ROLE',
permission: false,
id: config.ownerId,
type: 'USER',
permission: true,
},
],
async execute(interaction) {
const {
clientCommands,
commands,
} = require('../../index');
async execute(interaction, client) {
const commandName = interaction.options.getString('command');
const { commands, commandsData, rest } = require('../../index');
if (!(await commands.fetch()).find(command => command.name === commandName)) {
return interaction.reply({
content: `No such command \`${commandName}\` found.`,
ephemeral: true,
});
}
else if (commandName === 'reload') {
return interaction.reply({
content: 'You can\'t reload this command.',
ephemeral: true,
});
const cmdName = interaction.options.getString('command').toLowerCase();
const cmdIdx = commands.findIndex(c => c.data.name === cmdName);
const command = commands.find(c => c.data.name === cmdName);
if(!command?.meta.canReload) {
interaction.reply({ content: `You can't reload the \`${command.data.name}\` command.`, ephemeral: true });
return;
}
const commandFolders = fs.readdirSync('./commands');
const folderName = commandFolders.find(folder => fs.readdirSync(`./commands/${folder}`).includes(`${commandName}.js`));
const oldCommand = commands[cmdIdx];
const oldCommandData = commandsData[cmdIdx];
delete require.cache[require.resolve(`../${folderName}/${commandName}.js`)];
const clientCommand = (await client.guilds.cache.get(config.devGuildId)?.commands.fetch()).find(c => c.name === cmdName);
const commandFolder = fs.readdirSync('./commands').find(folder => fs.readdirSync(`./commands/${folder}`).includes(`${cmdName}.js`));
delete require.cache[require.resolve(`../../commands/${commandFolder}/${cmdName}.js`)];
try {
const cmd = require(`../${folderName}/${commandName}.js`);
clientCommands.set(commandName, {
id: (await commands.fetch()).find(command => command.name === commandName).id,
data: cmd,
});
await interaction.reply({
content: `Command \`${commandName}\` reload completed.`,
ephemeral: true,
});
const newCommand = require(`../../commands/${commandFolder}/${cmdName}.js`);
commands[cmdIdx] = {
data: newCommand.data,
permissions: newCommand.permissions,
execute: newCommand.execute,
id: clientCommand.id,
name: clientCommand.name,
};
commandsData[cmdIdx] = newCommand.data.toJSON();
await rest.put(
Routes.applicationGuildCommands(config.clientId, config.devGuildId), {
body: commandsData,
},
);
await clientCommand.permissions.set({ id: clientCommand.id, permissions: newCommand.permissions });
console.log(
'Old Command --------------------------------\n', oldCommand,
'\nData:\n', oldCommandData,
'\nPermissions:');
console.table(oldCommand.permissions);
console.log(
'\nNew Command --------------------------------\n', commands[cmdIdx],
'\nData:\n', commandsData[cmdIdx],
'\nPermissions:');
console.table(newCommand.permissions);
}
catch (error) {
console.error(error);
await interaction.reply({
content: `A error occur while trying to reload \`${interaction.commandName}\`\n\`\`\`${error}\`\`\``,
content: `A error occur while trying to reload the \`${cmdName}\` command.\n\`\`\`${error}\`\`\``,
ephemeral: true,
});
}

View File

@@ -1,8 +1,3 @@
const {
MessageActionRow,
MessageButton,
} = require('discord.js');
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
@@ -13,24 +8,13 @@ module.exports = {
{
id: '870383205306494997',
type: 'ROLE',
permission: false,
permission: true,
},
],
async execute(interaction) {
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('button')
.setLabel('Button')
.setStyle('PRIMARY'),
);
interaction.reply({
content: `Ping`,
components: [row],
});
console.log('ping hello!');
interaction.reply({ content: 'Ping!', ephemeral: true });
// console.log(`Ping! ${interaction.options.get('input').value}`);
},
};

View File

@@ -1,28 +1,34 @@
const { devGuildId } = require('../../config.json');
const { commands, permissions } = require('../../index');
const config = require('../../config.json');
const { commands } = require('../../index');
module.exports = {
name: 'ready',
async execute(client) {
console.log('bot ready!');
console.log(await commands);
console.log(await permissions);
await client.application?.fetch();
for (let i = 0; i < commands.length; i++) {
for (const command of commands) {
const command = (await client.guilds.cache.get(devGuildId)?.commands.fetch()).find(c => c.name === commands[i].name);
const clientCommand = (await client.guilds.cache.get(config.devGuildId)?.commands.fetch()).find(c => c.name === command.data.name);
console.log(command.id);
await clientCommand.permissions.set({ id: clientCommand.id, permissions: command.permissions });
const permission = permissions[i];
console.log(permission);
await command.permissions.set({ id: command.id, permissions: permission });
command.id = clientCommand.id;
command.name = clientCommand.name;
}
console.log('Guild commands totally registered');
console.log('\nGuild Commands:');
console.table(commands);
console.log('\nBot configurations');
console.table({
'Owner Id': config.ownerId,
'Development Guild Id': config.devGuildId,
'Client Id': config.clientId,
});
console.log('\n\n-- BOT READY --\n\n');
},
};

View File

@@ -1,22 +1,34 @@
const { commands } = require('../../index');
module.exports = {
name: 'interactionCreate',
async execute(interaction, client) {
if (!interaction.isCommand() || !client.commands.has(interaction.commandName)) return;
if(interaction.isCommand()) {
const command = client.commands.get(interaction.commandName);
console.log(`\nCommand ${interaction.commandName} executing ----------------\n`);
if (!command) return;
const command = commands.find(c => c.data.name === interaction.commandName);
console.log('\nCommand data:\n');
console.log(command);
console.log('\nCommand console:\n');
if(!command) return;
try {
await command.execute(interaction, client);
}
catch (error) {
console.error(error);
await interaction.reply({
content: `A error occur while trying to execute the command.\n\`\`\`${error}\`\`\``,
ephemeral: true,
});
}
console.log(`\nCommand ${interaction.commandName} executed ----------------\n`);
try {
await command.execute(interaction);
}
catch (error) {
console.error(error);
await interaction.reply({
content: 'A error occur while trying to execute the command.\n```' + error + '```',
ephemeral: true,
});
}
},

View File

@@ -1,22 +1,10 @@
const Discord = require('discord.js');
const {
Client,
Collection,
Intents,
} = require('discord.js');
const {
REST,
} = require('@discordjs/rest');
const {
Routes,
} = require('discord-api-types/v9');
const { Client, Collection, Intents } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
const {
token,
devGuildId,
clientId,
} = require('./config.json');
const { token, devGuildId, clientId } = require('./config.json');
const client = new Client({
intents: [Intents.FLAGS.GUILDS],
@@ -26,7 +14,7 @@ client.commands = new Collection();
const commandFolders = fs.readdirSync('./commands');
const commands = [];
const permissions = [];
const commandsData = [];
for (const folder of commandFolders) {
@@ -35,30 +23,28 @@ for (const folder of commandFolders) {
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
commands.push(command.data.toJSON());
permissions.push(command.permissions);
commands.push(command);
commandsData.push(command.data.toJSON());
}
}
module.exports = { commands, permissions };
const rest = new REST({
version: '9',
}).setToken(token);
module.exports = { commands, commandsData, rest };
(async () => {
try {
console.log('Refreshing application commands');
await rest.put(
Routes.applicationGuildCommands(clientId, devGuildId), {
body: commands,
body: commandsData,
},
);
console.log('Application commands refreshed');
}
catch (error) {
console.error(error);