Search a given discord channel for all messages that satisfies the condition and delete

白昼怎懂夜的黑 提交于 2019-12-23 04:55:29

问题


I was trying to make a code that will check all messages in a channel for messages that contain certain words, and delete them if it does contain them. So something like:

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
      };
    };
  });
};

I am aware that this will not check the entire channel and it will only check the last 50 messages, but I do not know how to make it check the whole channel so this will do until I find out how to do that.

But what code would delete the message that passes the check? Or any different way I could approach this?

Edit:

It seems I was not clear enough, so let's say a channel has the following conversation:

Person A: Hi, guys!

Person B: Hi

Person C: Bye

Let's say I want to delete all the messages with "Hi" in it through my bot, how should I do this? Note: I do not with to delete a message right after it has been sent, I only want to delete it when I want to do so.


回答1:


The bulkDelete function delete given messages that are newer than two weeks.

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 msgDel = msgs.filter(msgss => msgss.content.includes(check)) // Finds all messages with 'check'
    msg.channel.bulkDelete(msgDel) // Deletes all messages that got found
  });
};

To delete the messages older than 2 weeks, you have to iterate through the messages manually to delete them:

async function deleteReturnLast(chan, option, prevMsg, cond) {
  return chan.fetchMessages(option)
    .then(async msgs => {
      if (msgs.size === 0){
    if (cond(prevMsg)) {
      prevMsg.delete()
        .then(d => console.log('last message deleted: ' + d.content))
        .catch(err => console.log('ERR>>', err, prevMsg.content, option.before));   }
    return prevMsg;
      };
      let last = msgs.last();
      for (const[id, msg] of msgs) {
    let tmp = (id === last.id) ? prevMsg : msg;
    if (cond(tmp)) {
      tmp.delete()
        .then(d => console.log('Message deleted: ' + d.content))
        .catch(err => console.log('ERR>>', err));
    }
      };
      return last;
    })
    .catch(err => console.log('ERR>>', err));
}

function cond(msg) {
  return !msg.content.includes('a');
}

client.on('message', async function(msg) {
  let chan = msg.channel;
  let last = chan.lastMessage;
  while (last !== (last = await deleteReturnLast(chan, {limit: 2, before: last.id}, last, cond))){
  };
});



回答2:


Well, this is how I solved my problem after I realised the 2 week limitation of fetchMessages()

  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
    })
  }


来源:https://stackoverflow.com/questions/56819979/search-a-given-discord-channel-for-all-messages-that-satisfies-the-condition-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!