简体   繁体   English

为什么别名没有被执行?

[英]why are aliases not getting executed?

Hello, I'm having an issue with aliases in discord.js the aliases don't work, I don't know why?您好,我在 discord.js 中遇到别名问题,别名不起作用,我不知道为什么?

client.on('message', message =>{
    // Exit when the message from the bot
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const commandName = args.shift().toLowerCase();

    if (!client.commands.has(commandName)) return;

    const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

    try {
        command.execute(message, args, commandName, client, Discord);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

You check if the entered command exists like this:您检查输入的命令是否存在,如下所示:

if (!client.commands.has(commandName)) return;

So if the entered command is an alias, it will return.所以如果输入的命令是别名,它会返回。 You would wanna do something like this:你会想做这样的事情:

client.on('message', message => { // Setup the event listener
    // Exit when the message from the bot or is not a command
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/); // Define args
    const commandName = args.shift().toLowerCase(); // Get the command name

    let command = client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName)); // Try to find the command by alias

    if (!command && !client.commands.has(commandName)) return; // The command doesn't exist

    command = client.commands.get(commandName); // If the command is not an alias, find it by name

    try {
        command.execute(message, args, commandName, client, Discord); // Attempt to execute the command
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

First check if there are any aliases that match the entered command.首先检查是否有任何别名与输入的命令匹配。 If there are none, it will return undefined.如果没有,它将返回 undefined。 Then we have an if statement to check if neither the aliases or the command are found and if so return.然后我们有一个 if 语句来检查是否没有找到别名或命令,如果找到则返回。 Otherwise use the normal command name to find the command.否则使用普通命令名称查找命令。

if (!client.commands.has(commandName)) return; this line should be after the const command = ... because client.commands is a collection with the commands, and all of the keys are the commands' names and not the aliases.这一行应该在const command = ...之后,因为client.commands是一个包含命令的集合,所有的键都是命令的名称而不是别名。

You can't use aliases because you put你不能使用别名,因为你把

if(!client.commands.has(commandName)) return;

This returns false if you try an alias, or anything else that isn't in your client.commands keys.如果您尝试使用别名或任何其他不在您的client.commands键中的内容,这将返回 false。 Remove this line and use this instead:删除此行并使用它:

   const args = message.content.slice(prefix.length).trim().split(/ +/);
    const commandName = args.shift().toLowerCase();
    const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases?.includes(commandName));
if(!command) return;
//…

This looks for the command first and then if no command is found, name or alias, it returns这看起来的命令然后再如果没有命令被发现,名称或别名,它返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM