Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
discord.js v13 minecraft-server-util assertion error when getting the option of port

I am currently making a Minecraft server checker command using minecraft-server-util but everytime I enter the port number in the port option, the error AssertionError [ERR_ASSERTION]: Expected 'port' to be a 'number', got 'object' comes out. I am currently using the new interaction thing in discord.js v13.

Code:

const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageAttachment } = require('discord.js');
const util = require('minecraft-server-util');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('minecraft')
        .setDescription('Checks the status of a minecraft server.')
        .addStringOption(option => option.setName('address').setDescription('Enter the server address.').setRequired(true))
        .addNumberOption(option => option.setName('port').setDescription('Enter the server port. (25565 IS THE DEFAULT PORT)').setRequired(true)),
    async execute(interaction, client, MessageEmbed) {
        let server = interaction.options.getString('address');
        let serverPort = interaction.options.getNumber('port');

        if(serverPort == NaN) return interaction.reply({ content: 'The server port you provided is not a number.', ephemeral: true });

        util.status(server, { port: parseInt(serverPort)}).then((response) => {
            var fav = response.favicon.split(",").slice(1).join(",");
            var imageStream = Buffer.from(fav, "base64");
            var attachment = new MessageAttachment(imageStream, "favicon.png");
            const embed = new MessageEmbed()
            .setColor('#008000')
            .setTitle('Minecraft Server Status')
            .attachFiles([attachment])
            .addFields(
                {name: 'Server IP', value: response.host},
                {name: 'Online Players', value: response.onlinePlayers},
                {name: 'Player Threshold', value: response.maxPlayers},
                {name: 'MOTD', value: response.motd.clean},
                {name: 'Version', value: response.version},
            )
            .setThumbnail("attachment://favicon.png")
            interaction.reply({ embeds: [embed] })
        })
        .catch ((error) => {
            interaction.reply({ content: 'There was an error finding this server.', ephemeral: true });
            throw error;
        })
    },
};

It still won't work even if I change .addNumberOption into .addStringOption and parse serverPort into an integer using parseInt(serverPort).

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The error is that you pass an object as the second parameter here: util.status(server, { port: serverPort }) while it should be the port number:

return util.status(server, serverPort)

Another thing, serverPort == NaN is always false. Even NaN == NaN is false. You should use isNaN instead:

let serverPort = NaN

console.log(serverPort == NaN)
console.log(serverPort === NaN)
console.log(isNaN(serverPort))

Although I'm not sure if it's ever NaN, as getNumber() returns the value of the option, or null if not set and not required.

about 4 years ago · Juan Pablo Isaza Report

0

Seems like it has something to do with my PC because when I hosted it on Heroku, it seems to have worked. util.status(server, serverPort) seems to work better than { port: parseInt(serverPort) }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!