Thought process & Explanation of Implementation
Implement a Discord Bot where it'll mute someone if they spam x number of messages within y seconds. Assuming you guys know the Discord.JS library. Let's say if I declared a global map that takes care of handling each user's id, mapping to some value (not sure what that value could be yet). I was thinking that upon every message sent, if the author's ID of the message is NOT in a Map, if it isn't then that means they have not sent a message yet.
Now here's the easy implementation - If we add the user to the map, and then invoke a setTimeout() with y = 5 seconds and after 5 seconds, it will remove the user from the map, and then every time a message is sent by the author, if their ID is found in the Map, here's what I was thinking - the map can map user ID's to the number of messages. If their messages reach x = 5 messages, give them the mute role. They will eventually be removed from the map but the bot will work accordingly.
The problem with this is that if people figure out the system, they can easily abuse it. They can send their first message, have their ID added to the map, and then spam 3 more messages, (total 4 now), and then wait for their ID to be removed from the map, and then spam more messages. Because once the ID is removed from the Map, it will just add them to the setTimeout again, and they can continue this over and over again.
So another thing I was thinking was what if we have two maps, both have the user IDs as keys, but one maps to the total number of messages sent, the other maps to a setTimeout function. What I was thinking was saving the setTimeout callback for the user in a map, and then we can add some more checks, we can check to see if the user's messages sent each time has some kind of difference where the first message sent and the second message sent is about 2-4 seconds apart. If it is, then what we can do is reset their timeout by clearing the timeout by getting a reference to the timeout function in the map, clearing it, and then invoking another timeout and saving it to the map, replacing the old one. That way, if the user is sending messages normally, 2-4 seconds after each one, then we can reset their timer. But if the user is sending messages like 0-2 seconds every time, keep the timer.