Simple and easy to use Discord Rich Presence library for C.
Official library is deprecated and not maintained anymore and it SUCKS. So I decided to create a new one, but with some improvements and bug fixes.
- Simple and easy to use
- Almost no external dependencies (only
janssonfor JSON parsing) - Support for multiple instances
I only tested it on Linux amd64, probably it doesn't work on Windows because of the unix socket implementation.
janssonfor JSON parsinglibcfor system calls
git clone https://github.com/XielQs/DiscordRPC --recursive
cd DiscordRPCIf you want to build a static library, you can do it by running:
make lib -j
If you want to build a shared library, you can do it by running:
make shared -j
You can use the following example to initialize the library and set the activity:
#include <discordrpc.h> #include <string.h> #include <stdio.h> void readyEvent(const DiscordUser* user) { printf("Discord RPC is ready. User ID: %s, Username: %s, Global Name: %s\n", user->id, user->username, user->global_name); } int main() { // Initialize Discord RPC printf("Initializing Discord RPC...\n"); DiscordRPC discord; DiscordEventHandlers handlers; memset(&handlers, 0, sizeof(handlers)); handlers.ready = readyEvent; DiscordRPC_init(&discord, "YOUR_CLIENT_ID", &handlers); // You can pass NULL for handlers if you don't need them if (discord.connected) { printf("Connected to Discord RPC successfully!\n"); // Set the presence DiscordActivity activity; memset(&activity, 0, sizeof(activity)); activity.state = "Example State"; activity.details = "Example Details"; activity.startTimestamp = time(NULL); DiscordRPC_setActivity(&discord, &activity); // Run the main loop or do other stuff while (1) {} // Keep the program running DiscordRPC_shutdown(&discord); // Shutdown Discord RPC } else { printf("Failed to connect to Discord RPC: %s\n", discord.last_error); return 1; } return 0; }
- The library will fail with error
Socket connection failed.if Discord is running in a Flatpak. One possible way to solve the issue temporarily is by runningln -sf $XDG_RUNTIME_DIR/{app/com.discordapp.Discord,}/discord-ipc-0in the terminal.
It is nearly the same as the official library, you can find the official documentation here.
But if you want an overview of the API, here it is:
DiscordRPC_init(DiscordRPC* discord, const char* client_id, DiscordEventHandlers* handlers)- Initialize Discord RPCdiscord- Pointer to the DiscordRPC structclient_id- Your Discord application client IDhandlers- Pointer to the DiscordEventHandlers struct
DiscordRPC_shutdown(DiscordRPC* discord)- Shutdown Discord RPCdiscord- Pointer to the DiscordRPC struct
DiscordRPC_setActivity(DiscordRPC* discord, DiscordActivity* activity)- Set the activitydiscord- Pointer to the DiscordRPC structactivity- Pointer to the DiscordActivity struct containing the activity data (pass NULL to clear the activity)
DiscordRPC_acceptInvite(DiscordRPC* discord, const char* user_id)- Accept an invite from a userdiscord- Pointer to the DiscordRPC structuser_id- The ID of the user to accept the invite from
DiscordRPC_declineInvite(DiscordRPC* discord, const char* user_id)- Decline an invite from a userdiscord- Pointer to the DiscordRPC structuser_id- The ID of the user to decline the invite from
DiscordActivity- Struct containing the activity datastate- The state of the activity (e.g. "Playing")details- The details of the activity (e.g. "Playing a game")startTimestamp- The start timestamp of the activity (e.g. time(NULL))endTimestamp- The end timestamp of the activity (e.g. time(NULL) + 3600)largeImageKey- The key of the large image to displaylargeImageText- The text to display when hovering over the large imagesmallImageKey- The key of the small image to displaysmallImageText- The text to display when hovering over the small imagepartyId- The ID of the partypartySize- The size of the partypartyMax- The maximum size of the partypartyPrivacy- The privacy level of the party (DISCORD_PARTY_PUBLICfor public,DISCORD_PARTY_PRIVATEfor private)matchSecret- The secret to matchmake with the partyjoinSecret- The secret to join the partyspectateSecret- The secret to spectate the partyinstance- Whether the activity is an instance or not (1 for true, 0 for false)
DiscordEventHandlers- Struct containing the event handlersready(const DiscordUser* user)- Called when Discord RPC is readydisconnected(const bool was_error)- Called when Discord RPC is disconnectederror(int error_code, const char* message)- Called when there is an errorjoinGame(const char* join_secret)- Called when the user joins another users gamespectateGame(const char* spectate_secret)- Called when the user spectates another users gamejoinRequest(const DiscordUser* user)- Called when there is a pending join request from a user
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please feel free to submit a pull request or open an issue if you find any bugs or have suggestions for improvements.
My code sucks, so feel free to suggest any improvements or fixes. I will try to implement them as soon as possible.