1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#include <cJSON.h>
#include <stb_ds.h>
#include <dbs/abstract.h>
#include <dbs/api.h>
#include <dbs/commands.h>
#include <dbs/event.h>
#include <dbs/init.h>
#include <dbs/log.h>
#include <dbs/util.h>
extern char *app_id;
extern double api_latency;
Command ping_command = {
.type = COMMAND_CHAT_INPUT,
.name = "ping",
.description = "ping pong all the way to america :flag_us:",
.options = NULL
};
void ping(cJSON *i)
{
static int do_hidden = 1;
struct timeval sent;
struct timeval done;
gettimeofday(&sent, NULL);
if(!interaction_defer_reply(i, do_hidden))
print("ping: DEFER failed");
do_hidden = !do_hidden;
gettimeofday(&done, NULL);
char *response = malloc(128);
snprintf(response, 128, "{\"embeds\":[{\"title\": \"PONG!\", \"description\": \"Measured API Latency: %.4fms\\nWS Latency: %.4fms\"}]}",
(done.tv_sec - sent.tv_sec) * 1000.0f + (done.tv_usec - sent.tv_usec) / 1000.0f,
api_latency);
if(!interaction_edit_reply(i, response, 1))
print("ping: EDIT failed");
free(response);
}
declare_command(ping);
Command hi_command = {
.type = COMMAND_CHAT_INPUT,
.name = "hi",
.description = "hallo!!!!",
.options = NULL
};
static void hi(cJSON *i)
{
if(!interaction_reply(i, "hello world!", 0))
print("hi: REPLY failed");
}
declare_command(hi);
int interaction_create(cJSON *ev_data)
{
int i_type = js_getInt(ev_data, "type");
cJSON *i = cJSON_GetObjectItemCaseSensitive(ev_data, "data");
switch(i_type) {
case 1: ; /* PING */
break;
case 2: ; /* APPLICATION_COMMAND */
char *cmd_name = js_getStr(i, "name");
for(int i = 0; i < arrlen(commands); ++i) {
if(strcmp(cmd_name, commands[i].name) == 0) {
commands[i].callback(ev_data);
return 0;
}
}
print(LOG_WARN "commands: unknown command %s", cmd_name);
break;
case 3: ; /* MESSAGE_COMPONENT */
break;
case 4: ; /* APPLICATION_COMMAND_AUTOCOMPLETE */
break;
case 5: ; /* MODAL_SUBMIT */
break;
default:
break;
}
char *payload = cJSON_Print(ev_data);
print("inter_create: payload (see below)\n%s", payload);
free(payload);
return 1;
}
declare_event(INTERACTION_CREATE, interaction_create);
|