【问题标题】:How can the bot edit its message once the command executor's command has been edited?一旦命令执行者的命令被编辑,机器人如何编辑它的消息?
【发布时间】:2020-10-18 08:27:24
【问题描述】:
if(message.content === "hello") {
   message.channel.send("World");
}
if(message.content === "hi") {
   message.channel.send("There");
}

我正在为我的机器人尝试一项新功能,但我不完全确定如何启动它。所以我有关于这条消息的命令。所以我想做的是当我向机器人发送Hello 时,它会回复World,如果我将消息编辑到hi,机器人也会编辑它的消息;从WorldThere

discord.js 版本:12.2.0

【问题讨论】:

  • 你使用的是什么版本的 discord.js?
  • @Syntle v12.2.0

标签: javascript node.js bots discord discord.js


【解决方案1】:

这不是一个稳定的解决方案,因为在这种情况下没有适当的方法来编辑机器人的消息,因此您必须在消息被编辑的频道中获取机器人的最后一条消息并进行编辑.

您必须像这样使用messageUpdate 事件:

client.on('messageUpdate', async (oldMessage, newMessage) => {
  if (oldMessage.content === 'hello' && newMessage.content === 'hi') {
    const messages = await newMessage.channel.messages.fetch()
    const lastMsg = messages.filter((msg) => msg.author.id === client.user.id).first()
    lastMsg.edit('there')
  }
})

【讨论】:

  • 我删除了我的答案,Syntle 解决方案比我的更容易理解。在任何情况下,您都必须使用 messageUpdate 事件并在内容检查之前和内容检查之后做一些特定的情况。
  • 它给了我一个错误UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'edit' of undefined
  • 在您编辑消息之前,机器人是否发送了消息?
  • @Syntle 它没有发送任何东西
  • 那就是问题所在,我不确定您是否仅使用我的答案中的代码,但我的答案是您的代码的扩展,因此您仍然需要能够触发机器人在编辑您的消息之前先发送“世界”消息。
【解决方案2】:

你应该这样做。我厌倦了它并且它有效。

client.on('messageUpdate', (oldMessage, newMessage) => {
        
        if (newMessage.content === "hello") {
            message.channel.send("World");
        }
        if (newMessage.content === "hi") {
            message.channel.send("There");
        }
   
})

您也可以将 thie 与删除上一条消息结合使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2016-08-24
    • 2021-07-10
    • 2017-02-09
    • 2013-03-20
    • 2018-05-31
    • 2021-07-02
    • 2015-01-19
    相关资源
    最近更新 更多