首页 > 解决方案 > Discord Bot 自动批量删除问题

问题描述

我正在设置一个 Discord 机器人,它将在 15 分钟的间隔内删除特定文本频道中的所有消息,但这些消息不会删除。

Client.on("message", async function(message) {
    if(message.channel.id != "506258018032418817") return;
    setInterval (function () {
        var Trash = message.channel.fetchMessages({ limit: 100 }).then(
        message.channel.bulkDelete(Trash));
      }, 900000); 
});

标签: javascriptdiscord

解决方案


您正在尝试访问该Trash变量,但它不包含您认为的内容。 Trash被分配了代表函数链最终结果的 Promise

您应该将结果传递给then()并将其用作bulkDelete()调用的参数。

Client.on("message", async function(message) {
    if(message.channel.id != "506258018032418817") return;
    setInterval (function () {
        message.channel.fetchMessages({ limit: 100 }).then(
           function(deleteThese) { message.channel.bulkDelete(deleteThese); })
      }, 900000); 
});

请记住,此功能的逻辑并不合理。伪代码如下:

- 每当有消息发送到此特定频道时
  - 排队一个计时器在 15 分钟内发生
- 15分钟后,从频道中删除100条消息

考虑如果发生这种情况会发生什么

1:00 Paul talks in channel
--- (timer 1 is started)
1:01 Elizabeth responds
--- (timer 2 is started)
... < time passes > ...
1:14 Charles says something in the channel
... < timer 1 triggers; room is wiped > ...
1:15 Diane says something in response to Charles' deleted message
... < timer 2 triggers; room is wiped > ...
1:16 Charles asks Diane to repeat what she just said

您可能希望更改在fetchMessages()通话中打发时间的方法,以便仅删除 15 分钟或更早的消息。


推荐阅读