-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbot-ban.c
166 lines (128 loc) · 4.71 KB
/
bot-ban.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "discord.h"
void on_ready(struct discord *client)
{
const struct discord_user *bot = discord_get_self(client);
log_info("Ban-Bot succesfully connected to Discord as %s#%s!", bot->username,
bot->discriminator);
}
void on_guild_ban_add(struct discord *client,
u64_snowflake_t guild_id,
const struct discord_user *user)
{
struct discord_channel general = { 0 };
if (discord_get_channel_at_pos(client, guild_id, DISCORD_CHANNEL_GUILD_TEXT,
0, &general))
{
log_error("Couldn't fetch channel at position 0");
return;
}
char text[128];
snprintf(text, sizeof(text), "User `%s` has been banned.", user->username);
struct discord_create_message_params params = { .content = text };
discord_create_message(client, general.id, ¶ms, NULL);
discord_channel_cleanup(&general);
}
void on_guild_ban_remove(struct discord *client,
u64_snowflake_t guild_id,
const struct discord_user *user)
{
struct discord_channel general = { 0 };
if (discord_get_channel_at_pos(client, guild_id, DISCORD_CHANNEL_GUILD_TEXT,
0, &general))
{
log_error("Couldn't fetch channel at position 0");
return;
}
char text[128];
snprintf(text, sizeof(text), "User `%s` has been unbanned.", user->username);
struct discord_create_message_params params = { .content = text };
discord_create_message(client, general.id, ¶ms, NULL);
discord_channel_cleanup(&general);
}
void on_ban(struct discord *client, const struct discord_message *msg)
{
// get member list
struct discord_guild_member **members = NULL;
ORCAcode code;
code = discord_list_guild_members(
client, msg->guild_id,
&(struct discord_list_guild_members_params){ .limit = 1000, .after = 0 },
&members);
if (code != ORCA_OK || !members) return;
// get username and discriminator of the to be banned user
char username[128] = "";
char discriminator[5] = "";
sscanf(msg->content, "%[^#]#%s", username, discriminator);
if (!*username || !*discriminator) return;
// try to find match for to be banned user
struct discord_user *target = NULL;
for (size_t i = 0; members[i]; ++i) {
if (0 == strcmp(members[i]->user->username, username)
&& 0 == strcmp(members[i]->user->discriminator, discriminator))
{
target = members[i]->user;
break; /* EARLY BREAK */
}
}
if (!target) return; // member is not in guild
char reason[128];
snprintf(reason, sizeof(reason), "%s said so", msg->author->username);
discord_create_guild_ban(client, msg->guild_id, target->id,
&(struct discord_create_guild_ban_params){
.delete_message_days = 1, .reason = reason });
discord_guild_member_list_free(members);
}
void on_unban(struct discord *client, const struct discord_message *msg)
{
// get banned list
struct discord_ban **bans = NULL;
ORCAcode code;
code = discord_get_guild_bans(client, msg->guild_id, &bans);
if (code != ORCA_OK || !bans) return;
// get username and discriminator of the to be banned user
char username[128] = "";
char discriminator[5] = "";
sscanf(msg->content, "%[^#]#%s", username, discriminator);
if (!*username || !*discriminator) return;
// try to find match for to be banned user
struct discord_user *target = NULL;
for (size_t i = 0; bans[i]; ++i) {
if (0 == strcmp(bans[i]->user->username, username)
&& 0 == strcmp(bans[i]->user->discriminator, discriminator))
{
target = bans[i]->user;
break; /* EARLY BREAK */
}
}
if (!target) return; // member wasn't banned
discord_remove_guild_ban(client, msg->guild_id, target->id);
discord_ban_list_free(bans);
}
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_ready(client, &on_ready);
discord_set_on_guild_ban_add(client, &on_guild_ban_add);
discord_set_on_guild_ban_remove(client, &on_guild_ban_remove);
discord_set_on_command(client, "!ban", &on_ban);
discord_set_on_command(client, "!unban", &on_unban);
printf("\n\nThis bot demonstrates how easy it is to ban/unban people\n"
"1. Type '!ban user#1234' to ban user\n"
"2. Type '!unban user#1234' to unban user\n"
"\nTYPE ANY KEY TO START BOT\n");
fgetc(stdin); // wait for input
discord_run(client);
discord_cleanup(client);
orca_global_cleanup();
}