Bot crashing without any errors
Good afternoon! I apologize if this isn't the correct forum for my question, but my bot seems to be going offline after one particular command has been triggered. It processes through the on message create, posts the message in the channel (along with sending a couple of DMs to the users who triggered that command), but then it hangs and goes offline without any error message or anything indicating a problem.
Here is the section where everything occurs:
const startReadyCheckSequence = (vote, client) => {
message = "\n It's time to vote! Reply !vote yes|no in the channel or respond to the DM with your vote. You have 5min to respond"
vote.queue.forEach(user=> {console.log(
Sending message to ${user.username}`)
user.send({content: message})
})
sendMessageToVoteChannel(client,mentionUsers(vote) + message)
vote.ready = true
console.log ("BEFORE SET TIMEOUT")
setTimeout(removeNotReadyUsers, 300000, vote, client)
};
and here is the command parser section that calls it:
}else if ( primaryCommand === "join" ){ updateStatus() if (arguments[0] === undefined){ client.channels.fetch(msgChannelID, true) .then(channel=> channel.send("Please provide a valid vote code")) return; } if (!VOTE_CODES.includes(arguments[0].toLowerCase())){ client.channels.fetch(msgChannelID, true) .then(channel=> channel.send("Please provide a valid vote code")) return; } ///TODO clean this up var vote = chosenVote(arguments) VoteCommands.addToQueue(receivedMessage, vote, client)
10 Replies
- 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!
- ✅
Marked as resolved by staffAnd finally, what links the two togther.
const addToQueue = (receivedMessage, vote, client) => { if (vote.queue.length == vote.MIN_VOTE_SIZE){ receivedMessage.channel.send(vote.name +" is in progress of voting") return; } if (vote.queue.includes(receivedMessage.author)){ receivedMessage.channel.send("You are already in the queue") return; } const userName = receivedMessage.author.username vote.queue.push(receivedMessage.author) receivedMessage.channel.send(userName + ' was added to the queue ' + vote.queueCount())-- [email protected] node -v v18.16.0 If it helps for detail the join command works fine without hanging until the queu size is met, then when the vote is triggered, that's when it hangs, after processing the DMs and sending the message to the channel (the min_vote_size is set to 8)
//status.updateStatus() if (vote.queue.length == MIN_VOTE_SIZE){ startReadyCheckSequence(vote, client); }
};
first, please determine exactly which line is hanging
which is to say, you'll want to find exactly which line is the last to execute properly
If you aren't getting any errors, try to place
console.log
checkpoints throughout your code to find out where execution stops.
- Once you do, log relevant values and if-conditions
- More sophisticated debugging methods are breakpoints and runtime inspections: learn more
Please add the following code to your code base outside of any other event listeners and provide the full log output relevant to your issue.
- Note: if you initialize your Client as bot
or other identifiers you need to use these instead of client
- If the output is too long to post consider using a bin instead: gist | paste.gg | sourceb.in | hastebinthis may also help
Thank you for that, I've combed through everything I can, and the only thing I can think of that would be causing the issue is sending the DMs to users, does Discord throw a bot offline for sending out (at max 8) DMs in rapid succession? As soon as that happens, the bot hangs, then goes offline completely. No error, nothing.
Here is the last part of the log file:
It sends 2 DMs
Sends a console log ("BEFORE SET TIMEOUT")
Then the message.create event skips the three messages the bot posted in the bot channel on the server.
Then, after sitting there for about 150 seconds, it goes offline
Update: I've confirmed the DMs is not the issue, it's the posting of a message tagging users.
Got it, I’ll remove the var’s but userQueryValues is declared in the 4th line?
Got it again, thank you, that's updated now too
There's the two updates as per Qjuh's help! The issue remains though, after sending the DMs and message to the vote channel, the bot goes offline with no error. It just hangs. the node process actually doesn't close, I have to CTRL+C to close the process
If you aren't getting any errors, try to place
console.log
checkpoints throughout your code to find out where execution stops.
- Once you do, log relevant values and if-conditions
- More sophisticated debugging methods are breakpoints and runtime inspections: learn moreThank you again. On it.
Alright, strict mode on and variables all declared, but the same thing is happening.
Another update, the bot goes offline, but comes back after 15-20 minutes of downtime? Here is what the console shows:
[WS => Shard 0] Destroying shard Reason: Got disconnected by Discord Code: 1000 Recover: Reconnect [WS => Shard 0] Connection status during destroy Needs closing: false Ready state: 3 [WS => Shard 0] Connecting to wss://gateway.discord.gg?v=10&encoding=json [WS => Shard 0] Waiting for event hello for 60000ms [WS => Shard 0] Preparing first heartbeat of the connection with a jitter of 0.2764877814090323; waiting 11405ms [WS => Shard 0] Waiting for identify throttle [WS => Shard 0] Identifying shard id: 0 shard count: 1 intents: 38403 compression: noneThat's what happens when it comes back online. There's nothing in the console. No activity at all for that 15 minute window. Sorry, let me get a link with all the code. I've just been trying to step through everything to see where it stops, and it looks like it finishes the message.create event (which is when the last person signals they're ready) and then nothing until this reconnect message after 15-20 minutes BTW thank you so much for your patience and help as I stumble through all this Here's the full file: draebot.js - https://sourceb.in/qfLtkyu6xR voteCommands.js -https://sourceb.in/nEaiLuV0sj db.js - https://sourceb.in/LbBNNRrNXX (as a precursor I haven't strict-moded the other two files, just the votecommands)
================ Rechecking for active channels (Tuesday, Nov 14, 2023, 10:15 AM) ================ ================ Rechecking for active channels (Tuesday, Nov 14, 2023, 10:15 AM) ================ pod-vote-coordination(1110947498685649046) [voteWhosMissing] Sending 3rd who's missing [isvoteReady] Checking if lci is ready! [voteReady] sending query...SELECT * FROM vote WHERE format = $1; Readycheck result: false 1/2 Got: false from isvoteReady function Nope, skipping bot message [voteWhosMissing] Sending 2nd who's missing [isvoteReady] Checking if lci is ready! [voteReady] sending query...SELECT * FROM vote WHERE format = $1; Readycheck result: false 1/2 Got: false from isvoteReady function Nope, skipping bot message [WS => Shard 0] Heartbeat acknowledged, latency of 34ms. ================ Rechecking for active channels (Tuesday, Nov 14, 2023, 10:16 AM) ================ ================ Rechecking for active channels (Tuesday, Nov 14, 2023, 10:16 AM) ================ pod-vote-coordination(1110947498685649046) [WS => Shard 0] Heartbeat acknowledged, latency of 28ms. [isvoteReady] Checking if lci is ready! [voteReady] sending query...SELECT * FROM vote WHERE format = $1; Readycheck result: false 1/2 [RemoveUsers]Sending query DELETE FROM vote WHERE format = $1 and status != 'ready' with lci [votestatusMessage]Sending query..." SELECT setcode, (SELECT CASE WHEN EXISTS ( SELECT COUNT(discordname) FROM vote WHERE lower(format) = lower(dr.setcode)) THEN (SELECT COUNT(discordname) FROM vote WHERE lower(format) = lower(dr.setcode)) ELSE 0 End as queuecount UNION ALL SELECT 0 WHERE NOT EXISTS (SELECT COUNT(discordname) FROM vote WHERE lower(format) = lower(dr.setcode)) ) FROM voteformat dr WHERE active = true with undefined Nope, skipping bot message<here's where the pause occurs>
================ Rechecking for active channels (Tuesday, Nov 14, 2023, 10:31 AM) ================ ================ Rechecking for active channels (Tuesday, Nov 14, 2023, 10:31 AM) ================ Hey! 🔥-pod-vote-coordination(1110947498685649046) is no longer active! Readycheck result: false 1/2 Got: false from isvoteReady function [WS => Shard 0] Destroying shard Reason: Got disconnected by Discord Code: 1000 Recover: Reconnect [WS => Shard 0] Connection status during destroy Needs closing: false Ready state: 3 [WS => Shard 0] Connecting to wss://gateway.discord.gg?v=10&encoding=json [WS => Shard 0] Waiting for event hello for 60000ms [WS => Shard 0] Preparing first heartbeat of the connection with a jitter of 0.5097057277951755; waiting 21025ms [WS => Shard 0] Waiting for identify throttle [WS => Shard 0] Identifying shard id: 0 shard count: 1 intents: 38403 compression: none [WS => Shard 0] Waiting for event ready for 15000ms [WS => Shard 0] Shard received all its guilds. Marking as fully ready. Nope, skipping bot messageWill do! No change in the output in the console. Same 15ish minute pause before it comes back online. Okay, this is blowing my mind a little bit, but it's working now, with one caveat. The places where the bot got disconnected, it was because of the content of the message? For example, the clearing users not ready part. It started working when I switched: to The same thing with the voters not ready count down (the 5 minute timers) I switched: to: and it all works. I have no idea why the message content is breaking it. I can reproduce it every time now, in the code above. But I start my bot in production with pm2, my dev bot is just running in a command line instance local to my pc. There weren’t any errors in the pm2 logs either. Is there something I’m doing wrong with just sending a message to a channel? I still don’t understand why sending a simple few words works fine, but the other messages don’t. What should I be looking out for there then? Everything I'm putting in the message variable is a string. Why would one string cause the bot to crash, but not the other? voteStatus is the return from a query to the postgres db. and voteStatus.usersNotReady is an array of usernames .format is also in the query. I don't know what you want me to give you, tbh. I'm sorry for not answering the questions better? That's the whole function, it gets called every minute, checking the queue in the database to see if the number of voters has been met That all said, every minute, this function is called: if I send message as is, it hangs and disconnects if I change nothing else in the code, other than: if works fine. So I don't understand what the difference is. I tested each of the variables I'm putting in the message and they're all strings, with values. So, I'd like to hopefully restate the problem with more information, and see if it helps to clarify where I'm at. What reason can anyone think of, that this would work: and this would post the message in the channel, then the bot would go offline. Just to assuage fears of values being empty, unassigned or anything else, here is the log of the message variable (in the case where the message fails.
Message variable type string: Message content "LCI draft (3 minutes remaining) - 2 drafters not ready sarkhan6590 draechen"The underlying code is the exact same thing for both messages, one works, and one does not. Thank you for all the help, I've come across a workaround by converting the message into an embed, that works for what I need, and I'll just chalk this up to ... ghosts or something. I sincerely appreciate all the time and effort of everyone giving me a helping hand.