如何基于现有C++代码为Discord RPC添加自定义按钮?
给Discord Rich Presence添加按钮的实现方案
你的原始代码
#include "CDiscord.h" using namespace std; static int64_t eptime = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count(); void Start() { DiscordEventHandlers Handle; memset(&Handle, 0, sizeof(Handle)); Discord_Initialize("1334300888808702279", &Handle, 1, NULL); DiscordRichPresence discordPresence; memset(&discordPresence, 0, sizeof(discordPresence)); discordPresence.details = "Game Link: scorpion.ppp"; discordPresence.state = "Classic Server"; discordPresence.startTimestamp = eptime; discordPresence.largeImageKey = "1334300888808702279";// large img discordPresence.largeImageText = "SCORPIONS"; // txt large img //discordPresence.smallImageKey = "8080"; // small img //discordPresence.smallImageText = "scorpion"; // txt small img Discord_UpdatePresence(&discordPresence); } void CDiscord::Shutdown() { Discord_Shutdown(); } void CDiscord::Init() { CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)Start, NULL, NULL, NULL); }
修改后的代码(已添加按钮)
#include "CDiscord.h" using namespace std; static int64_t eptime = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count(); void Start() { DiscordEventHandlers Handle; memset(&Handle, 0, sizeof(Handle)); Discord_Initialize("1334300888808702279", &Handle, 1, NULL); DiscordRichPresence discordPresence; memset(&discordPresence, 0, sizeof(discordPresence)); discordPresence.details = "Game Link: scorpion.ppp"; discordPresence.state = "Classic Server"; discordPresence.startTimestamp = eptime; discordPresence.largeImageKey = "1334300888808702279";// large img discordPresence.largeImageText = "SCORPIONS"; // txt large img //discordPresence.smallImageKey = "8080"; // small img //discordPresence.smallImageText = "scorpion"; // txt small img // 配置两个按钮,最多支持2个 discordPresence.buttons[0].label = "进入游戏网页"; discordPresence.buttons[0].url = "https://你的游戏网页地址"; // 替换成实际的游戏网页链接 discordPresence.buttons[1].label = "关注Discord服务器"; discordPresence.buttons[1].url = "https://discord.gg/你的服务器邀请链接"; // 替换成实际的Discord服务器永久邀请链接 Discord_UpdatePresence(&discordPresence); } void CDiscord::Shutdown() { Discord_Shutdown(); } void CDiscord::Init() { CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)Start, NULL, NULL, NULL); }
注意事项
- 按钮最多只能添加2个,超过的部分Discord会直接忽略
- 按钮的
url必须是有效的HTTPS链接,HTTP或者本地路径都不被支持 - 按钮的
label文字尽量控制在32字符以内,太长会被Discord截断显示 - Discord服务器邀请链接要使用永久有效链接,临时链接过期后按钮点击会失效
- 确保你的Discord应用在开发者门户中已经正确配置了Rich Presence相关权限,否则按钮可能无法正常显示
内容的提问来源于stack exchange,提问作者Kiopeed Game
相关产品推荐
相关产品推荐

