Automod System

I realized if I say 3 bad words fast enough it won't trigger the automod, why could this happend? Code: https://pastebin.com/cy6T1Re2
Pastebin
Pastebin.com - Locked Paste
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
33 Replies
Lioness100
Lioness100•14mo ago
Since event listeners are triggered concurrently, and you there is a lot of async functionality in the listener, you're in a situation where messages sent in quick succession might not be fully processed before the next one arrives. i.e.: message 1 is detected data 1 is fetched from the db message 2 is detected data 2 is fetched from the db new data is written to the db from message 1 (not fast enough!) new data is written to the db from message 2 The easiest solution, in my opinion, is to use @sapphire/async-queue, which will ensure that the messages are processed sequentially. Example:
await queue.wait();
try {
let data = await db.find({
user: message.member.id,
guild: message.guild.id,
automod: true,
});

// ...
} finally {
queue.shift();
}
await queue.wait();
try {
let data = await db.find({
user: message.member.id,
guild: message.guild.id,
automod: true,
});

// ...
} finally {
queue.shift();
}
One potential performance concern would be that this would process all messages sequentially when you really only care about processing an individual's messages sequentially (and doing this concurrently for all individuals). This could be solved by creating a different queue for each person (I'll leave that as an exercise to the reader kekw
-Carlos🎃
-Carlos🎃•14mo ago
instead of // ... I put the rest of my code right
Lioness100
Lioness100•14mo ago
The best way for that would probably be Map<string, Queue>, and just add a new entry for each user. As a form of cache invalidation, you can check before queue.shift() if there are no other promises (queue.promises), remove it from the map Yes (although use your discretion)
-Carlos🎃
-Carlos🎃•14mo ago
Got an error:
[ERROR] Encountered error on event listener "filter" for event "messageCreate" at path "C:\Users\cmart\Desktop\Discord Bots\Sapphire\src\listeners\Automod\filter.js" TypeError: queue.wait is not a function
at messageCreateListener.run (C:\Users\cmart\Desktop\Discord Bots\Sapphire\src\listeners\Automod\filter.js:24:17)
[ERROR] Encountered error on event listener "filter" for event "messageCreate" at path "C:\Users\cmart\Desktop\Discord Bots\Sapphire\src\listeners\Automod\filter.js" TypeError: queue.wait is not a function
at messageCreateListener.run (C:\Users\cmart\Desktop\Discord Bots\Sapphire\src\listeners\Automod\filter.js:24:17)
Lioness100
Lioness100•14mo ago
You didn't create the queue variable. As I said, use your discretion. Make sure you understand the library and how to use it (it's a small library, so reading the source code might be best).
Lioness100
Lioness100•14mo ago
GitHub
utilities/packages/async-queue/src/lib/AsyncQueue.ts at main · sapp...
Common JavaScript utilities for Sapphire Projects. Contribute to sapphiredev/utilities development by creating an account on GitHub.
Lioness100
Lioness100•14mo ago
It's more productive for you to learn how it works than for me to microedit your code
-Carlos🎃
-Carlos🎃•14mo ago
const { AsyncQueue } = require("@sapphire/async-queue");
const queue = new AsyncQueue();
const { AsyncQueue } = require("@sapphire/async-queue");
const queue = new AsyncQueue();
Added this, (not sure if it's correct), it doesn't log any error now, but it also still doesn't work
Lioness100
Lioness100•14mo ago
Please send the full code. Also, automod and automodMute both return promises, so you have to await them if you want to use the queue effectively. You also should await the message.channel.send and message.delete (Or, if you want to make it fast, use Promise.all and resolve the promises all at once.)
-Carlos🎃
-Carlos🎃•14mo ago
Pastebin
Pastebin.com - Locked Paste
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Lioness100
Lioness100•14mo ago
The queue should be defined outside of the listener so it can maintain state between function calls.
-Carlos🎃
-Carlos🎃•14mo ago
done any way of making it delete the command instantly?
Lioness100
Lioness100•14mo ago
Delete the command?
-Carlos🎃
-Carlos🎃•14mo ago
message I meant
Lioness100
Lioness100•14mo ago
Order your promises consciously. If you want to delete the message before you check the DB, do that. If you want to do both at the same time, use Promise.all. if you want to register the infraction in the DB before sending the message, do that. If you want to do it at the same time, use Promise.all. Order your promises considering your priorities Maybe you want to delete the message while querying the DB while sending the confirmation message, then register the infraction in the DB, then unshift the queue Often you want the visible things to happen first to give the illusion (whether true or false) of things being fast In situations such as sending the confirmation message early, this is called an optimistic update. Sorry that's a bunch of word jumble it's late lol
-Carlos🎃
-Carlos🎃•14mo ago
it's ok lol if I do this I would also need to put my if to check if the message.content contains a filtered word, which would not work in my command since I want it to check if the member has 3, 5, 7, 8 or 9 auto warnings first
Lioness100
Lioness100•14mo ago
Then no, you can't delete the message instantly 🤷 Maybe you could do some caching in the db? Or, since the queue only needs to wait until you write to the db, try to write to the db and shift the queue as soon as possible
-Carlos🎃
-Carlos🎃•14mo ago
. Like this?
Lioness100
Lioness100•14mo ago
I'm on my phone so I don't have the original code up, but probably
-Carlos🎃
-Carlos🎃•14mo ago
yeah just tried and it didn't work
Lioness100
Lioness100•14mo ago
Can you send the full code
-Carlos🎃
-Carlos🎃•14mo ago
Pastebin
Pastebin.com - Locked Paste
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Lioness100
Lioness100•14mo ago
Should you be waiting until after automod() too?
-Carlos🎃
-Carlos🎃•14mo ago
wouldn't that just be the same as before
Lioness100
Lioness100•14mo ago
What do you mean
-Carlos🎃
-Carlos🎃•14mo ago
I meant my code was already like that
Lioness100
Lioness100•14mo ago
I don't remember your previous code well, but I think you sent a message before shifting that wasn't necessary for the queue to shift
-Carlos🎃
-Carlos🎃•14mo ago
i mean, the delete time is just like 1 second or less, so it doesn't really matter however I just realized this: If I member gets auto warned 3 times, gets auto timeouted, then I manually untimeout them, then that person say anything (anything, even if the word is not in the filter), it will timeout them like if they had 5 warnings weird since 1. word wasn't on filter 2. the user would have 4 warnings, not 5
Lioness100
Lioness100•14mo ago
Is that because youre checking data.length + 1 instead of data.length?
-Carlos🎃
-Carlos🎃•14mo ago
earlier, when I was making this command, I was trying stuff like if (data.length === 3) { do something } before making await this.container.util.automodMute() (and it didn't work), due to this I logged data.length and it said 0 so adding a + 1 fixed it
Lioness100
Lioness100•14mo ago
You should probably figure out what the original problem was, because your solution might've created another one
-Carlos🎃
-Carlos🎃•14mo ago
what really confuses me is that when I manually untimeout them, and then the user says anything it timeouts them for 4 hours, which is the 5 warns punishment it doesn't add the warn to the db but it adds the timeout it also doesn't send the .send() part
-Carlos🎃
-Carlos🎃•14mo ago
anyone has any idea why could this happend? code: https://pastebin.com/cy6T1Re2 Pastebin Password: TL8PytN6tj
Pastebin
Pastebin.com - Locked Paste
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Want results from more Discord servers?
Add your server