-
Notifications
You must be signed in to change notification settings - Fork 30
/
components.c
228 lines (206 loc) · 7.16 KB
/
components.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h> /* PRIu64 */
#include <assert.h>
#include "discord.h"
#include "log.h"
void
print_usage(void)
{
printf(
"\n\nThis bot demonstrates how to load message components"
" with three different methods.\n"
"1 - Dynamic-approach (type !dynamic): Load the components from "
"a JSON string.\n"
"2 - Static-approach (type !static): A clean initialization approach "
"using the combination of designated initialization and compound "
"literals.\n"
"\nTYPE ANY KEY TO START BOT\n");
}
char JSON[] =
"[\n"
" {\n"
" \"type\": 1,\n"
" \"components\": [\n"
" {\n"
" \"type\": 3,\n"
" \"custom_id\": \"class_select_1\",\n"
" \"options\":[\n"
" {\n"
" \"label\": \"Rogue\",\n"
" \"value\": \"rogue\",\n"
" \"description\": \"Sneak n stab\",\n"
" \"emoji\": {\n"
" \"name\": \"rogue\",\n"
" \"id\": \"625891304148303894\"\n"
" }\n"
" },\n"
" {\n"
" \"label\": \"Mage\",\n"
" \"value\": \"mage\",\n"
" \"description\": \"Turn 'em into a sheep\",\n"
" \"emoji\": {\n"
" \"name\": \"mage\",\n"
" \"id\": \"625891304081063986\"\n"
" }\n"
" },\n"
" {\n"
" \"label\": \"Priest\",\n"
" \"value\": \"priest\",\n"
" \"description\": \"You get heals when I'm done "
"doing damage\",\n"
" \"emoji\": {\n"
" \"name\": \"priest\",\n"
" \"id\": \"625891303795982337\"\n"
" }\n"
" }\n"
" ],\n"
" \"placeholder\": \"Choose a class\",\n"
" \"min_values\": 1,\n"
" \"max_values\": 3\n"
" }\n"
" ]\n"
" }\n"
"]\n";
void
on_ready(struct discord *client, const struct discord_ready *event)
{
log_info("Components-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
void
on_dynamic(struct discord *client, const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_components components = { 0 };
discord_components_from_json(JSON, sizeof(JSON), &components);
struct discord_create_message params = {
.content = "Mason is looking for new arena partners. What classes do "
"you play?",
.components = &components
};
discord_create_message(client, event->channel_id, ¶ms, NULL);
/* must cleanup 'components' afterwards */
discord_components_cleanup(&components);
}
void
on_static(struct discord *client, const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_select_option select_options[] = {
{
.label = "Rogue",
.value = "rogue",
.description = "Sneak n stab",
.emoji =
&(struct discord_emoji){
.name = "rogue",
.id = 625891304148303894ULL,
},
},
{
.label = "Mage",
.value = "mage",
.description = "Turn 'em into a sheep",
.emoji =
&(struct discord_emoji){
.name = "mage",
.id = 625891304081063986ULL,
},
},
{
.label = "Priest",
.value = "priest",
.description = "You get heals when I'm "
"done doing damage",
.emoji =
&(struct discord_emoji){
.name = "priest",
.id = 625891303795982337ULL,
},
},
};
struct discord_component select_menu[] = {
{
.type = DISCORD_COMPONENT_SELECT_MENU,
.custom_id = "class_select_1",
.options =
&(struct discord_select_options){
.size = sizeof(select_options) / sizeof *select_options,
.array = select_options,
},
.placeholder = "Choose a class",
.min_values = 1,
.max_values = 3,
},
};
struct discord_component action_rows[] = {
{
.type = DISCORD_COMPONENT_ACTION_ROW,
.components =
&(struct discord_components){
.size = sizeof(select_menu) / sizeof *select_menu,
.array = select_menu,
},
},
};
struct discord_create_message params = {
.content = "Mason is looking for new arena partners. What classes do "
"you play?",
.components =
&(struct discord_components){
.size = sizeof(action_rows) / sizeof *action_rows,
.array = action_rows,
},
};
discord_create_message(client, event->channel_id, ¶ms, NULL);
}
void
on_interaction_create(struct discord *client,
const struct discord_interaction *event)
{
log_info("Interaction %" PRIu64 " received", event->id);
if (!event->data || !event->data->values) return;
char values[1024];
strings_to_json(values, sizeof(values), event->data->values);
char text[DISCORD_MAX_MESSAGE_LEN];
snprintf(text, sizeof(text),
"So you have chosen:\n"
"```json\n"
"%s\n"
"```",
values);
struct discord_interaction_response params = {
.type = DISCORD_INTERACTION_CHANNEL_MESSAGE_WITH_SOURCE, // 4
.data =
&(struct discord_interaction_callback_data){
.content = text,
.flags = DISCORD_MESSAGE_EPHEMERAL // 1 << 6
}
};
discord_create_interaction_response(client, event->id, event->token,
¶ms, NULL);
}
int
main(int argc, char *argv[])
{
const char *config_file;
if (argc > 1)
config_file = argv[1];
else
config_file = "../config.json";
ccord_global_init();
struct discord *client = discord_config_init(config_file);
assert(NULL != client && "Couldn't initialize client");
discord_set_on_ready(client, &on_ready);
discord_set_prefix(client, "!");
discord_set_on_command(client, "dynamic", &on_dynamic);
discord_set_on_command(client, "static", &on_static);
discord_set_on_interaction_create(client, &on_interaction_create);
print_usage();
fgetc(stdin); // wait for input
discord_run(client);
discord_cleanup(client);
ccord_global_cleanup();
}