-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbot-voice.c
129 lines (102 loc) · 3.73 KB
/
bot-voice.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include "discord.h"
#include "discord-internal.h"
void on_ready(struct discord *client)
{
const struct discord_user *bot = discord_get_self(client);
log_info("Voice-Bot succesfully connected to Discord as %s#%s!",
bot->username, bot->discriminator);
}
void on_list_voice_regions(struct discord *client,
const struct discord_message *msg)
{
if (msg->author->bot) return;
struct discord_voice_region **voice_regions = NULL;
discord_list_voice_regions(client, &voice_regions);
if (!voice_regions) {
log_error("Could not obtain voice regions");
return;
}
struct discord_create_message_params params = { 0 };
for (size_t i = 0; voice_regions[i]; ++i) {
params.content = voice_regions[i]->name;
discord_create_message(client, msg->channel_id, ¶ms, NULL);
}
discord_voice_region_list_free(voice_regions);
}
void on_voice_join(struct discord *client, const struct discord_message *msg)
{
if (msg->author->bot) return;
struct discord_channel vchannel;
int position = -1;
sscanf(msg->content, "%d", &position);
discord_get_channel_at_pos(client, msg->guild_id,
DISCORD_CHANNEL_GUILD_VOICE, position - 1,
&vchannel);
if (vchannel.id != 0) { // founds voice channel at pos
discord_voice_join(client, msg->guild_id, vchannel.id, false, false);
}
else { // couldn't find a voice channel at pos
struct discord_create_message_params params = {
.content = "Invalid channel position"
};
discord_create_message(client, msg->channel_id, ¶ms, NULL);
}
discord_channel_cleanup(&vchannel);
}
void on_voice_kick(struct discord *client, const struct discord_message *msg)
{
if (msg->author->bot) return;
char text[DISCORD_MAX_MESSAGE_LEN];
u64_snowflake_t user_id = 0;
sscanf(msg->content, "%" SCNu64, &user_id);
if (!user_id) {
sprintf(text, "Couldn't find user");
}
else {
discord_disconnect_guild_member(client, msg->guild_id, user_id, NULL);
snprintf(text, sizeof(text), "<@!%" PRIu64 "> has been kicked from VC",
user_id);
}
struct discord_create_message_params params = { .content = text };
discord_create_message(client, msg->channel_id, ¶ms, NULL);
}
void log_on_voice_state_update(struct discord *client,
const struct discord_voice_state *vs)
{
log_info("User <@!%" PRIu64 "> has joined <#%" PRIu64 ">!", vs->user_id,
vs->channel_id);
}
int main(int argc, char *argv[])
{
const char *config_file;
if (argc > 1)
config_file = argv[1];
else
config_file = "../config.json";
orca_global_init();
struct discord *client = discord_config_init(config_file);
assert(NULL != client && "Couldn't initialize client");
discord_set_on_voice_state_update(client, &log_on_voice_state_update);
discord_set_prefix(client, "voice.");
discord_set_on_command(client, "list_regions", &on_list_voice_regions);
discord_set_on_command(client, "join", &on_voice_join);
discord_set_on_command(client, "kick", &on_voice_kick);
printf("\n\nThis bot is a work in progress, it should demonstrate some "
"Voice related utilities\n"
"1. Type 'voice.list_regions' to list regions that can be used when "
"creating servers\n"
"2. Type 'voice.join <channel position>' to join a particular voice "
"channel by its position\n"
"3. Type 'voice.kick <user id>' to kick a particular user from the "
"voice channel he's at\n"
"\nTYPE ANY KEY TO START BOT\n");
fgetc(stdin); // wait for input
discord_run(client);
discord_cleanup(client);
orca_global_cleanup();
}