send-presence.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. This is a simple example in C of using the rich presence API asynchronously.
  3. */
  4. #define _CRT_SECURE_NO_WARNINGS /* thanks Microsoft */
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <time.h>
  9. #include "discord_rpc.h"
  10. static const char* APPLICATION_ID = "345229890980937739";
  11. static int FrustrationLevel = 0;
  12. static int64_t StartTime;
  13. static int SendPresence = 1;
  14. static int prompt(char* line, size_t size)
  15. {
  16. int res;
  17. char* nl;
  18. printf("\n> ");
  19. fflush(stdout);
  20. res = fgets(line, (int)size, stdin) ? 1 : 0;
  21. line[size - 1] = 0;
  22. nl = strchr(line, '\n');
  23. if (nl) {
  24. *nl = 0;
  25. }
  26. return res;
  27. }
  28. static void updateDiscordPresence()
  29. {
  30. if (SendPresence) {
  31. char buffer[256];
  32. DiscordRichPresence discordPresence;
  33. memset(&discordPresence, 0, sizeof(discordPresence));
  34. discordPresence.state = "West of House";
  35. sprintf(buffer, "Frustration level: %d", FrustrationLevel);
  36. discordPresence.details = buffer;
  37. discordPresence.startTimestamp = StartTime;
  38. discordPresence.endTimestamp = time(0) + 5 * 60;
  39. discordPresence.largeImageKey = "canary-large";
  40. discordPresence.smallImageKey = "ptb-small";
  41. discordPresence.partyId = "party1234";
  42. discordPresence.partySize = 1;
  43. discordPresence.partyMax = 6;
  44. discordPresence.partyPrivacy = DISCORD_PARTY_PUBLIC;
  45. discordPresence.matchSecret = "xyzzy";
  46. discordPresence.joinSecret = "join";
  47. discordPresence.spectateSecret = "look";
  48. discordPresence.instance = 0;
  49. Discord_UpdatePresence(&discordPresence);
  50. }
  51. else {
  52. Discord_ClearPresence();
  53. }
  54. }
  55. static void handleDiscordReady(const DiscordUser* connectedUser)
  56. {
  57. printf("\nDiscord: connected to user %s#%s - %s\n",
  58. connectedUser->username,
  59. connectedUser->discriminator,
  60. connectedUser->userId);
  61. }
  62. static void handleDiscordDisconnected(int errcode, const char* message)
  63. {
  64. printf("\nDiscord: disconnected (%d: %s)\n", errcode, message);
  65. }
  66. static void handleDiscordError(int errcode, const char* message)
  67. {
  68. printf("\nDiscord: error (%d: %s)\n", errcode, message);
  69. }
  70. static void handleDiscordJoin(const char* secret)
  71. {
  72. printf("\nDiscord: join (%s)\n", secret);
  73. }
  74. static void handleDiscordSpectate(const char* secret)
  75. {
  76. printf("\nDiscord: spectate (%s)\n", secret);
  77. }
  78. static void handleDiscordJoinRequest(const DiscordUser* request)
  79. {
  80. int response = -1;
  81. char yn[4];
  82. printf("\nDiscord: join request from %s#%s - %s\n",
  83. request->username,
  84. request->discriminator,
  85. request->userId);
  86. do {
  87. printf("Accept? (y/n)");
  88. if (!prompt(yn, sizeof(yn))) {
  89. break;
  90. }
  91. if (!yn[0]) {
  92. continue;
  93. }
  94. if (yn[0] == 'y') {
  95. response = DISCORD_REPLY_YES;
  96. break;
  97. }
  98. if (yn[0] == 'n') {
  99. response = DISCORD_REPLY_NO;
  100. break;
  101. }
  102. } while (1);
  103. if (response != -1) {
  104. Discord_Respond(request->userId, response);
  105. }
  106. }
  107. static void discordInit()
  108. {
  109. DiscordEventHandlers handlers;
  110. memset(&handlers, 0, sizeof(handlers));
  111. handlers.ready = handleDiscordReady;
  112. handlers.disconnected = handleDiscordDisconnected;
  113. handlers.errored = handleDiscordError;
  114. handlers.joinGame = handleDiscordJoin;
  115. handlers.spectateGame = handleDiscordSpectate;
  116. handlers.joinRequest = handleDiscordJoinRequest;
  117. Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL);
  118. }
  119. static void gameLoop()
  120. {
  121. char line[512];
  122. char* space;
  123. StartTime = time(0);
  124. printf("You are standing in an open field west of a white house.\n");
  125. while (prompt(line, sizeof(line))) {
  126. if (line[0]) {
  127. if (line[0] == 'q') {
  128. break;
  129. }
  130. if (line[0] == 't') {
  131. printf("Shutting off Discord.\n");
  132. Discord_Shutdown();
  133. continue;
  134. }
  135. if (line[0] == 'c') {
  136. if (SendPresence) {
  137. printf("Clearing presence information.\n");
  138. SendPresence = 0;
  139. }
  140. else {
  141. printf("Restoring presence information.\n");
  142. SendPresence = 1;
  143. }
  144. updateDiscordPresence();
  145. continue;
  146. }
  147. if (line[0] == 'y') {
  148. printf("Reinit Discord.\n");
  149. discordInit();
  150. continue;
  151. }
  152. if (time(NULL) & 1) {
  153. printf("I don't understand that.\n");
  154. }
  155. else {
  156. space = strchr(line, ' ');
  157. if (space) {
  158. *space = 0;
  159. }
  160. printf("I don't know the word \"%s\".\n", line);
  161. }
  162. ++FrustrationLevel;
  163. updateDiscordPresence();
  164. }
  165. #ifdef DISCORD_DISABLE_IO_THREAD
  166. Discord_UpdateConnection();
  167. #endif
  168. Discord_RunCallbacks();
  169. }
  170. }
  171. int main(int argc, char* argv[])
  172. {
  173. discordInit();
  174. gameLoop();
  175. Discord_Shutdown();
  176. return 0;
  177. }