-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.cpp
More file actions
244 lines (215 loc) · 7.22 KB
/
events.cpp
File metadata and controls
244 lines (215 loc) · 7.22 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "discord.hpp"
#include "gateway.hpp"
#include "bot.hpp"
#include "commands.hpp"
#include "utils.hpp"
#include <boost/tokenizer.hpp>
#include <algorithm>
#include <iterator>
#include <vector>
#define EVENT_HANDLER(t) void gateway::event_##t(const discord::payload &msg)
#define GATEWAY_HANDLE(op) void gateway::handle_##op(const discord::payload &msg)
using namespace boost;
namespace backend {
/**
* @brief: handle the READY event
*
* The ready event provides the following information:
* 1) sharding
* 2) user object
* 3) guilding
* 4) session id
*/
EVENT_HANDLER(READY) {
session_id = msg.d["session_id"].get<std::string>();
bot->guild_info.id = std::stoul(msg.d["guilds"][0]["id"].get<std::string>()); // part of only 1 guild
bot->status = ACTIVE;
}
EVENT_HANDLER(RESUMED) {
bot->up_to_date = true;
}
EVENT_HANDLER(HELLO) {}
EVENT_HANDLER(CHANNEL_CREATE) {
}
EVENT_HANDLER(CHANNEL_UPDATE){}
EVENT_HANDLER(CHANNEL_DELETE){}
EVENT_HANDLER(CHANNEL_PINS_UPDATE){}
/**
* @brief: grab guild information
*
* This is a very important event because it contains information about the guild as well as
* all of the users that exists insdie of it. Pearlbot only exists in one guild so only one, so it
* holds the guild object instead of vector of guilds. Since the guild create event is only sent
* once, send a REQUEST_GUILD_MEMBERS to populate the user list.
*/
EVENT_HANDLER(GUILD_CREATE) {
try {
bot->guild_info.member_count = msg.d["member_count"].get<int>();
auto roles = msg.d["roles"];
auto channels = msg.d["channels"];
for(auto it = roles.begin(); it != roles.end(); ++it) {
(bot->guild_info.roles).push_back(parse_role(*it));
}
for(auto it = channels.begin(); it != channels.end(); ++it) {
(bot->guild_info.channels).push_back(parse_channel(*it));
}
send_payload(package({discord::REQUEST_GUILD_MEMBERS}));
} catch(const std::exception &e) {
std::cout << __FILE__ << __LINE__ << ": " << e.what() << std::endl;
}
}
EVENT_HANDLER(GUILD_UPDATE){}
EVENT_HANDLER(GUILD_DELETE){}
EVENT_HANDLER(GUILD_BAN_ADD){}
EVENT_HANDLER(GUILD_BAN_REMOVE){}
EVENT_HANDLER(GUILD_EMOJIS_UPDATE){}
EVENT_HANDLER(GUILD_INTEGRATIONS_UPDATE){}
EVENT_HANDLER(GUILD_MEMBER_ADD){}
EVENT_HANDLER(GUILD_MEMBER_REMOVE){}
EVENT_HANDLER(GUILD_MEMBER_UPDATE){}
/**
* @brief: handle guild member chunk event
*
* Chunk provides information on all of the members in the guild. Use this to populate the guild
* member map. This event provides a list of member objects which will be ignored. The only
* relevant information here is the user object.
*/
EVENT_HANDLER(GUILD_MEMBERS_CHUNK) {
try {
// make sure that we have the correct guild
assert(std::stoul(msg.d["guild_id"].get<std::string>()) == bot->guild_info.id);
auto members = msg.d["members"];
for(auto it = members.begin(); it != members.end(); ++it) {
discord::member new_member = parse_member(*it);
(bot->guild_info.members)[new_member.usr_info.id] = new_member;
}
} catch(const std::exception &e) {
std::cout << __FILE__ << __LINE__ << ": " << e.what() << std::endl;
}
}
EVENT_HANDLER(GUILD_ROLE_CREATE){}
EVENT_HANDLER(GUILD_ROLE_UPDATE){}
EVENT_HANDLER(GUILD_ROLE_DELETE){}
/**
* @brief: handle the message create event
*
* Checks if the messages is a command and then checks if the user has permissions to issue the
* command.
*/
EVENT_HANDLER(MESSAGE_CREATE) {
std::string content = msg.d["content"].get<std::string>();
uint64_t channel_id = std::stoul(msg.d["channel_id"].get<std::string>());
if(content.at(0) == bot->ref) {
bot_task task;
task.channel_id = channel_id;
// check if the user has permission to issue a command
uint64_t requestor_id = std::stoul(msg.d["author"]["id"].get<std::string>());
discord::member temp{requestor_id};
auto result = find(std::begin(bot->whitelist), std::end(bot->whitelist), temp);
if(result == std::end(bot->whitelist)) {
task.todo = {command_permission_denied};
bot->command_q.push(task);
return;
}
content.erase(0,1);
std::string cmd = content.substr(0, content.find(" ")); // finds first word
if(cmd == "makegang") {
tokenizer<> tok(content);
std::vector<discord::user> args;
for(tokenizer<>::iterator beg=tok.begin(); beg != tok.end(); ++beg) {
args.push_back({std::stoul(*beg)});
}
task.todo = std::bind(command_makegang, args);
(bot->command_q).push(task);
} else if(cmd == "gaygang") {
task.todo = {command_pinggang};
(bot->command_q).push(task);
} else if(cmd == "hello") {
task.todo = {command_hello};
(bot->command_q).push(task);
}
}
}
EVENT_HANDLER(MESSAGE_UPDATE){}
EVENT_HANDLER(MESSAGE_DELETE){}
EVENT_HANDLER(MESSAGE_DELETE_BULK){}
EVENT_HANDLER(MESSAGE_REACTION_ADD){}
EVENT_HANDLER(MESSAGE_REACTION_REMOVE){}
EVENT_HANDLER(MESSAGE_REACTION_REMOVE_ALL){}
EVENT_HANDLER(PRESENCE_UPDATE) {}
EVENT_HANDLER(TYPING_START) {}
EVENT_HANDLER(USER_UPDATE){}
EVENT_HANDLER(VOICE_STATE_UPDATE){}
EVENT_HANDLER(VOICE_SERVER_UPDATE){}
EVENT_HANDLER(WEBHOOKS_UPDATE){}
GATEWAY_HANDLE(DISPATCH) {
last_sequence_data++;
try {
(this->*(this->events[msg.t]))(msg);
} catch(const std::exception &e) {
std::cout << msg.t << " not found" << std::endl;
}
}
/**
* @brief: handle a heartbeat request from wss
*
* Sends a heartbeat back.
*
* @bug: will send twice because other thread will send a heartbeat too
*/
GATEWAY_HANDLE(HEARTBEAT) {
send_payload(
{
{ "op", discord::HEARTBEAT },
{ "d", this->last_sequence_data },
});
}
GATEWAY_HANDLE(IDENTIFY) {}
GATEWAY_HANDLE(STATUS_UPDATE) {}
GATEWAY_HANDLE(VOICE_UPDATE) {}
GATEWAY_HANDLE(RESUME) {}
GATEWAY_HANDLE(RECONNECT) {}
GATEWAY_HANDLE(REQUEST_GUILD_MEMBERS) {
}
GATEWAY_HANDLE(INVALID_SESS) {
bot->status = DISCONNECTED;
bot->up_to_date = false;
}
/**
* @brief: responds to the HELLO payload
*
* The initial HELLO payload provides the heartbeat_interval as well as
* the gateway server location.
*/
GATEWAY_HANDLE(HELLO) {
// this is the first time we've received a HELLO packet
if(bot->status == NEW) {
heartbeat_interval = msg.d["heartbeat_interval"].get<int>();
// the first identify payload is unique
send_payload(
{
{U("d"),{
{ U("token"), bot->token },
{ U("properties"), {
{ U("$os"), U("linux") },
{ U("$browser"), U("Discord") },
{ U("$device"), U("Discord") }}
},
{ U("compress"), false }}
},
{ U("op"), discord::IDENTIFY },
});
} else if(bot->status == DISCONNECTED) {
send_payload({
{ "token", bot->token },
{ "session_id", session_id },
{ "seq", last_sequence_data }
});
}
}
GATEWAY_HANDLE(HEARTBEAT_ACK) {
heartbeat_lock.lock();
heartbeat_ticks--;
heartbeat_lock.unlock();
}
}