Discord library in plain C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

discord.h 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // Created by Memer on 24.08.18.
  3. //
  4. #ifndef _DISCORD_H
  5. #define _DISCORD_H
  6. #include <stdbool.h>
  7. #include <curl/curl.h>
  8. #define LD_API_ENDPOINT "https://discordapp.com/api"
  9. enum
  10. {
  11. LD_REST_UNAUTHORIZED = 401,
  12. LD_REST_BAD_REQUEST = 400,
  13. LD_REST_FORBIDDEN = 403,
  14. LD_REST_NOT_FOUND = 404,
  15. LD_REST_METHOD_NOT_ALLOWED = 405,
  16. LD_REST_TOO_MANY_REQUESTS = 429,
  17. LD_REST_GATEWAY_UNAVAILABLE = 502,
  18. LD_REST_SERVER_ERROR = 500,
  19. LD_REST_UNKNOWN = 1
  20. };
  21. struct ld_context
  22. {
  23. // TODO: Websocket stuff
  24. char *bot_token;
  25. char *session_id;
  26. bool hello_triggered;
  27. int heartbeat_interval;
  28. int heartbeat_acks;
  29. int sequence;
  30. struct ld_rest_error *last_rest_status;
  31. CURL *curl_handle;
  32. // ld_user_t self;
  33. };
  34. struct ld_context_info
  35. {
  36. char *bot_token;
  37. int log_level;
  38. };
  39. struct ld_gateway_bot_resp
  40. {
  41. char *url;
  42. int shards;
  43. };
  44. struct ld_rest_error
  45. {
  46. int what;
  47. char *text;
  48. };
  49. enum game_types {
  50. GAME = 0,
  51. STREAMING = 1
  52. };
  53. enum channel_types {
  54. GUILD_TEXT,
  55. DM,
  56. GUILD_VOICE,
  57. GROUP_DM,
  58. GUILD_CATEGORY
  59. };
  60. enum {
  61. DISPATCH,
  62. HEARTBEAT,
  63. IDENTIFY,
  64. PRESENCE,
  65. VOICE_STATE,
  66. VOICE_PING,
  67. RESUME,
  68. RECONNECT,
  69. REQUEST_MEMBERS,
  70. INVALIDATE_SESSION,
  71. HELLO,
  72. HEARTBEAT_ACK
  73. };
  74. typedef struct guild_channel {
  75. char *id;
  76. char *guild_id;
  77. char *name;
  78. int type;
  79. int position;
  80. bool nsfw;
  81. // TODO: permission_overwrites
  82. char *topic;
  83. char *last_message_id;
  84. int bitrate;
  85. int user_limit;
  86. // TODO: recipients
  87. char *icon;
  88. char *owner_id;
  89. char *application_id;
  90. char *parent_id;
  91. char *last_pin_timestamp;
  92. } guild_channel_t;
  93. typedef struct thumbnail {
  94. char* url;
  95. char* proxy_url;
  96. int height;
  97. int width;
  98. } thumbnail_t;
  99. typedef struct video {
  100. char* url;
  101. int height;
  102. int width;
  103. } video_t;
  104. typedef struct image {
  105. char* url;
  106. char* proxy_url;
  107. int height;
  108. int width;
  109. } image_t;
  110. typedef struct provider {
  111. char* name;
  112. char* url;
  113. } provider_t;
  114. typedef struct author {
  115. char* name;
  116. char* url;
  117. char* icon_url;
  118. char* proxy_icon_url;
  119. } author_t;
  120. typedef struct footer {
  121. char* text;
  122. char* icon_url;
  123. char* proxy_icon_url;
  124. } footer_t;
  125. typedef struct field {
  126. char* name;
  127. char* value;
  128. int _inline;
  129. } field_t;
  130. typedef struct embed {
  131. char* title;
  132. char* type;
  133. char* description;
  134. char* url;
  135. char* timestamp;
  136. int color;
  137. footer_t footer;
  138. image_t image;
  139. thumbnail_t thumbnail;
  140. video_t video;
  141. provider_t provider;
  142. author_t author;
  143. field_t fields;
  144. } embed_t;
  145. typedef struct user {
  146. char* id;
  147. char* username;
  148. char* discriminator;
  149. char* avatar;
  150. int bot;
  151. int mfa_enabled;
  152. int verified;
  153. char* email;
  154. } user_t;
  155. typedef struct dm_channel {
  156. char *id;
  157. int is_private;
  158. user_t recipient;
  159. char *last_message_id;
  160. } dm_channel_t;
  161. typedef struct game {
  162. char *name;
  163. int type;
  164. char *url; // Only if type is STREAMING (1)
  165. } game_t;
  166. typedef struct presence_update {
  167. user_t user; // Can be partial
  168. game_t game; // May be null
  169. char *guild_id;
  170. char *status;
  171. char *roles[200];
  172. } presence_update_t;
  173. typedef struct emoji {
  174. char* id;
  175. char* name;
  176. } emoji_t;
  177. typedef struct reaction {
  178. int counter;
  179. int me;
  180. emoji_t _emoji;
  181. } reaction_t;
  182. typedef struct message {
  183. char* id;
  184. char* channel_id;
  185. user_t author;
  186. char* content;
  187. char* timestamp;
  188. char* edited_timestamp;
  189. int tts;
  190. int mention_everyone;
  191. user_t mentions[1024];
  192. /*
  193. TODO:
  194. mention_roles
  195. attachments
  196. embeds
  197. nonce
  198. */
  199. embed_t embeds[1024];
  200. reaction_t reactions[1024];
  201. int pinned;
  202. char* webhook_id;
  203. } message_t;
  204. struct ld_context *ld_create_context(struct ld_context_info *info);
  205. char *ld_get_gateway(struct ld_context *ctx);
  206. struct ld_gateway_bot_resp *ld_get_gateway_bot(struct ld_context *ctx);
  207. int ld_create_message(struct ld_context *ctx, char *channel_id, char *content);
  208. int ld_get_channel(struct ld_context *ctx, char *channel_id, guild_channel_t *target);
  209. #endif // _DISCORD_H