Javascript:反垃圾邮件自动调解器 (Discord.js)

Javascript: Anti-spam Automoderator (Discord.js)

我正在编写一个多用途的 Discord 机器人来替换一些较小的机器人,我正在寻找一段代码来实现一种功能,该功能可以识别重复的消息或在很短的时间段内发送的消息(假设 5000 毫秒) ).

这里是可以用来实现这个想法的东西。

client.on("message", (message) => {
//let's use something like a spam variable for 10 or more messages sent within 5000ms
if(message.content === spam) {
    message.reply("Warning: Spamming in this channel is forbidden.");
    console.log(message.author.username + " (" + message.author.id + ") has sent 10 messages or more in 5 seconds in " + message.channel.name + ".");
  }
});

作为参考,我还使用 ~delete [n] 命令创建了一个删除消息的功能。它看起来像这样:

//this will only delete one message in the channel, the most recent one.
    message.delete(1000);
//1000 represents the timeout duration. it will only delete one message, regardless of the value.

//we can delete multiple messages with this, but note it has to come before the reply message.
    message.channel.bulkDelete(11);

我正在考虑以某种方式将删除命令与识别垃圾邮件结合起来。如果你有什么想法,那将是完美的。

试试这个:

const usersMap = new Map();
const LIMIT = 7;
const DIFF = 5000;

client.on('message', async(message) => {
    if(message.author.bot) return;
    
    if(usersMap.has(message.author.id)) {
        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) {

               message.reply("Warning: Spamming in this channel is forbidden.");
              message.channel.bulkDelete(LIMIT);
               
            } 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
        });
    }
})