My Minecraft server status Discord bot is throwing an error when the server is offline

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:

const updateServerInfo = async () => {
  const serverData = await fetchServerStatus(config.serverIP);
  if (!serverData) {
    return setChannelName(config.statusChannel, '🔴 Server Offline');
  }
  
  const playerCount = serverData.players.online;
  const status = serverData.online ? '🟢 Online' : '🔴 Offline';
  
  setChannelName(config.playerChannel, `👥 Players: ${playerCount}`);
  setChannelName(config.statusChannel, `Status: ${status}`);
}

Any help would be awesome! Thanks!

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:

const updateServerInfo = async () => {
  try {
    const serverData = await fetchServerStatus(config.serverIP);
    const status = serverData && serverData.online ? '🟢 Online' : '🔴 Offline';
    const playerCount = serverData && serverData.players ? serverData.players.online : 'N/A';

    setChannelName(config.statusChannel, `Status: ${status}`);
    setChannelName(config.playerChannel, `👥 Players: ${playerCount}`);
  } catch (error) {
    console.error('Failed to update server info:', error);
    setChannelName(config.statusChannel, '🔴 Server Offline');
  }
}

This approach should handle both online and offline scenarios more gracefully. Give it a shot and let us know if it helps!

hey mia, i’ve run into this before. the problem’s probably cuz ur trying to get info that isn’t there when the server’s down. here’s a quick fix:

const updateServerInfo = async () => {
  try {
    const serverData = await fetchServerStatus(config.serverIP);
    const status = serverData?.online ? '🟢 Online' : '🔴 Offline';
    const playerCount = serverData?.players?.online ?? 'N/A';

    setChannelName(config.statusChannel, `Status: ${status}`);
    setChannelName(config.playerChannel, `👥 Players: ${playerCount}`);
  } catch (err) {
    setChannelName(config.statusChannel, '🔴 Server Offline');
  }
}

tthis should handle both online and offline cases. good luck!

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:

const updateServerInfo = async () => {
  try {
    const serverData = await fetchServerStatus(config.serverIP);
    const status = serverData && serverData.online ? '🟢 Online' : '🔴 Offline';
    const playerCount = serverData && serverData.players ? serverData.players.online : 0;

    setChannelName(config.statusChannel, `Status: ${status}`);
    setChannelName(config.playerChannel, `👥 Players: ${playerCount}`);
  } catch (error) {
    console.error('Error updating server info:', error);
    setChannelName(config.statusChannel, '🔴 Server Offline');
  }
}

This approach should prevent the ‘substr’ error and handle offline scenarios more gracefully.