Discord bot fails to display server information

Hello everyone! I’m trying to create a Discord bot that shows details about my Minecraft server when a specific command is issued. Unfortunately, I keep encountering an error that I can’t seem to resolve.

const { Client, MessageEmbed } = require('discord.js');
const serverUtil = require('minecraft-server-util');

const myBot = new Client();
const TOKEN = 'your_token_here';
const COMMAND_PREFIX = '!';

myBot.on('ready', () => {
    console.log('The bot is online.');
});

myBot.on('message', (msg) => {
    if (msg.content === 'check') {
        serverUtil.status('your.server.ip', 25565)
            .then((response) => {
                const embedMessage = new MessageEmbed()
                    .setTitle('Server Info')
                    .addField('IP Address', response.host)
                    .addField('Version', response.version)
                    .addField('Online Players', response.players.online)
                    .addField('Max Players', response.players.max);
                
                msg.channel.send(embedMessage);
            })
            .catch((error) => {
                console.error('Error fetching server info:', error);
                msg.channel.send('Error fetching server details. Sorry!');
            });
    }
});

myBot.login(TOKEN);

The error I’m seeing seems to involve constructors and unhandled promise rejections. I’m relatively new to both JavaScript and Discord.js, so any guidance would be greatly appreciated. Thank you!

hey jack, looks like ur missing the command prefix in your check. you wrote msg.content === 'check' but it should be msg.content === '!check' since you defined the prefix as ‘!’. also try updating your discord.js version cause MessageEmbed is deprecated now - use EmbedBuilder instead. that constructor error is probably from version missmatch.

Looking at your code, there’s a couple of things that might be causing the constructor errors. First, make sure you’re initializing your Client with the proper intents - without them, the message events won’t fire properly. You should be using something like new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }) if you’re on a recent discord.js version. The unhandled promise rejection is probably coming from the minecraft-server-util call. I’ve found that library can be pretty finicky with timeouts and connection issues. Try adding a timeout option to your status call like serverUtil.status('your.server.ip', 25565, { timeout: 5000 }). Also double-check that your server IP and port are correct - even a small typo will cause the promise to reject. One more thing - your command check is looking for exactly ‘check’ but you probably want it to be ‘!check’ based on your prefix constant.

I ran into similar issues when I started working with Discord bots. The problem is likely that you’re using an outdated version of discord.js. The MessageEmbed constructor and the way messages are sent has changed significantly in newer versions.

If you’re using discord.js v14, you need to import EmbedBuilder instead of MessageEmbed and use { embeds: [embedMessage] } when sending. Also make sure your bot has the proper intents enabled in the client constructor - you’ll need GatewayIntentBits.GuildMessages and GatewayIntentBits.MessageContent for message events to work.

Another thing to check is whether your Minecraft server is actually reachable. I’ve noticed that some hosting providers block the ports that minecraft-server-util tries to use for status queries. Try testing the server connection separately first to rule out network issues.