魔术8ball机器上的命令有问题

时间:2019-08-10 08:04:55

标签: javascript discord.js

我正在用Discord.js制作一个discord机器人,现在我正在尝试制作一个神奇的8ball。我遇到的问题是启动命令并启动,因为它不会响应。 (我之前在同一文件中使用过bot.on('message', message =>行。

我尝试在youtube周围寻找一台8球机,但它们却大不相同。另外我对编程还很陌生

bot.on('message', message => {
    if (message.startsWith(prefix + "8ball") && (message.length() > 10)) {
        let replies = ["Yes.", "No.", "I don't see it happening anytime soon.", "Maybe.", "Maybe....... Not.", "The future looks bright.", "Definitely Yes.", "Please ask again later.", "C'mon man don't put me on the spot like that."];
        let result = Math.floor((Math.random() * replies.length));
        message.channel.send(result);
    } else {
        message.channel.reply("sorry I dont understand your question");
    }
});```

1 个答案:

答案 0 :(得分:1)

.length()不是一个函数,message是一个包含很多细节而不只是内容的对象。您想做的是message.content.length > 10 因此您的代码应类似于:

bot.on('message', message => {
    if (message.startsWith(`${prefix}8ball`) && (message.content.length > 10)) {
        let replies = ["Yes.", "No.", "I don't see it happening anytime soon.", "Maybe.", "Maybe....... Not.", "The future looks bright.", "Definitely Yes.", "Please ask again later.", "C'mon man don't put me on the spot like that."];
        let result = Math.floor((Math.random() * replies.length));
        message.channel.send(result);
    } else {
        message.channel.reply("sorry I dont understand your question");
    }
});

还请注意,我将prefix + '8ball'更改为`$ {prefix} 8ball。通过$ {}标记,您可以将内容直接放置在字符串中,而不必每次都将它们串联起来。