Discord Bot自动批量删除问题

时间:2019-04-17 22:21:40

标签: javascript discord

我正在设置一个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); 
});

1 个答案:

答案 0 :(得分:0)

您正在尝试访问Trash变量,但是该变量不包含您认为的变量。 Trash被分配了the Promise that represents the end result of the function chain

您应该将结果传递到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); 
});

请记住,此功能的逻辑并不完善。伪代码是这样的:

- whenever a message is sent to this specific channel
  - queue up a timer to happen in 15 minutes
- after 15 minutes, delete 100 messages from the channel

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

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分钟或更早的邮件。