我的Discord机器人在显示服务器时出错

时间:2020-06-24 09:30:30

标签: javascript node.js discord.js

你好,我希望我的discordbot显示我的mc服务器

我尝试以下代码:

const {Client, RichEmbed } = require('discord.js')
 
const bot = new Client()
 
const ping = require('minecraft-server-util')
 
const token = 'tokenid'
 
const PREFIX = '!'
 
bot.on('ready', () =>{
    console.log('Bot has come online.')
})
 
bot.on('message', message =>{
if (message.content === 'dnm') {

ping('hypixel.net', 25565, (error, reponse) =>{
    if(error) throw error
    const Embed = new Discord.MessageEmbed()
    .setTitle('Sunucu Bilgileri')
    .addField('Sunucu ip', reponse.host)
    .addField('Sunucu Versiyonu', reponse.version)
    .addField('online oyuncu', reponse.onlinePlayers)
    .addField('Maksimun Oyuncu', reponse.maxPlayers)

    message.channel.send(Embed)
    })
}     
    }
 
})
 
bot.login(token)

但是我得到这个错误

**

(节点:13980)UnhandledPromiseRejectionWarning:TypeError:RichEmbed是 不是构造函数enter code here 在D:\ Discordbot \ MCBOT \ bas.js:27:31 在D:\ Discordbot \ MCBOT \ node_modules \ minecraft-server-util \ src \ index.js:137:23 在processTicksAndRejections(内部/进程/task_queues.js:97:5)(节点:13980) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个 由抛出异步函数引起的错误 没有障碍,或者拒绝没有处理的承诺 使用.catch()。根据未处理的诺言终止节点进程 拒绝,请使用CLI标志--unhandled-rejections=strict(请参见 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝ID:1)(节点:13980)[DEP0018] DeprecationWarning:未处理 答应拒绝已弃用。将来,承诺会被拒绝 未处理的将终止Node.js进程 非零退出代码。

**

1 个答案:

答案 0 :(得分:1)

在Discord.js v12上,RichEmbed被删除,而被MessageEmbed取代。几乎所有您需要做的就是在您的代码中的任何地方都将RichEmbed替换为MessageEmbed,就像我在下面这样:

const {Client, MessageEmbed} = require('discord.js')
 
const bot = new Client()
 
const ping = require('minecraft-server-util')
 
const token = 'tokenid'
 
const PREFIX = '!'
 
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 'mc':
 
            if(!args[1]) return message.channel.send('You must type a minecraft server ip')
            if(!args[2]) return message.channel.send('You must type a minecraft server port')
 
            ping(args[1], parseInt(args[2]), (error, reponse) =>{
                if(error) throw error
                const Embed = new MessageEmbed()
                .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)

我希望这会有所帮助!