Home > Software design >  Expected 'port' to be a 'number', got 'object', minecraft server statu
Expected 'port' to be a 'number', got 'object', minecraft server statu

Time:01-05

i have this simple code in my discord bot to check mc server.

const Discord = require('discord.js')
const client = new Discord.Client()
const { MessageButton, MessageButtonStyles } = require('discord-buttons')
require('discord-buttons')(client)
const db = require('quick.db')
let mineutil = require('minecraft-server-util')

client.on('ready', () => {
  console.log('Started!\n---')
  client.user.setPresence({
    status: 'online',
    activity: {
      type: 'LISTENING',
      name: '!help'
    }
  })
})

client.on('message', async (message) => {
  if (message.content == 'привет') {
    message.reply('привет')   
  }

//more code

  const SERVER_ADDRESS = 'adress'
  const SERVER_PORT = 25565
  const STATUS_ONLINE = '**Сервер включен**  -  '
  const STATUS_PLAYERS = '**{online}** **человек(a) онлайн!**'
  const STATUS_EMPTY = '**никто не играет**'
  const cacheTime = 15 * 1000; // 15 sec cache time
  let data, lastUpdated = 0;

  function statusCommand(message) {
    getStatus().then(data => {
      let status = STATUS_ONLINE;
      status  = data.onlinePlayers ?
        STATUS_PLAYERS.replace('{online}', data.onlinePlayers) : STATUS_EMPTY;
      let statuspanel = new Discord.MessageEmbed()
        .setColor('2ecc71')
        .setDescription(status)
      send(statuspanel)
    }).catch(err => {
      console.error(err)
      let statuserror = new Discord.MessageEmbed()
        .setColor('ff0000')
        .setDescription('**Сервер выключен**')
      send(statuserror)
    })
  }
  function getStatus() {
    if (Date.now() < lastUpdated   cacheTime) return Promise.resolve(data);
    return mineutil.status(SERVER_ADDRESS, { port: SERVER_PORT })
      .then(res => {
        data = res;
        lastUpdated = Date.now();
        return data;
      })
  }

  if (message.content == '!server') {
    statusCommand(message)
  }

})

client.login(TOKEN)

And it works in Visual studio, but i just placed it on Replit and it catches this error:

(node:172) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Expected 'port' to be a 'number', got 'object'
    at Object.status (/home/runner/Makak-discord-bot/node_modules/minecraft-server-util/dist/status.js:23:26)
    at getStatus (/home/runner/Makak-discord-bot/index.js:210:21)
    at statusCommand (/home/runner/Makak-discord-bot/index.js:192:5)
    at Client.<anonymous> (/home/runner/Makak-discord-bot/index.js:219:5)
    at Client.emit (events.js:314:20)
    at Client.EventEmitter.emit (domain.js:483:12)
    at MessageCreateAction.handle (/home/runner/Makak-discord-bot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/Makak-discord-bot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/Makak-discord-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/Makak-discord-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
(node:172) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:172) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

And if i change, for example { port: SERVER_PORT } to 25565, it always says that server is off, even if server online(

PS sorry for my english, and russian text in code

CodePudding user response:

EDIT Just saw the question's last line about the server being reported as offline when using a number rather than an object. That actually confirms my suspicion below, as you're no longer getting an error from the SDK itself (ie, it seems to be "working," in that it's at least making the network call). I would double-check your address and port number, and ensure the server is accessible from replit.

--- Original response below ---

Difficult to say for sure without knowing the mineutil API you're using, but it looks like you may be sending more than you need to the mineutil.status() function (And if you're using this library, I'm fairly certain you are).

I'm guessing that the following line:

return mineutil.status(SERVER_ADDRESS, { port: SERVER_PORT })

which is sending an object `{port: SERVER_PORT}' as its second parameter, should just be sending the number itself. For example:

return mineutil.status(SERVER_ADDRESS, SERVER_PORT )

CodePudding user response:

i just renamed mineutil to util and its working, i dont know sourse of this problem but now we know.

Working code:

const Discord = require('discord.js')
const client = new Discord.Client()
const { MessageButton, MessageButtonStyles } = require('discord-buttons')
require('discord-buttons')(client)
//const db = require('quick.db')
let util = require('minecraft-server-util')

client.on('ready', () => {
  console.log('Started!\n---')
  client.user.setPresence({
    status: 'online',
    activity: {
      type: 'LISTENING',
      name: '!help'
    }
  })
})

client.on('message', async (message) => {
  if (message.content == 'привет') {
    message.reply('привет')   
  }

//more code

  const SERVER_ADDRESS = 'adress'
  const SERVER_PORT = 25565
  const STATUS_ONLINE = '**Сервер включен**  -  '
  const STATUS_PLAYERS = '**{online}** **человек(a) онлайн!**'
  const STATUS_EMPTY = '**никто не играет**'
  const cacheTime = 15 * 1000; // 15 sec cache time
  let data, lastUpdated = 0;

  function statusCommand(message) {
    getStatus().then(data => {
      let status = STATUS_ONLINE;
      status  = data.onlinePlayers ?
        STATUS_PLAYERS.replace('{online}', data.onlinePlayers) : STATUS_EMPTY;
      let statuspanel = new Discord.MessageEmbed()
        .setColor('2ecc71')
        .setDescription(status)
      send(statuspanel)
    }).catch(err => {
      console.error(err)
      let statuserror = new Discord.MessageEmbed()
        .setColor('ff0000')
        .setDescription('**Сервер выключен**')
      send(statuserror)
    })
  }
  function getStatus() {
    if (Date.now() < lastUpdated   cacheTime) return Promise.resolve(data);
    return util.status(SERVER_ADDRESS, { port: SERVER_PORT })
      .then(res => {
        data = res;
        lastUpdated = Date.now();
        return data;
      })
  }

  if (message.content == '!server') {
    statusCommand(message)
  }

})

client.login(TOKEN)
  •  Tags:  
  • Related