【问题标题】:Can i make a return with messageEmbed?我可以使用 messageEmbed 退货吗?
【发布时间】:2025-12-21 15:05:07
【问题描述】:

我正在尝试创建一个 Discord 机器人。当有人键入! status 命令时,机器人应该传达服务器状态。我的代码基于this bot

我所做的唯一修改是让机器人立即响应 ! status 命令而不询问 IP。

这是我的代码:

const Discord = require('discord.js');
const client = new Discord.Client();
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

function commandIs(str, msg){
    return msg.content.toLowerCase().startsWith("!" + str);
}

function status(callback) {
    var ourRequest = new XMLHttpRequest();
    ourRequest.open('GET', 'https://mcapi.us/server/status?ip=chocolatada.serveftp.com', true);
      ourRequest.onload = () => {
        var ourData = JSON.parse(ourRequest.responseText);
        callback(null, checkStatus(ourData));
    };
    ourRequest.onerror = function() {
        console.error(ourRequest.statusText);
    };
    ourRequest.send();
}

function checkStatus(data){
    if(data.online){
        return "Chococraft esta online!, actualmente hay: " + data.players.now + " /" + data.players.max + " " + "La version del server es: " + data.server.name;
    } else {
        return "server offline";
    }
}

client.on('message', message => {
    var args = message.content.split(/[ ]+/);
    if(commandIs("hello", message)){
        message.reply('Hello there');
    }
    if(commandIs("status", message)){
        if(args.length === 2){
            message.channel.sendMessage('Por favor, utiliza `!status`');
        } else if (args.length === 1){
            status((error, result) => {
                if (error) {
                    message.channel.sendMessage("error!");
                    return;
                }
            message.channel.sendMessage(result);
        }, args[1]);
        } else {
            message.channel.sendMessage('You defined too many arguments. Usage: `!status [ip]`');
        }
    }
});

client.on('ready', () => {
    console.log("this bot is online")
    console.log('Bot: Hosting ' + `${client.users.size}` + ' users, in ' + `${client.channels.size}` + ' channels of ' + `${client.guilds.size}` + ' guilds.');
    });

client.login('token');

返回前我放const embed = new Discord.MessageEmbed()和同一个在线播放器和服务器数据,然后返回后我放了embed,控制台给了我一个错误的请求。 (我没有把所有的数据我都放了测试)

function checkStatus(data){
    if(data.online){
        const embed = new Discord.MessageEmbed()
        .addDescription(data.players)
        return message.channel.send(embed);
    } else {
        return "server offline";
    }
}

我现在遇到的问题是语法错误。

使用richembed 我尝试了以下方法:

function checkStatus(data){
    if(data.online){
        const embed = new Discord.RichEmbed()
        .addDescription('data.players')
        return message.channel.send(embed);
    } else {
        return "server offline";
    }
}

控制台显示“TypeError: (intermediate value).addDescription is not a function 在 checkStatus (D:\Documents\Chococraft\Bots\bot status pruebas\Status\bot.js:25:10)"

我尝试使用 .setdescription 并且控制台显示“ReferenceError:未定义消息 在 checkStatus (D:\Documents\Chococraft\Bots\bot status pruebas\Status\bot.js:26:9) 在exports.XMLHttpRequest.ourRequest.onload (D:\Documents\Chococraft\Bots\bot status pruebas\Status\bot.js:14:24)"

function checkStatus(data){
    if(data.online){
        const embed = new Discord.RichEmbed()
        .setDescription('data.players')
        return message.channel.send(embed);
    } else {
        return "server offline";
    }
}

【问题讨论】:

  • 语法错误在哪里?
  • @Pepe_W​​orm TypeError: Cannot read property 'client' of undefined
  • 您能否告诉我们错误在哪一行,以便答案可以查明哪个语句有问题?
  • 你运行npm install discord.js了吗?如果还没有,请尝试这样做,看看会发生什么。
  • 我试过了,它给了我上面评论的错误

标签: javascript node.js discord.js


【解决方案1】:

当您使用 Discord 版本 11 时,您正在使用 MessageEmbed,其中构建嵌入只能通过使用 RichEmbed 进行。 MessageEmbed 用于接收嵌入,RichEmbed 用于制作它们。

解决方案,使用 RichEmbed() 代替 MessageEmbed()。

希望我能帮上忙!

【讨论】:

  • 当我尝试这个时,控制台显示“TypeError: (intermediate value).addDescription is not a function”
  • 我尝试使用 .setdescription 并且控制台显示“ReferenceError: 消息未在 checkStatus 中定义 (D:\Documents\Chococraft\Bots\bot status pruebas\Status\bot.js:26:9)在exports.XMLHttpRequest.ourRequest.onload (D:\Documents\Chococraft\Bots\bot status pruebas\Status\bot.js:14:24)"
  • 您可能还希望将消息作为参数传递,如错误所示,函数中未定义消息。另外,我相信你应该学习如何编码,而不是被我或其他任何人讥讽,所以这是我的帮助方式。