You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于C++ DPP库的Discord机器人Embed消息发送故障求助

问题:DPP机器人/version命令无法返回Embed消息回复

我用C++的DPP库开发Discord机器人,执行/version命令时收不到预设的Embed嵌入消息回复。试过event.reply(dpp::interaction_response_type::EMBED, embed);和event.reply(embed);两种方式都不行,以下是完整代码:

#include <dpp/dpp.h>

const std::string BOT_TOKEN = "--------";

int main() {
    dpp::cluster bot(BOT_TOKEN);

    bot.on_log(dpp::utility::cout_logger());

    bot.on_slashcommand([](const dpp::slashcommand_t& event) {
        if (event.command.get_command_name() == "ping") {
            event.reply("Pong!");
        }
        // Check if the command name is "review"
        else if (event.command.get_command_name() == "review") {
            // Respond with the review message
            event.reply("Ai uitat să lași un review? Urât din partea ta, apasa repede la http://distopia.mywebcommunity.org/review.php și postează un review ;)");
        }
        // Check if the command name is "website"
        else if (event.command.get_command_name() == "website") {
            // Respond with the website message
            event.reply("Vrei să inviți un prieten, uite un mic link http://distopia.mywebcommunity.org/ pentru a face o mică prezentare la server:))");
        }
        // Check if the command name is "version"
        else if (event.command.get_command_name() == "version") {
            // Create an embedded message
            dpp::embed embed;
            embed.set_title("Bot and API Distopia Versions");
            embed.set_description("Iată versiunile actuale ale botului și API Distopia:");
            embed.set_color(0x3498db); // BULLSHIT COLOR

            // Add fields to the embedded message
            embed.add_field("Bot Version", "3.0", true); // Inline field
            embed.add_field("API Distopia Version", "Working.. progress..", true); // Inline field
            embed.add_field("Security pach", "Working...", false); // Non-inline field

            // Reply with the embedded message
            event.reply();
        }
    });

    bot.on_ready([&bot](const dpp::ready_t& event) {
        if (dpp::run_once<struct register_bot_commands>()) {
            bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id));
            bot.global_command_create(dpp::slashcommand("review", "Review.", bot.me.id));
            bot.global_command_create(dpp::slashcommand("website", "Website", bot.me.id));
            bot.global_command_create(dpp::slashcommand("version", "Bot Version", bot.me.id));
        }
    });

    bot.start(dpp::st_wait);
}

解决方案

核心问题

你的代码中event.reply();是空调用,没有将创建好的Embed对象传递给回复方法,这是导致没有返回消息的直接原因。

修正后的代码(重点修改version命令部分)

else if (event.command.get_command_name() == "version") {
    // Create an embedded message
    dpp::embed embed;
    embed.set_title("Bot and API Distopia Versions");
    embed.set_description("Iată versiunile actuale ale botului și API Distopia:");
    embed.set_color(0x3498db); // 蓝色

    // Add fields to the embedded message
    embed.add_field("Bot Version", "3.0", true); // 行内字段
    embed.add_field("API Distopia Version", "Working.. progress..", true); // 行内字段
    embed.add_field("Security patch", "Working...", false); // 非行内字段(修正拼写错误:pach→patch)

    // 正确回复方式1:直接传递Embed,DPP自动处理响应类型
    event.reply(embed);

    // 正确回复方式2:显式指定响应类型并传递Embed(效果与方式1一致)
    // event.reply(dpp::interaction_response_type::ir_channel_message_with_source, embed);
}

额外注意事项

  1. 修正了字段拼写错误:Security pach改为Security patch,避免显示错误。
  2. 如果修正后仍无响应,查看控制台日志(cout_logger输出),排查是否存在权限不足、Embed格式不符合Discord规范等问题。
  3. 确保使用最新版本的DPP库,旧版本可能对Embed的API支持存在差异。

内容的提问来源于stack exchange,提问作者Andrei Alexandru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 01:39:57