【问题标题】:I'm new to making discord bot with node.js and i'm stumped on this我是使用 node.js 制作不和谐机器人的新手,我对此感到困惑
【发布时间】:2020-04-25 23:35:32
【问题描述】:

我是制作不和谐机器人的新手,我正在尝试制作命令。我为此遵循了一个 Youtube 教程,该人说要这样做

const prefix = '-';
bot.on('message', msg=>{
    let args = msg.content.substring(prefix.length).split(" ");
    }

witch(args[0]){
        case 'ping':
            msg.channel.send('pong');
        break;
    }
})

而且看任何只有一个字符的东西都可以工作。有什么办法可以让它只适用于prefix 而没有别的吗?我一直在做很多挖掘,但我不知道如何。有人可以帮忙吗?

【问题讨论】:

  • 能否请您添加一个链接到您看过的教程?

标签: javascript node.js discord discord.js


【解决方案1】:

我不太确定你在问什么,但我可能知道为什么那段代码不起作用

const prefix = '-';
bot.on('message', msg => {
    let args = msg.content.substring(prefix.length).split(" ");
    if (message.content.indexOf(prefix) !== 0 || message.author.bot) return;
    //the line above means, stop working when the message doesn't start with the prefix or is sent by a bot

    switch (args[0]) {
        case 'ping':
            msg.channel.send('pong');
            break;
    }
});

这就是你要找的东西吗(也有一个不需要的} 可能会导致机器人崩溃)

【讨论】:

  • 非常感谢!这正是我所需要的!
  • @TakamiyaMichael 没问题,很高兴能帮上忙!
  • 很高兴你做到了!
猜你喜欢
  • 2020-08-18
  • 1970-01-01
  • 2021-09-22
  • 2011-10-13
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
相关资源
最近更新 更多