-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
158 lines (137 loc) · 5.61 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <concord/discord.h>
#include <concord/log.h>
#include "interactions.h"
void
on_interaction_create(struct discord *cogbot,
const struct discord_interaction *interaction)
{
/* Return in case of missing user input */
if (!interaction->data) return;
/* initialize interaction response with some default values */
struct discord_interaction_response params = {
.type = DISCORD_INTERACTION_CHANNEL_MESSAGE_WITH_SOURCE,
.data =
&(struct discord_interaction_callback_data){
.flags = DISCORD_MESSAGE_EPHEMERAL,
.content = "⚠️ Internal Error! Interaction is "
"malfunctioning, please "
"report to the staff.",
},
};
switch (interaction->type) {
case DISCORD_INTERACTION_APPLICATION_COMMAND:
if (interaction->data->options
&& 0 == strcmp(interaction->data->name, "mychannel")) {
for (int i = 0; i < interaction->data->options->size; ++i) {
char *cmd = interaction->data->options->array[i].name;
if (0 == strcmp(cmd, "action"))
react_rubberduck_channel_action(
cogbot, ¶ms, interaction,
interaction->data->options->array[i].options);
else if (0 == strcmp(cmd, "configure"))
react_rubberduck_channel_configure(
cogbot, ¶ms, interaction,
interaction->data->options->array[i].options);
else if (0 == strcmp(cmd, "delete"))
react_rubberduck_channel_delete(
cogbot, ¶ms, interaction,
interaction->data->options->array[i].options);
}
}
break;
case DISCORD_INTERACTION_MESSAGE_COMPONENT:
if (0 == strcmp(interaction->data->custom_id, "channel-subscriptions"))
react_select_subscriptions_menu(cogbot, ¶ms, interaction);
else if (0 == strcmp(interaction->data->custom_id, "create-channel"))
react_rubberduck_channel_menu(cogbot, ¶ms, interaction);
else if (0 == strcmp(interaction->data->custom_id, "os"))
react_select_OS(cogbot, ¶ms, interaction);
break;
default:
log_error("Interaction (%d code) is not dealt with",
interaction->type);
break;
}
discord_create_interaction_response(cogbot, interaction->id,
interaction->token, ¶ms, NULL);
}
void
on_ready(struct discord *cogbot, const struct discord_ready *event)
{
(void)cogbot;
log_info("Cog-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
struct cogbot_primitives
cogbot_get_primitives(struct discord *cogbot)
{
struct cogbot_primitives primitives = { 0 };
char *path[3] = { "cog_bot", "", "" };
struct ccord_szbuf_readonly json;
/* cog_bot.guild_id */
path[1] = "guild_id";
json = discord_config_get_field(cogbot, path, 2);
primitives.guild_id = strtoull(json.start, NULL, 10);
/* cog_bot.category_id */
path[1] = "category_id";
json = discord_config_get_field(cogbot, path, 2);
primitives.category_id = strtoull(json.start, NULL, 10);
/* cog_bot.roles.verified_id */
path[1] = "roles";
path[2] = "verified_id";
json = discord_config_get_field(cogbot, path, 3);
primitives.roles.verified_id = strtoull(json.start, NULL, 10);
/* cog_bot.roles.rubberduck_id */
path[2] = "rubberduck_id";
json = discord_config_get_field(cogbot, path, 3);
primitives.roles.rubberduck_id = strtoull(json.start, NULL, 10);
/* cog_bot.roles.helper_id */
path[2] = "helper_id";
json = discord_config_get_field(cogbot, path, 3);
primitives.roles.helper_id = strtoull(json.start, NULL, 10);
/* cog_bot.roles.watcher_id */
path[2] = "watcher_id";
json = discord_config_get_field(cogbot, path, 3);
primitives.roles.watcher_id = strtoull(json.start, NULL, 10);
/* cog_bot.roles.announcements_id */
path[2] = "announcements_id";
json = discord_config_get_field(cogbot, path, 3);
primitives.roles.announcements_id = strtoull(json.start, NULL, 10);
/* cog_bot.roles.linux_id */
path[2] = "linux_id";
json = discord_config_get_field(cogbot, path, 3);
primitives.roles.linux_id = strtoull(json.start, NULL, 10);
/* cog_bot.roles.windows_id */
path[2] = "windows_id";
json = discord_config_get_field(cogbot, path, 3);
primitives.roles.windows_id = strtoull(json.start, NULL, 10);
/* cog_bot.roles.macos_id */
path[2] = "macos_id";
json = discord_config_get_field(cogbot, path, 3);
primitives.roles.macos_id = strtoull(json.start, NULL, 10);
/* cog_bot.roles.bsd_id */
path[2] = "bsd_id";
json = discord_config_get_field(cogbot, path, 3);
primitives.roles.bsd_id = strtoull(json.start, NULL, 10);
return primitives;
}
int
main(int argc, char *argv[])
{
struct cogbot_primitives primitives;
struct discord *cogbot;
ccord_global_init();
cogbot = discord_config_init((argc > 1) ? argv[1] : "config.json");
assert(NULL != cogbot && "Couldn't initialize client");
primitives = cogbot_get_primitives(cogbot);
discord_set_data(cogbot, &primitives);
discord_set_on_ready(cogbot, &on_ready);
discord_set_on_interaction_create(cogbot, &on_interaction_create);
discord_run(cogbot);
discord_cleanup(cogbot);
ccord_global_cleanup();
}