【问题标题】:How to detect a discord message being edited (but not updated)?如何检测正在编辑(但未更新)的不和谐消息?
【发布时间】:2022-02-11 00:29:19
【问题描述】:

我正在使用 Node 和 Discord.JS,试图查看是否有一种方法可以在编辑消息时区分编辑类型,以便机器人可以取笑用户编辑他们的消息。该功能目前运行良好,如下:

let responses = ["some", "responses"];
bot.on('messageUpdate', ( message ) => {
     let result = responses[Math.floor(Math.random()*(responses.length)-1)]
     message.channel.send(result);
})

但是,这会检测 所有 消息更新,包括更新链接以进行嵌入。有什么方法可以区分故意的用户编辑和通过事件侦听器嵌入更新的消息,还是我需要使用 if..else 语句来解决?

【问题讨论】:

    标签: discord discord.js bots


    【解决方案1】:

    要检查消息何时被编辑,您需要使用messageUpdate 事件监听器。
    messageUpdate 事件中,有2 个参数。 (oldMessage, newMessage) Learn More

    然后,您可以使用.content 属性检查oldMessagenewMessage 参数的内容。

    代码示例:

    client.on("messageUpdate", (oldMessage, newMessage) => {
        if(oldMessage.content === newMessage.content) return // Will do nothing if the content of oldMessage is equals to newMessage's content
        // Do here your stuff
    })
    

    希望对你有所帮助!

    【讨论】:

    • 还应该检查邮件是否由机器人发送/编辑。
    • 这似乎有效,但现在我遇到了一个问题,即我从结果数组中获得的结果不会发送,错误为“无法读取未定义的属性(读取'通道') ”。如果我在传递的参数中留下“消息”,则表示该消息未定义,但通过它似乎无法读取它的属性。有什么解决办法吗?
    【解决方案2】:

    忽略这个,我没有找到我希望的答案;(

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2021-07-01
      • 2020-08-07
      • 2021-05-09
      • 2020-12-30
      相关资源
      最近更新 更多