【问题标题】:Working on discord.js, and my bot keeps spamming the same message在 discord.js 上工作,我的机器人一直在发送相同的消息
【发布时间】:2019-07-17 19:24:42
【问题描述】:

我一直在用 discord.js 开发一个机器人,并制作了一个基本的机器人。每当我尝试使用“dadjoke”或“dadpun”命令时,它只会一遍又一遍地发送占位符消息。有什么帮助吗?

代码在这里:

} else if (message.content.startsWith(`${prefix}joke`)) {
            var repl = rand[Math.floor(Math.random()*rand.length)];
        message.channel.send(repl);
    } else if (message.content.startsWith(`${prefix}pun`)) {
        var repl2 = pun[Math.floor(Math.random()*pun.length)];
        message.channel.send(repl2);

提前致谢!

【问题讨论】:

  • 什么占位符消息?
  • dadjoke1,dadjoke2,dadpun1,dadpun2。只是基本的占位符
  • 好的,但是这些是在哪里定义的? randpun 中有什么内容?您的代码从这些数组的内容中随机选择。
  • 这两个变量是 var rand =["dadjoke 1", "dadjoke 2"]; var pun =["dadpun 1", "dadpun 2"];

标签: discord.js


【解决方案1】:

尝试将此添加到您的 client.on("message", ...) 代码的顶部。

if (message.author.bot) return;

这可能会解决您的问题。

【讨论】:

  • 嗨,我在这里添加了代码:if (message.author.bot) return; client.on("message", async (message) => {,它在终端中输出的只是消息未定义。有任何想法吗?此外,评论没有显示它,但它在 client.on 上方。
  • 没关系,原来我需要把它放在下面,而不是上面。感谢您的帮助!
  • 没问题!抱歉,我之前没有回复。
  • 无论如何我应该有更多的澄清。
猜你喜欢
  • 2021-04-19
  • 2021-05-08
  • 2020-02-26
  • 2019-09-20
  • 1970-01-01
  • 2021-08-18
  • 2021-01-09
  • 2020-10-26
  • 2021-02-03
相关资源
最近更新 更多