Discord.js command hendler
Здраствуйте, появилась небольшая проблема, начинал создавать бота я много "заимствовал" чужой код, а теперь страдаю, так как не могу внести корректировки. К сути проблемы: у меня есть "менеджер команд" вот так он выглядит:
const Discord = require('discord.js')
const client = new Discord.Client()
const db = require('quick.db');
const { readdirSync } = require('fs');
const { join } = require('path');
const prefix = (`!`);
const logs = require('discord-logs');
logs(client);
client.commands = new Discord.Collection();
const commandFiles = readdirSync(join(__dirname, "commands")).filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(join(__dirname, "commands", `${file}`));
client.commands.set(command.name, command);
}
client.on("message", async message => {
if (message.author.bot) return;
if (message.channel.type === 'dm') return;
if (!message.content.startsWith(prefix)) return db.add(`${message.author.id}.msg`, 1);
if (message.content.startsWith(prefix)) {
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).run(client, message, args);
} catch (error) {
console.error(error);
}
}
})
а вот так выглядит пример команды:
const Discord = require('discord.js')
module.exports = {
name: "ping",
description: "test command",
async run (client, message, args) {
message.channel.bulkDelete(1, true);
const ping = new Discord.MessageEmbed()
.setDescription(`?\`${client.ws.ping}\`ms`);
message.channel.send(ping);
}
}
И мне бы хотелось добавить возможность "вариативности" написания этой команды, не добавляя несколько файлов (Пример: !ping выполняет команду и !serverping выполняет ее же)
Ответы (1 шт):
Автор решения: Геннадий П
→ Ссылка
Используйте алиасы команд:
module.exports = {
name: "ping",
aliases: ['serverping', 'anotherping'],
description: "test command",