問題描述
我正在嘗試編寫一個代碼,該代碼將檢查頻道中的所有消息以查找包含某些單詞的消息,如果確實包含這些單詞,則將其刪除.所以像:
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
};
};
});
};
我知道這不會檢查整個頻道,它只會檢查最后 50 條消息,但我不知道如何讓它檢查整個頻道,所以在我找到方法之前會這樣做.
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?
我好像還不夠清楚,假設一個頻道有以下對話:
It seems I was not clear enough, so let's say a channel has the following conversation:
A:伙計們!
B 人:嗨
C 人:再見
假設我想通過我的機器人刪除所有帶有Hi"的消息,我應該怎么做?注意:我不會在消息發送后立即刪除它,我只想在我想刪除它時刪除它.
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.
推薦答案
bulkDelete 功能刪除給定的新消息超過兩周.
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
});
};
要刪除超過 2 周的消息,您必須手動遍歷消息以刪除它們:
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))){
};
});
這篇關于在給定的不和諧頻道中搜索滿足條件的所有消息并刪除的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!