【问题标题】:Delete message in a specific channel删除特定频道中的消息
【发布时间】:2019-06-13 12:40:47
【问题描述】:

我正在尝试使用我的机器人删除特定频道中的旧消息。
下面的代码不起作用,我不知道为什么。

if (msg.channel == channelDLid) {
  msg.delete(6000);
}

代码已执行,但没有做任何事情。

【问题讨论】:

    标签: node.js bots discord discord.js


    【解决方案1】:

    如果你想用 id 检查频道,你应该写:

    if (msg.channel.id == channelDLid) {
      msg.delete(6000);
    }
    

    【讨论】:

    • 确定如何从我的机器人中删除特定频道中的所有旧消息?只是不要删除最后 4 条消息?
    • 对此,请看 Plasma Chicken 的回答
    【解决方案2】:

    您可以使用Channel#bulkDelete,它允许您删除最多 2 周前的消息。 要仅删除特定消息,您可以使用Channel#fetchMessages,例如:

    const messages = await message.channel.fetchMessages({ limit: 100}) // Fetch last 100 messages
      .then(msgs => msgs.first(msgs.size - 3)) // Remove the last 3 messages out of the collection to delete
    
    message.channel.bulkDelete(messages, true);
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2020-10-01
      • 1970-01-01
      • 2020-11-22
      • 2021-04-21
      • 2019-03-08
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      相关资源
      最近更新 更多