我试图编写一个代码来检查 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/