Hey guys, I’m working on a Discord bot to check my Minecraft server status. It works fine when the server is up, but I’m getting a weird error when it’s offline. Here’s what pops up:
TypeError: Cannot read property 'substr' of null
at Client. (C:\Users\Me\Desktop\BotProject\index.js:67:83)
I think it’s happening because the bot can’t get the server info when it’s down. I’ve tried wrapping it in a try-catch, but no luck. Anyone know how to fix this? Maybe I need to add a check if the server is offline before trying to get the favicon?
Here’s a snippet of my code that might be causing the issue:
As someone who’s dealt with similar Minecraft server status bots, I can relate to your frustration. The ‘substr’ error usually pops up when you’re trying to access properties of null objects. In your case, it’s likely happening because the server data isn’t available when it’s offline.
Here’s a trick I’ve used that might help:
Instead of assuming the server data will always be there, add some checks. Before accessing any property, verify if it exists. You could modify your updateServerInfo function like this:
I’ve encountered similar issues with Minecraft server status bots. The problem likely stems from assuming the server data structure is always complete. To fix this, you should implement more robust error handling and null checks throughout your code.
Try modifying your updateServerInfo function to handle potential null values: