I have 3 replicas that all respond to the same thing

Yo! I have a bot that sends a message when a thread is created, but Im running 3 replicas, so the problem is that they all send the messge at the same time. I tried to resolve this by just checking if the last message was by the bot, but since they all check at once its false for all of them, and all of them end up sending. Any idea how I can resolve this without creating a database / api or creating extra overhead beyond the function itself?
client.on(Events.ThreadCreate, async (thread: ThreadChannel) => {
if (thread.parent?.name === 'whatever') {
const lastMessage = (await thread.messages.fetch({ limit: 1 })).first();

// Checks if the bot has already sent this message
if (lastMessage && lastMessage?.author.id === thread.guild.members.me?.id) {
return
}

return thread.send({
content: "whatever"
})
}
})
client.on(Events.ThreadCreate, async (thread: ThreadChannel) => {
if (thread.parent?.name === 'whatever') {
const lastMessage = (await thread.messages.fetch({ limit: 1 })).first();

// Checks if the bot has already sent this message
if (lastMessage && lastMessage?.author.id === thread.guild.members.me?.id) {
return
}

return thread.send({
content: "whatever"
})
}
})
30 Replies
d.js toolkit
d.js toolkit4mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Mark
Mark4mo ago
Why do you have three of the same bot running?
Axe
AxeOP4mo ago
for redundancy
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Axe
AxeOP4mo ago
how? Someone crashes one of them and the bot still survives? 2 instances left while the last one restarts
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Axe
AxeOP4mo ago
obv but edge cases exist
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Axe
AxeOP4mo ago
I dont know, if I knew I would fix it But things in production may crash if something that isnt supposed to happen happens and in that case we have redundancy
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Axe
AxeOP4mo ago
yes
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Axe
AxeOP4mo ago
Because bugs can still happen even if you test it?
Deimoss
Deimoss4mo ago
restarting is pretty fast, and you shouldn't have crashes often anyway
Axe
AxeOP4mo ago
this is besides the point tho and doesnt help with solving the problem im facing
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Axe
AxeOP4mo ago
How is it not related? Im looking for a method to interact with the discordjs api to avoid an issue with a discordjs bot? its a goal yeah but the world isnt perfect ^^ its not like facebook has one instance running because they dont make mistakes sure it restarts fast but that doesnt help if its down its down
Mark
Mark4mo ago
You can utilize a timeout to wait a few seconds and then fetch the last message and check if it's from the bot, but that's still incredibly inefficient and you're making multiple API calls to make up for the fact that you're not addressing the root cause of the crashes You don't see bot projects doing this because it doesn't scale at all
monbrey
monbrey4mo ago
What you're attempting to do to achieve redundancy is lacking a key component called load balancing. Having three whole complete bots running at once with no other changes will result in what is happening here - three events being processed three times causing three responses. Conceptually, the way you should handle this is to have a single incoming point of traffic (the websocket) that can send the event to be processed on any one of multiple servers, by checking that it's running first. This is essentially how websites scale to handle load. But discord.js does not support this decoupling. You'd have to build it yourself using the modules (/ws, /rest, /core etc) The other thing to consider here is that redundancy is better at address hardware failures. If one of your bots crashes because of an error in the code, the other two will crash too because the same error exists there.
Axe
AxeOP4mo ago
The api response is buffered or something, I tried this and it doesnt work isnt this the point of sharding? no that doesnt happen. Only 1/3 crashes whenever a slash command fails. but Ive investigated it a lot now and it seems there is no way except sharding that will solve this
monbrey
monbrey4mo ago
Yes but also no. Sharding splits the guild load across each process. Each shard still only runs once though, guilds don't shift to another shard if one goes down That seems genuinely odd, unless that crash is because one of the others already replied. Even then, I'd expect 2/3
Axe
AxeOP4mo ago
🤷‍♀️ Try it ig Wait so even with 3 shards only 1 will be in each guild so it still goes down for that guild if their shard goes down?
monbrey
monbrey4mo ago
Yes
Axe
AxeOP4mo ago
alright then im forced to scale down ig
monbrey
monbrey4mo ago
Or not use discord.js, yeah
Axe
AxeOP4mo ago
options? without switching language
monbrey
monbrey4mo ago
Mentioned one here In my personal opinion though, without knowing how big your bot is, it's really not worth it
Axe
AxeOP4mo ago
yeah I skipped over that part, thats what I said in the initial message 'without api / database' will rather focus resources on other projects its not a major issue I assumed so, but it was worth a try, might have been some hacky workaround the timeout idea would have been perfect if they didnt store the reply
cloud
cloud4mo ago
instead write a script to start another bot if one goes down make a endpoint to check health
Axe
AxeOP4mo ago
breaks the constraint of not having things outside of the bot Like everything should be in the bots code, it should be a single bot, not have an api or database or be environment dependant, and not require maintenance also then it needs to be down for the healthcheck to work, even 5s downtime is too much, im looking for a solution that is always online Im aware, which is why I gave it up. There are a bunch of small things blocking it tho so its not impossible, but just due to the way discordjs works and how discord handles api calls within a short time frame
Want results from more Discord servers?
Add your server