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•13mo 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👑•13mo ago
instead of // ... I put the rest of my code right
Lioness100
Lioness100•13mo 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👑•13mo 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•13mo 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•13mo 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•13mo ago
It's more productive for you to learn how it works than for me to microedit your code
-Carlos👑
-Carlos👑•13mo 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•13mo 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👑•13mo 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•13mo ago
The queue should be defined outside of the listener so it can maintain state between function calls.
-Carlos👑
-Carlos👑•13mo ago
done any way of making it delete the command instantly?
Lioness100
Lioness100•13mo ago
Delete the command?
-Carlos👑
-Carlos👑•13mo ago
message I meant
Lioness100
Lioness100•13mo 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
Want results from more Discord servers?
Add your server