【问题标题】:I have a problem with my Discord bot (Minecraft server status bot)我的 Discord 机器人(Minecraft 服务器状态机器人)有问题
【发布时间】:2021-04-03 12:42:42
【问题描述】:

这是机器人的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.

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

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

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

    基于错误;

    预期的“选项”是一个对象或未定义,得到数字

    并且,基于this commit,您应该将第二个参数作为对象而不是PORT,因为它是StatusOptions的类型而不是number.

    所以,而不是;

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

    你应该这样做;

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

    【讨论】:

    • 这不起作用,它不会发送任何不和谐的消息,也不会在控制台中显示任何错误,但感谢您的努力。
    【解决方案2】:

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

    【讨论】:

    • 这也确实有效,它不发送任何消息或嵌入到不和谐的服务器上,也不会在控制台中显示任何错误。
    猜你喜欢
    • 2021-07-29
    • 2020-07-04
    • 2021-12-12
    • 1970-01-01
    • 2018-10-10
    • 2018-12-17
    • 1970-01-01
    • 2021-02-09
    • 2020-11-10
    相关资源
    最近更新 更多