-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbot-emoji.c
116 lines (90 loc) · 3.11 KB
/
bot-emoji.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
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h> /* PRIu64, SCNu64 */
#include <assert.h>
#include "discord.h"
void on_ready(struct discord *client)
{
const struct discord_user *bot = discord_get_self(client);
log_info("Emoji-Bot succesfully connected to Discord as %s#%s!",
bot->username, bot->discriminator);
}
void on_list(struct discord *client, const struct discord_message *msg)
{
if (msg->author->bot) return;
struct discord_emoji **emojis = NULL;
char text[2000];
ORCAcode code;
code = discord_list_guild_emojis(client, msg->guild_id, &emojis);
if (code != ORCA_OK || !emojis) {
sprintf(text, "No guild emojis found.");
}
else {
char *cur = text;
char *end = &text[sizeof(text) - 1];
char *prev;
for (size_t i = 0; emojis[i]; ++i) {
prev = cur;
cur += snprintf(cur, end - cur, "<%s:%s:%" PRIu64 ">(%" PRIu64 ")\n",
emojis[i]->animated ? "a" : "", emojis[i]->name,
emojis[i]->id, emojis[i]->id);
if (cur >= end) { // to make sure no emoji is skipped
*prev = '\0'; // end string before truncation
// reset for retry
cur = text;
--i;
struct discord_create_message_params params = { .content = text };
discord_create_message(client, msg->channel_id, ¶ms, NULL);
continue;
}
}
discord_emoji_list_free(emojis);
}
struct discord_create_message_params params = { .content = text };
discord_create_message(client, msg->channel_id, ¶ms, NULL);
}
void on_get(struct discord *client, const struct discord_message *msg)
{
if (msg->author->bot) return;
char text[DISCORD_MAX_MESSAGE_LEN];
u64_snowflake_t emoji_id = 0;
sscanf(msg->content, "%" SCNu64, &emoji_id);
if (!emoji_id) {
sprintf(text, "Missing 'emoji_id'");
}
else {
struct discord_emoji emoji = { 0 };
discord_get_guild_emoji(client, msg->guild_id, emoji_id, &emoji);
if (emoji.id)
sprintf(text, "Here you go: <%s:%s:%" PRIu64 ">",
emoji.animated ? "a" : "", emoji.name, emoji.id);
else
sprintf(text, "Unknown emoji");
discord_emoji_cleanup(&emoji);
}
struct discord_create_message_params params = { .content = text };
discord_create_message(client, msg->channel_id, ¶ms, NULL);
}
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 && "Could not initialize client");
discord_set_on_ready(client, &on_ready);
discord_set_prefix(client, "emoji.");
discord_set_on_command(client, "list", &on_list);
discord_set_on_command(client, "get", &on_get);
printf("\n\n This bot demonstrates how easy it is to create/delete emojis\n"
"1. Type 'emoji.list' to get a list of server emojis\n"
"2. Type 'emoji.get <emoji_id>' to get the selected emoji\n"
"\nTYPE ANY KEY TO START BOT\n");
fgetc(stdin); // wait for input
discord_run(client);
discord_cleanup(client);
orca_global_cleanup();
}