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

如何使用C++ Game SDK为Discord Rich Presence添加按钮?

如何在Discord Rich Presence中添加按钮

问题描述

我想了解如何为Discord Rich Presence添加按钮,但在Game SDK附带的文件中未找到相关示例。当前使用的C++代码如下:

struct DiscordState
{
   discord::User currentUser;
   std::unique_ptr<discord::Core> core;
};

DiscordState state{};
discord::Core* core;
auto result = discord::Core::Create(..., DiscordCreateFlags_Default, &core);

state.core.reset(core);
if (state.core)
{
   state.core->SetLogHook(discord::LogLevel::Debug, [](discord::LogLevel level, const char* message) {
       qDebug() << "Log(" << static_cast<uint32_t>(level) << "): " << message << "\n";
   });

   core->UserManager().OnCurrentUserUpdate.Connect([&state]() 
   {
       state.core->UserManager().GetCurrentUser(&state.currentUser);
       qDebug() << "Current user updated: " << state.currentUser.GetUsername() << state.currentUser.GetDiscriminator() << "\n";
   });

   discord::Activity activity{};
   activity.SetType(discord::ActivityType::Playing);
   activity.SetState("Playing my game 123");
   activity.SetDetails("In a match");
   
   activity.GetAssets().SetLargeImage("..."); // 从Discord开发者门户获取的大图资源Key
   activity.GetAssets().SetLargeText("...");  // 悬停大图时显示的文本
   activity.GetAssets().SetSmallImage("...");
   activity.GetAssets().SetSmallText("....");
   
   core->ActivityManager().UpdateActivity(activity, [](discord::Result result) 
   {
       if (result == discord::Result::Ok)
           qDebug() << "Successfully updated activity.";
       else
           qDebug() <<  "Failed to update activity.";
   });
}
else
   qDebug() << "Failed to instantiate discord core! (err " << static_cast<int>(result) << ")\n";

QTimer coreTimer;
coreTimer.start(1000);
QObject::connect(&coreTimer, &QTimer::timeout, &a, [&]{ core->RunCallbacks(); });

我想要添加的是Rich Presence卡片下方的跳转按钮,请问这类按钮在Game SDK中是否可用?目前我仅找到针对个人资料而非特定应用的按钮添加示例。

解决方案

这类Rich Presence按钮在Discord Game SDK中是完全支持的,属于应用活动状态的一部分,和个人资料按钮是不同功能。你只需在discord::Activity对象中添加按钮配置即可,具体修改如下:

在设置完Activity的状态、详情、资源后,添加按钮初始化代码:

// 定义按钮数组,最多支持2个按钮
discord::ActivityButton buttons[2];

// 设置第一个按钮的标签和跳转URL
buttons[0].SetLabel("加入游戏");
buttons[0].SetUrl("https://your-game-invite-url.com");

// 设置第二个按钮的标签和跳转URL
buttons[1].SetLabel("查看游戏详情");
buttons[1].SetUrl("https://your-game-official-site.com");

// 将按钮数组绑定到Activity对象
activity.GetButtons().Set(buttons, 2);

更新后的完整Activity配置代码如下:

discord::Activity activity{};
activity.SetType(discord::ActivityType::Playing);
activity.SetState("Playing my game 123");
activity.SetDetails("In a match");

activity.GetAssets().SetLargeImage("...");
activity.GetAssets().SetLargeText("...");
activity.GetAssets().SetSmallImage("...");
activity.GetAssets().SetSmallText("....");

// 添加Rich Presence按钮
discord::ActivityButton buttons[2];
buttons[0].SetLabel("加入游戏");
buttons[0].SetUrl("https://your-game-invite-url.com");
buttons[1].SetLabel("查看游戏详情");
buttons[1].SetUrl("https://your-game-official-site.com");
activity.GetButtons().Set(buttons, 2);

core->ActivityManager().UpdateActivity(activity, [](discord::Result result) 
{
    if (result == discord::Result::Ok)
        qDebug() << "Successfully updated activity with buttons.";
    else
        qDebug() <<  "Failed to update activity.";
});

注意事项

  • Rich Presence最多支持添加2个按钮,超出数量会被Discord忽略
  • 按钮的URL必须是有效的HTTP/HTTPS链接,且符合Discord内容规范
  • 按钮标签文本长度有限制(通常不超过32字符),过长会被自动截断

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 00:22:47