如何删除垃圾邮件?
Posted
技术标签:
【中文标题】如何删除垃圾邮件?【英文标题】:How can I delete spam messages? 【发布时间】:2021-08-30 09:36:16 【问题描述】:我有以下事件可以防止不和谐的垃圾邮件发送并自动静音垃圾邮件发送者。我从a YouTube video 获取了该代码。代码如下:
const usersMap = new Map();
const LIMIT = 10;
const TIME = 10000;
const DIFF = 4500;
client.on('message', async(message) =>
if(message.author.bot) return;
if(usersMap.has(message.author.id) && !message.member.roles.cache.has(staffId))
const userData = usersMap.get(message.author.id);
const lastMessage, timer = userData;
const difference = message.createdTimestamp - lastMessage.createdTimestamp;
let msgCount = userData.msgCount;
console.log(difference);
if(difference > DIFF)
clearTimeout(timer);
console.log('Cleared Timeout');
userData.msgCount = 1;
userData.lastMessage = message;
userData.timer = setTimeout(() =>
usersMap.delete(message.author.id);
console.log('Removed from map.')
, TIME);
usersMap.set(message.author.id, userData)
else
++msgCount;
if(parseInt(msgCount) === LIMIT)
let muterole = message.guild.roles.cache.find(role => role.name === 'Muted');
if(!muterole)
try
muterole = await message.guild.roles.create(
name : "Muted",
permissions: []
)
message.guild.channels.cache.forEach(async (channel, id) =>
await channel.createOverwrite(muterole,
SEND_MESSAGES: false,
ADD_REACTIONS : false
)
)
catch (e)
console.log(e)
message.member.roles.add(muterole);
message.channel.send(`$message.author.tag has been muted for potential spam.`);
setTimeout(() =>
message.member.roles.remove(muterole);
, TIME);
else
userData.msgCount = msgCount;
usersMap.set(message.author.id, userData);
else
let fn = setTimeout(() =>
usersMap.delete(message.author.id);
console.log('Removed from map.')
, TIME);
usersMap.set(message.author.id,
msgCount: 1,
lastMessage : message,
timer : fn
);
)
现在的问题是,如何让机器人删除垃圾邮件?基本上,我怎样才能一次删除所有垃圾邮件?
我试过了,但很困惑。
【问题讨论】:
请参阅How to Ask。您需要更具体地了解困扰您的问题。 您是否遇到任何错误? @Toasty 不,代码工作正常 @bruhbruh 好的,但是 *** 不是我们为您编写代码的地方。您至少应该自己尝试一下,如果遇到任何错误,您可以寻求帮助:) @Toasty 我知道这一点,但我只是不知道如何,嗯,一次性删除所有垃圾邮件。我不要求你为我编写代码。 【参考方案1】:您需要获取一定数量的消息并对其进行过滤,以便您只收到垃圾邮件发送者发送的消息,然后将它们传递给 bulkDelete()
这是一个小例子:
message.channel.messages.fetch(
limit: 100 // Change `100` to however many messages you want to fetch.
).then((messages) =>
const messagesToDelete = [];
// Use filter to find message by the spammer by id.
messages.filter(m => m.author.id === THE_SPAMMERS_ID_HERE).forEach(msg => messagesToDelete.push(msg));
// Delete the spam messages here.
message.channel.bulkDelete(messagesToDelete).then(() =>
message.channel.send("Messages deleted.").then(msg => msg.delete(
timeout: 3000
))
);
);
【讨论】:
【参考方案2】:你可以试试这个。
const Messages = await message.channel.messages.fetch( limit: 1000 );
Messages.forEach(msg =>
if (msg.author.id == "spammers id here") msg.delete();
);
【讨论】:
.then() 不需要等待。以上是关于如何删除垃圾邮件?的主要内容,如果未能解决你的问题,请参考以下文章