我的 Discord 机器人(Minecraft 服务器状态机器人)有问题

时间:2020-12-25 14:21:12

标签: node.js discord.js

这是机器人的 index.js 脚本:

const { Client, RichEmbed } = require('discord.js')

const bot = new Client()

const util = require('minecraft-server-util')

const token = 'bot_token'

const PREFIX = 'l.'

bot.on('ready', () => {
    console.log('Bot has come online.')
})

bot.on('message', message => {

    let args = message.content.substring(PREFIX.length).split(' ')

    switch (args[0]) {
        case 'status':

            util.status('IP', (PORT), (error, reponse) => {
                if (error) throw error
                const Embed = new RichEmbed()
                    .setTitle('Server Status')
                    .addField('Server IP', reponse.host)
                    .addField('Server Version', reponse.version)
                    .addField('Online Players', reponse.onlinePlayers)
                    .addField('Max Players', reponse.maxPlayers)

                message.channel.send(Embed)
            })
            break

    }

})

bot.login(token)

当我在控制台中输入 node . 并在 Discord 服务器上输入 l.status 时,它在控制台上显示此错误:

(node:3300) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Expected 'options' to
be an object or undefined, got number
    at C:\Users\HNRK\Desktop\LightSide\node_modules\minecraft-server-util\dist\status.js:46:25
    at Generator.next (<anonymous>)
    at C:\Users\HNRK\Desktop\LightSide\node_modules\minecraft-server-util\dist\status.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\HNRK\Desktop\LightSide\node_modules\minecraft-server-util\dist\status.js:4:12)
    at status (C:\Users\HNRK\Desktop\LightSide\node_modules\minecraft-server-util\dist\status.js:40:12)
    at Object.<anonymous> (C:\Users\HNRK\Desktop\LightSide\node_modules\minecraft-server-util\dist\status.js:113:17)
    at Generator.next (<anonymous>)
    at C:\Users\HNRK\Desktop\LightSide\node_modules\minecraft-server-util\dist\status.js:8:71
    at new Promise (<anonymous>)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3300) 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: 2)
(node:3300) [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.

有人可以帮忙吗?我也尝试了很多不同的解决方案,但都没有奏效。

2 个答案:

答案 0 :(得分:0)

您使用的是过时版本的 discord.js。在终端中输入 npm i discord.js@latest。在最新版本的 discord.js 中,RichEmbed() 已被删除并替换为 MessageEmbed()

答案 1 :(得分:0)

这是基于假设的答案,因为我以前从未使用过 minecraft-server-util。还假设您使用的是最新的 minecraft-server-util,

基于错误;

<块引用>

预期“选项”为对象或未定义,得到编号

并且,基于 this commit,您应该将第二个参数作为对象而不是 PORT 传递,因为它是一种 {{ 3}} 而不是 number

所以,而不是;

util.status('IP', (PORT), (error, reponse) => {

你应该做的;

util.status('IP', {
  port: (PORT)
}, (error, reponse) => {