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.8KB

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