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).
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.
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) }