Discord bot:命令名称的命令处理程序别名 [英] Discord bot: Command Handler alias for command name
问题描述
我正在开发Discord机器人,并试图改善我已经运行的命令处理程序.我有一个文件夹,每个文件都是一个额外的命令.我想扩展系统,所以我有相同命令的别名,例如我希望我的clearchat命令可与/clearchat或/cc一起使用,但我不想仅创建另一个文件并复制代码.这就是我所拥有的:
I am working on a Discord bot, and trying to improve my already functioning command handler. I have a folder, and every file is an extra command. I want to expand the system, so I have alias name for the same command, e.g. I want my clearchat command to function with /clearchat or with /cc, but I dont want to create just another file and copy the code. This is what I have:
// I left out the other imports etc.
client.commands = new Discord.Collection();
// Reading commands-folder
const commandFiles = fs.readdirSync("./commands/").filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on("message", msg => {
if (msg.content.startsWith(config.prefix) && !msg.author.bot && msg.guild) {
const args = msg.content.slice(config.prefix.length).split(" ");
const command = args.shift().toLowerCase();
if (client.commands.find(f => f.name === command)) {
client.commands.get(command).execute(client, msg, args);
}
}
});
,然后是命令文件夹中的命令文件:
and then a command file inside the commands-folder:
module.exports = {
name: "clearchat",
execute(client, msg, args) {
if (msg.member.hasPermission("ADMINISTRATOR")) {
msg.channel.messages.fetch({limit: 99}).then(messages => {
msg.channel.bulkDelete(messages);
});
}
}
}
(我知道最多最多只能删除100条消息,对此我可以接受)
(I know it only deletes 100 messages max, I am fine with that)
我通过更改我的client.on("message")函数中的几行来成像一些东西,而只需要在clearchat.js文件中写一行 name:["clearchat","cc",...]
,在这里我可以根据需要编写尽可能多的别名.
I image something in changing a few lines in my client.on("message) function, and just having to write in the clearchat.js file a line like name: ["clearchat", "cc", ...]
where I can go writing as much aliases as I want.
提前谢谢!
推荐答案
首先,您必须使用命令中的别名创建一个数组.
First, you'll have to create an array with the aliases in your command.
module.exports = {
name: "clearchat",
aliases: ["cc"],
execute(client, msg, args) {
}
}
然后,就像使用命令一样,为别名创建一个Collection.
Then, the same you did with commands, create a Collection for the aliases.
client.aliases = new Discord.Collection()
最后,将别名绑定到命令:
And finally, bind the alias to the command:
if (command.aliases) {
command.aliases.forEach(alias => {
client.aliases.set(alias, command)
})
}
现在,当您想执行命令时,必须检查它是否具有别名.
Now, when you want to execute a command, you'll have to check if it has an alias.
const commandName = "testcommand" // This should be the user's input.
const command = client.commands.get(commandName) || client.aliases.get(commandName); // This will return the command and you can proceed by running the execute method.
fs.readdir(`./commands/`, (error, files) => {
if (error) {return console.log("Error while trying to get the commmands.");};
files.forEach(file => {
const command = require(`./commands/${file}`);
const commandName = file.split(".")[0];
client.commands.set(commandName, command);
if (command.aliases) {
command.aliases.forEach(alias => {
client.aliases.set(alias, command);
});
};
});
});
这篇关于Discord bot:命令名称的命令处理程序别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!