javascript - 在给定的discord channel 中搜索满足条件的所有消息并删除

标签 javascript bots discord.js

我试图编写一个代码来检查 channel 中所有消息中是否包含某些单词的消息,如果确实包含它们,则将其删除。所以类似:

if(msg.content.startsWith(prefix+'clean') {
  let check = msg.content.split(prefix+'clean')[1]; // Condition, in this case if it containts a certain string
  msg.channel.fetchMessages().then(msgs => { // Get messages to check
    let msglog = msgs.array() // Make an array with all the messages fetched
    for(var i = 0; i < msglog.size; i++) { // Loop to check all messages in array
      if (check in msglog[i]) {
        // Code to delete that message
      };
    };
  });
};

我知道这不会检查整个 channel ,它只会检查最后 50 条消息,但我不知道如何让它检查整个 channel ,所以在我找到如何做到这一点之前,这会起作用。

但是什么代码会删除通过检查的消息呢?或者我可以采取任何不同的方式来解决这个问题?

编辑:

看来我还不够清楚,所以假设一个 channel 有以下对话:

A:大家好!

B:嗨

C 人:再见

假设我想通过我的机器人删除所有带有“Hi”的消息,我应该怎么做?注意:我不会在消息发送后立即删除它,我只想在需要时删除它。

最佳答案

嗯,这就是我在意识到 fetchMessages() 的 2 周限制后解决问题的方法

  else if(msg.content.startsWith(`${prefix}clean`}) { // Check for command
    let check = msg.content.split(`${prefix}clean`)[1] // Defines a check
    msg.channel.fetchMessages({ limit: 100 }).then(msgs => { // Fetches the last 100 messages of the channel were the command was given
      const msgstodelete = msgs.filter(del => del.content.includes(check)) // Filters the messages according to the check
      msg.delete() // Deletes the original message with the command
      for (var i = 0; i<Array.from(msgstodelete.keys()).length; i++) { 
        msg.channel.fetchMessage(Array.from(msgstodelete.keys())[i]).then(deldel => deldel.delete())
      } // Loop to delete all messages that passed the filter
    })
  }

关于javascript - 在给定的discord channel 中搜索满足条件的所有消息并删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56819979/

相关文章:

html - Telegraf:不能使用额外的

rest - 微软机器人框架自行崩溃

javascript - 如果公会成员不在一个语音 channel 中,我可以将他们移动到一个语音 channel 吗?

javascript - 尝试检查所述属性是否未定义时如何防止 "Cannot read property example of undefined"

javascript - parseInt() 错误地解析带有指数的数字文字

javascript - 使用 python 处理 javascript

javascript - Nightmarejs-如何读取表行内容?

javascript - Discord 机器人开发 : How do I stop this infinite loop?

javascript - 这是在 GitHub Pages 上实现博客功能的好方法吗?

javascript - 以编程方式打开 <select> 选择器