Last active
July 6, 2022 16:26
-
-
Save Jimbly/f08906d843687a8f34bec2883156ab59 to your computer and use it in GitHub Desktop.
Testing discord SDK rich presence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS 1 | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <assert.h> | |
#include "discord_game_sdk.h" | |
#ifdef _WIN32 | |
#include <Windows.h> | |
#else | |
#include <unistd.h> | |
#include <string.h> | |
#endif | |
#define APP_ID 418559331265675294 | |
#define DISCORD_REQUIRE(x) assert(x == DiscordResult_Ok) | |
#if defined (_WIN64) | |
#pragma comment(lib, "../lib/x86_64/discord_game_sdk.dll.lib") | |
#else | |
#pragma comment(lib, "../lib/x86/discord_game_sdk.dll.lib") | |
#endif | |
struct Application { | |
struct IDiscordCore* core; | |
struct IDiscordUserManager* users; | |
struct IDiscordActivityManager* activities; | |
struct IDiscordRelationshipManager* relationships; | |
DiscordUserId user_id; | |
}; | |
void UpdateActivityCallback(void* data, enum EDiscordResult result) | |
{ | |
DISCORD_REQUIRE(result); | |
} | |
bool RelationshipPassFilter(void* data, struct DiscordRelationship* relationship) | |
{ | |
return (relationship->type == DiscordRelationshipType_Friend); | |
} | |
void OnRelationshipsRefresh(void* data) | |
{ | |
struct Application* app = (struct Application*)data; | |
struct IDiscordRelationshipManager* module = app->relationships; | |
module->filter(module, app, RelationshipPassFilter); | |
int32_t count = 0; | |
DISCORD_REQUIRE(module->count(module, &count)); | |
printf("=== Friends ===\n"); | |
for (int32_t i = 0; i < count; i += 1) { | |
struct DiscordRelationship relationship; | |
DISCORD_REQUIRE(module->get_at(module, i, &relationship)); | |
printf("%lld %s#%s : %s\n", | |
relationship.user.id, | |
relationship.user.username, | |
relationship.user.discriminator, | |
relationship.presence.activity.name); | |
} | |
} | |
void OnRelationshipsUpdate(void *data, struct DiscordRelationship *relationship) | |
{ | |
OnRelationshipsRefresh(data); | |
} | |
void OnUserUpdated(void* data) | |
{ | |
struct Application* app = (struct Application*)data; | |
struct DiscordUser user; | |
app->users->get_current_user(app->users, &user); | |
app->user_id = user.id; | |
} | |
void setActivity(struct Application *app, const char *name, const char *details, const char *state) { | |
struct DiscordActivity activity = {0}; | |
activity.type = DiscordActivityType_Playing; | |
activity.application_id = APP_ID; | |
strcpy(activity.name, name); | |
strcpy(activity.state, state); | |
strcpy(activity.details, details); | |
app->activities->update_activity(app->activities, &activity, NULL, NULL); | |
} | |
int main(int argc, char** argv) | |
{ | |
struct Application app = {0}; | |
struct IDiscordUserEvents users_events = {0}; | |
struct IDiscordActivityEvents activities_events = {0}; | |
struct IDiscordRelationshipEvents relationships_events = {0}; | |
users_events.on_current_user_update = OnUserUpdated; | |
relationships_events.on_refresh = OnRelationshipsRefresh; | |
relationships_events.on_relationship_update = OnRelationshipsUpdate; | |
struct DiscordCreateParams params; | |
DiscordCreateParamsSetDefault(¶ms); | |
params.client_id = APP_ID; | |
params.flags = DiscordCreateFlags_NoRequireDiscord; // DiscordCreateFlags_Default; | |
params.event_data = &app; | |
params.activity_events = &activities_events; | |
params.relationship_events = &relationships_events; | |
params.user_events = &users_events; | |
DISCORD_REQUIRE(DiscordCreate(DISCORD_VERSION, ¶ms, &app.core)); | |
app.users = app.core->get_user_manager(app.core); | |
app.activities = app.core->get_activity_manager(app.core); | |
app.relationships = app.core->get_relationship_manager(app.core); | |
setActivity(&app, "TestApp", "Running", "Like a fox"); | |
for (;;) { | |
DISCORD_REQUIRE(app.core->run_callbacks(app.core)); | |
#ifdef _WIN32 | |
Sleep(16); | |
#else | |
usleep(16 * 1000); | |
#endif | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run this on two clients who are friends. When you start this app for the second client, the first client will (correctly) get a relationship update and print out that their friend is now playing "Test Application", however, the second client gets only a single relationship update (the initial refresh) and in it their friend has no data in
relationship.presence
, and never gets an update.