-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.cpp
More file actions
48 lines (43 loc) · 1.21 KB
/
bot.cpp
File metadata and controls
48 lines (43 loc) · 1.21 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
#include "connection.hpp"
#include "endpoints.hpp"
#include "gateway.hpp"
#include "bot.hpp"
#include <string>
#include <iostream>
/**
* @brief: bot constructor
*
* Constructor for bot.
*
* @param[in]: token, the bot's token
* @param[in]: ref, a reference for the discord users to use to ping the bot
*/
Bot::Bot(std::string token, char ref)
: status{NEW}, up_to_date{true}, wss_gateway(this, false, backend::JSON), c(this), token{token}, ref{ref} {}
int Bot::run() {
try {
c.identify(); // establish the connection
std::thread wss = std::thread([this](){this->wss_gateway.run();}); // start the gateway handler
while(status == NEW); // gateway to confirm we are ready
while(status == ACTIVE) {
// sleep until notified that there is command waiting
if(!command_q.empty()) {
auto task = command_q.front();
command_q.pop(); // make the commit
curr_chan = task.channel_id;
task.todo();
}
}
wss.join();
} catch(const std::exception& e) {
std::cout << __FILE__ << __LINE__ << e.what() << std::endl;
}
return 0;
}
Bot::~Bot() {
status = TERMINATE;
wss_gateway.close();
}
void Bot::create_message(std::string msg) {
c.create_message(msg);
}