FiveM Server Status Bot for Discord

Hey everyone! I’m trying to set up a Discord bot that shows FiveM server status, but I’m running into an error. Here’s what I’ve got so far:

const Discord = require('discord.js');
const bot = new Discord.Client();
const settings = require('./settings.json');
const serverQuery = require('gameserver-query');

bot.login(settings.token).then(() => {
  console.log('Bot online!');
  updateStatus();
  updateActivity();
});

function updateStatus() {
  serverQuery.query(settings.serverAddress, settings.serverPort, (error, data) => {
    if (error) {
      console.error('Query failed:', error);
    } else {
      const guild = bot.guilds.cache.get('123456789012345678');
      const statusChannel = guild.channels.cache.get('987654321098765432');
      statusChannel.setName(`Players: ${data.players}/${data.maxPlayers}`);
    }
    setTimeout(updateStatus, 10000);
  });
}

function updateActivity() {
  serverQuery.query(settings.serverAddress, settings.serverPort, (error, data) => {
    if (error) {
      console.error('Activity update failed:', error);
    } else {
      bot.user.setActivity(`${data.players}/${data.maxPlayers} online`, { type: settings.activityType });
    }
    setTimeout(updateActivity, 10000);
  });
}

The error I’m getting is:

TypeError: Cannot read property 'memberCount' of undefined

Any ideas what might be causing this? Thanks for your help!

I’ve dealt with similar issues when setting up FiveM status bots. The error you’re seeing usually pops up when the bot can’t find the guild or channel you’re trying to access. Double-check that your guild and channel IDs are correct in the settings file. Also, make sure your bot has actually joined the server and has the right permissions.

One thing I’ve found helpful is to add some logging to see what’s happening. Try adding console.log statements before accessing the guild and channel, like this:

console.log(‘Bot guilds:’, bot.guilds.cache.size);
console.log(‘Target guild found:’, bot.guilds.cache.has(‘123456789012345678’));

This will help you see if the bot is in the right place. If these logs show everything’s fine, the issue might be with the gameserver-query package. I’ve had better luck using the fivem-api package for querying FiveM servers. It’s more reliable and easier to use in my experience.

Hope this helps you troubleshoot the problem!

Looking at your code, I don’t see any reference to ‘memberCount’, which is odd given the error you’re encountering. This suggests the issue might be in a part of the code you haven’t shared. However, I can spot a potential problem in your updateStatus function.

You’re assuming the guild and channel IDs are valid and accessible. If either of these isn’t found, you’ll get an ‘undefined’ error when trying to access properties. To fix this, add some error checking:

function updateStatus() {
  serverQuery.query(settings.serverAddress, settings.serverPort, (error, data) => {
    if (error) {
      console.error('Query failed:', error);
    } else {
      const guild = bot.guilds.cache.get('123456789012345678');
      if (!guild) {
        console.error('Guild not found');
        return;
      }
      const statusChannel = guild.channels.cache.get('987654321098765432');
      if (!statusChannel) {
        console.error('Status channel not found');
        return;
      }
      statusChannel.setName(`Players: ${data.players}/${data.maxPlayers}`);
    }
    setTimeout(updateStatus, 10000);
  });
}

This should help pinpoint where the issue is occurring. Also, ensure your bot has the necessary permissions to change channel names.

yo, i had similar probs with my fivem bot. Try console.logging the guild and channel before u use em. like:

console.log(‘Guild:’, bot.guilds.cache.get(‘123456789012345678’));
console.log(‘Channel:’, guild.channels.cache.get(‘987654321098765432’));

this way u can see if theyre undefined. also, make sure ur bot token is correct in settings.json. good luck man!