繁体   English   中英

带有多个单词的命令的 Javascript Discord bot

[英]Javascript Discord bot with command that has multiple words

所以我有一段这样的代码。

case 'trait':
        if (args[1] === 'aerobic'){
            const embed = new RichEmbed()
            .setTitle('Aerobic')    
            .addField('Effect','When attacking with Window, gets SPDEF - and SPD +.')
            .addField('Temtem that can have this trait', 'Volarend')
            message.channel.send(embed); 
        }else if (args[1]  === 'air specialist'){
            const embed = new RichEmbed()
            .setTitle('Air specialist')
            .addField('Effect','+15% damage with Wind techniques.')
            .addField('Temtem that can have this trait', 'Saku, Barnshe, Zebhyruff')
            message.channel.send(embed);
        }else {
            message.channel.sendMessage('Invalid trait (use "-" instaed of space, when using two word trait)')
             }
        break;

第一个“如果”按预期工作,但我对第二个有问题,因为它有两个词。 我想像这样在 Discord 上使用这个命令

!trait air specialist

代码的开头看起来像这样,不包括令牌、前缀等:

    bot.on('ready', () => {
    console.log('This bot is online!');
})

bot.on('message', message=>{

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

先感谢您。

问题是您的消息被拆分,因此这两个词不在args[1] args[1]是“空气”, args[2]是“专家”。

您可以通过更改拆分消息的方式使它们位于同一字符串中来解决此问题,或者如果仍需要拆分,则查看多个 args 以输入 else if 语句。

例如,您的消息可以这样拆分:

const args = message.content.toLowerCase().slice(PREFIX.length).trim().split(/ (.*)/);

或者您可以按如下方式输入 if 语句:

else if (args[1]  === 'air' && args[2] === 'specialist') { ... }

如果您只希望命令中包含一个条件参数,则一种解决方案是使用串联将它们重新组合。 这仅在您需要命令和一个参数时才有效,但如果您希望单个命令有 2 个或更多参数,则它会更复杂一些。

case 'trait':
    // We should always have the first one 
    // but you should error check this first.
    let combinedArgs = args[1];

    for(let i = 2; i < args.length; i++)
    {
        combinedArgs += " " + args[i];
    }

    if (combinedArgs === 'aerobic'){

    ... and so on

暂无
暂无

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

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