admin管理员组

文章数量:1334954

I am coding a multipurpose Discord bot to replace some of the more minor ones, and I am looking for a piece of code for a feature that recognizes repeated messages or messages sent in a very short time period (let's say 5000ms).

Here is what could be used to implement this idea.

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 + ".");
  }
});

For reference, I also made a feature that deletes messages, using a ~delete [n] mand. It looks like this:

//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 e before the reply message.
    message.channel.bulkDelete(11);

I was thinking of somehow bining the delete mand with recognizing spam messages. If you have any ideas, that would be perfect.

I am coding a multipurpose Discord bot to replace some of the more minor ones, and I am looking for a piece of code for a feature that recognizes repeated messages or messages sent in a very short time period (let's say 5000ms).

Here is what could be used to implement this idea.

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 + ".");
  }
});

For reference, I also made a feature that deletes messages, using a ~delete [n] mand. It looks like this:

//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 e before the reply message.
    message.channel.bulkDelete(11);

I was thinking of somehow bining the delete mand with recognizing spam messages. If you have any ideas, that would be perfect.

Share Improve this question edited Mar 23, 2018 at 16:44 Uncle Iroh 6,0556 gold badges53 silver badges64 bronze badges asked Jul 11, 2017 at 18:23 Kith M.Kith M. 1111 gold badge1 silver badge10 bronze badges 9
  • 1 You can actually change your console.log message a bit. With ES6 your can change you console.log() line from message.author.username + " (" + message.author.id + ") has sent 10 messages or more in 5 seconds in " + message.channel.name + "." to `${message.author.username} (${message.author.id}) has send 10 messages or more in 5 seconds in ${message.channel.name}.` – csk Commented Jul 13, 2017 at 8:45
  • that's much simpler. thanks – Kith M. Commented Jul 14, 2017 at 2:25
  • You are wele =D I think I have a solution, just let me test it! – csk Commented Jul 14, 2017 at 2:46
  • Also, is your bot exclusive to only one guild? Tf it is, you might be able to make an array for each guild member on startup (Even though its not the best idea) – csk Commented Jul 14, 2017 at 2:50
  • yes it is exclusive to only one guild as of now. and i am looking forward to that anti-spam solution XD – Kith M. Commented Jul 14, 2017 at 17:34
 |  Show 4 more ments

1 Answer 1

Reset to default 3

Try This:

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

本文标签: commandJavascript Antispam Automoderator (Discordjs)Stack Overflow