testbot.c (1105B)
1 #include <alloca.h> 2 #include <stdio.h> 3 4 #include "libdiscord.h" 5 6 static char buf[8192]; 7 8 int 9 main(void) 10 { 11 printf("discord version: %s\n", LIBDISCORD_VERSION); 12 13 struct discord *discord = alloca(discord_sizeof()); 14 15 if (discord_init(discord, buf, sizeof buf) < 0) { 16 fprintf(stderr, "Failed to initialise discord ctx\n"); 17 return -1; 18 } 19 20 if (discord_connect_gateway(discord) < 0) { 21 fprintf(stderr, "Failed to connect to discord gateway\n"); 22 return -1; 23 } 24 25 struct discord_event evs[16]; 26 size_t cap = sizeof evs / sizeof evs[0]; 27 28 int res; 29 while ((res = discord_poll_events(discord, evs, cap)) > 0) { 30 for (int i = 0; i < res; i++) { 31 struct discord_event *ev = &evs[i]; 32 33 switch (ev->type) { 34 case DISCORD_EVENT_API: 35 printf("got api message: \"%.*s\"\n", 36 ev->tag.api.len, 37 ev->tag.api.ptr); 38 break; 39 40 case DISCORD_EVENT_GATEWAY: 41 printf("got gateway message: \"%.*s\"\n", 42 ev->tag.gateway.len, 43 ev->tag.gateway.ptr); 44 break; 45 46 case DISCORD_EVENT_VOICE: 47 printf("got voice message!\n"); 48 break; 49 } 50 } 51 } 52 53 discord_free(discord); 54 55 return 0; 56 }