Add config, neaten up some things
This commit is contained in:
parent
da93eda0be
commit
4b2d0ee50a
3
.gitignore
vendored
3
.gitignore
vendored
@ -10,5 +10,8 @@
|
|||||||
/TriviaBot/data_management/questions
|
/TriviaBot/data_management/questions
|
||||||
/TriviaBot/bot/db/trivia.db
|
/TriviaBot/bot/db/trivia.db
|
||||||
|
|
||||||
|
# Config file
|
||||||
|
config.json
|
||||||
|
|
||||||
# Compiled sqlite file
|
# Compiled sqlite file
|
||||||
sqlite3.obj
|
sqlite3.obj
|
60
TriviaBot/bot/BotConfig.cpp
Normal file
60
TriviaBot/bot/BotConfig.cpp
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include "BotConfig.hpp"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
|
#include "json/json.hpp"
|
||||||
|
|
||||||
|
#include "Logger.hpp"
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
BotConfig::BotConfig() {
|
||||||
|
is_new_config = false;
|
||||||
|
std::stringstream ss;
|
||||||
|
|
||||||
|
std::ifstream config_file("config.json");
|
||||||
|
if(!config_file) {
|
||||||
|
config_file.close();
|
||||||
|
create_new_file();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ss << config_file.rdbuf();
|
||||||
|
config_file.close();
|
||||||
|
std::string config = ss.str();
|
||||||
|
load_from_json(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BotConfig::load_from_json(std::string data) {
|
||||||
|
json parsed = json::parse(data);
|
||||||
|
|
||||||
|
token = parsed.value("bot_token", "");
|
||||||
|
owner_id = parsed.value("owner_id", "");
|
||||||
|
cert_location = parsed.value("api_cert_file", "bot/http/DiscordCA.crt");
|
||||||
|
|
||||||
|
createjs_roles = parsed["v8"].value("createjs_allowed_roles", std::unordered_set<std::string> { "Admin", "Coder" });
|
||||||
|
|
||||||
|
Logger::write("config.json file loaded", Logger::LogLevel::Info);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BotConfig::create_new_file() {
|
||||||
|
std::string config = json {
|
||||||
|
{ "bot_token", "" },
|
||||||
|
{ "owner_id", "" },
|
||||||
|
{ "api_cert_file", "bot/http/DiscordCA.crt" },
|
||||||
|
{ "v8", {
|
||||||
|
{ "createjs_allowed_roles", {
|
||||||
|
"Admin", "Coder", "Bot Commander"
|
||||||
|
} }
|
||||||
|
} }
|
||||||
|
}.dump(4);
|
||||||
|
|
||||||
|
std::ofstream config_file("config.json");
|
||||||
|
config_file << config;
|
||||||
|
config_file.close();
|
||||||
|
|
||||||
|
Logger::write("Created new config.json file", Logger::LogLevel::Info);
|
||||||
|
is_new_config = true;
|
||||||
|
}
|
23
TriviaBot/bot/BotConfig.hpp
Normal file
23
TriviaBot/bot/BotConfig.hpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef BOT_BOTCONFIG
|
||||||
|
#define BOT_BOTCONFIG
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
class BotConfig {
|
||||||
|
public:
|
||||||
|
BotConfig();
|
||||||
|
|
||||||
|
bool is_new_config;
|
||||||
|
|
||||||
|
std::string token;
|
||||||
|
std::string owner_id;
|
||||||
|
std::string cert_location;
|
||||||
|
std::unordered_set<std::string> createjs_roles;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void load_from_json(std::string data);
|
||||||
|
void create_new_file();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -3,60 +3,58 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "GatewayHandler.hpp"
|
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
|
#include "BotConfig.hpp"
|
||||||
|
|
||||||
ClientConnection::ClientConnection() {
|
ClientConnection::ClientConnection(BotConfig &c) : config(c), gh(config) {
|
||||||
// Reset the log channels
|
// Reset the log channels
|
||||||
c.clear_access_channels(websocketpp::log::alevel::all);
|
cli.clear_access_channels(websocketpp::log::alevel::all);
|
||||||
|
|
||||||
// Only want application logging, logging from the initial connection stages or any error logging
|
// Only want application logging, logging from the initial connection stages or any error logging
|
||||||
c.set_access_channels(websocketpp::log::alevel::app | websocketpp::log::alevel::connect);
|
cli.set_access_channels(websocketpp::log::alevel::app | websocketpp::log::alevel::connect);
|
||||||
c.set_error_channels(websocketpp::log::elevel::all);
|
cli.set_error_channels(websocketpp::log::elevel::all);
|
||||||
|
|
||||||
// Initialize ASIO
|
// Initialize ASIO
|
||||||
c.init_asio();
|
cli.init_asio();
|
||||||
|
|
||||||
// Bind handlers
|
// Bind handlers
|
||||||
c.set_socket_init_handler(bind(
|
cli.set_socket_init_handler(bind(
|
||||||
&ClientConnection::on_socket_init,
|
&ClientConnection::on_socket_init,
|
||||||
this,
|
this,
|
||||||
websocketpp::lib::placeholders::_1
|
websocketpp::lib::placeholders::_1
|
||||||
));
|
));
|
||||||
c.set_tls_init_handler(bind<context_ptr>(
|
cli.set_tls_init_handler(bind<context_ptr>(
|
||||||
&ClientConnection::on_tls_init,
|
&ClientConnection::on_tls_init,
|
||||||
this,
|
this,
|
||||||
websocketpp::lib::placeholders::_1
|
websocketpp::lib::placeholders::_1
|
||||||
));
|
));
|
||||||
c.set_message_handler(bind(
|
cli.set_message_handler(bind(
|
||||||
&ClientConnection::on_message,
|
&ClientConnection::on_message,
|
||||||
this,
|
this,
|
||||||
websocketpp::lib::placeholders::_1,
|
websocketpp::lib::placeholders::_1,
|
||||||
websocketpp::lib::placeholders::_2
|
websocketpp::lib::placeholders::_2
|
||||||
));
|
));
|
||||||
c.set_open_handler(bind(
|
cli.set_open_handler(bind(
|
||||||
&ClientConnection::on_open,
|
&ClientConnection::on_open,
|
||||||
this,
|
this,
|
||||||
websocketpp::lib::placeholders::_1
|
websocketpp::lib::placeholders::_1
|
||||||
));
|
));
|
||||||
c.set_close_handler(bind(
|
cli.set_close_handler(bind(
|
||||||
&ClientConnection::on_close,
|
&ClientConnection::on_close,
|
||||||
this,
|
this,
|
||||||
websocketpp::lib::placeholders::_1
|
websocketpp::lib::placeholders::_1
|
||||||
));
|
));
|
||||||
c.set_fail_handler(bind(
|
cli.set_fail_handler(bind(
|
||||||
&ClientConnection::on_fail,
|
&ClientConnection::on_fail,
|
||||||
this,
|
this,
|
||||||
websocketpp::lib::placeholders::_1
|
websocketpp::lib::placeholders::_1
|
||||||
));
|
));
|
||||||
|
|
||||||
gh = std::make_unique<GatewayHandler>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open a connection to the URI provided
|
// Open a connection to the URI provided
|
||||||
void ClientConnection::start(std::string uri) {
|
void ClientConnection::start(std::string uri) {
|
||||||
websocketpp::lib::error_code ec;
|
websocketpp::lib::error_code ec;
|
||||||
client::connection_ptr con = c.get_connection(uri, ec);
|
client::connection_ptr con = cli.get_connection(uri, ec);
|
||||||
|
|
||||||
if (ec) { // failed to create connection
|
if (ec) { // failed to create connection
|
||||||
Logger::write("Failed to create connection: " + ec.message(), Logger::LogLevel::Severe);
|
Logger::write("Failed to create connection: " + ec.message(), Logger::LogLevel::Severe);
|
||||||
@ -64,8 +62,8 @@ void ClientConnection::start(std::string uri) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Open the connection
|
// Open the connection
|
||||||
c.connect(con);
|
cli.connect(con);
|
||||||
c.run();
|
cli.run();
|
||||||
|
|
||||||
Logger::write("Finished running", Logger::LogLevel::Debug);
|
Logger::write("Finished running", Logger::LogLevel::Debug);
|
||||||
}
|
}
|
||||||
@ -91,7 +89,7 @@ context_ptr ClientConnection::on_tls_init(websocketpp::connection_hdl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ClientConnection::on_fail(websocketpp::connection_hdl hdl) {
|
void ClientConnection::on_fail(websocketpp::connection_hdl hdl) {
|
||||||
client::connection_ptr con = c.get_con_from_hdl(hdl);
|
client::connection_ptr con = cli.get_con_from_hdl(hdl);
|
||||||
|
|
||||||
// Print as much information as possible
|
// Print as much information as possible
|
||||||
Logger::write("Fail handler: \n" +
|
Logger::write("Fail handler: \n" +
|
||||||
@ -117,10 +115,10 @@ void ClientConnection::on_message(websocketpp::connection_hdl hdl, message_ptr m
|
|||||||
|
|
||||||
|
|
||||||
// Pass the message to the gateway handler
|
// Pass the message to the gateway handler
|
||||||
gh->handle_data(message->get_payload(), c, hdl);
|
gh.handle_data(message->get_payload(), cli, hdl);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientConnection::on_close(websocketpp::connection_hdl) {
|
void ClientConnection::on_close(websocketpp::connection_hdl) {
|
||||||
Logger::write("Connection closed", Logger::LogLevel::Info);
|
Logger::write("Connection closed", Logger::LogLevel::Info);
|
||||||
c.stop();
|
cli.stop();
|
||||||
}
|
}
|
@ -5,6 +5,8 @@
|
|||||||
#include <websocketpp/config/asio_client.hpp>
|
#include <websocketpp/config/asio_client.hpp>
|
||||||
#include "json/json.hpp"
|
#include "json/json.hpp"
|
||||||
|
|
||||||
|
#include "GatewayHandler.hpp"
|
||||||
|
|
||||||
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
|
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
|
||||||
|
|
||||||
using websocketpp::lib::bind;
|
using websocketpp::lib::bind;
|
||||||
@ -14,15 +16,20 @@ typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
|
|||||||
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
|
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
|
||||||
typedef client::connection_ptr connection_ptr;
|
typedef client::connection_ptr connection_ptr;
|
||||||
|
|
||||||
#include "GatewayHandler.hpp"
|
class BotConfig;
|
||||||
|
|
||||||
class ClientConnection {
|
class ClientConnection {
|
||||||
public:
|
public:
|
||||||
ClientConnection();
|
ClientConnection(BotConfig &c);
|
||||||
|
|
||||||
// Open a connection to the URI provided
|
// Open a connection to the URI provided
|
||||||
void start(std::string uri);
|
void start(std::string uri);
|
||||||
|
|
||||||
|
private:
|
||||||
|
client cli;
|
||||||
|
BotConfig &config;
|
||||||
|
GatewayHandler gh;
|
||||||
|
|
||||||
// Event handlers
|
// Event handlers
|
||||||
void on_socket_init(websocketpp::connection_hdl);
|
void on_socket_init(websocketpp::connection_hdl);
|
||||||
context_ptr on_tls_init(websocketpp::connection_hdl);
|
context_ptr on_tls_init(websocketpp::connection_hdl);
|
||||||
@ -30,10 +37,6 @@ public:
|
|||||||
void on_open(websocketpp::connection_hdl hdl);
|
void on_open(websocketpp::connection_hdl hdl);
|
||||||
void on_message(websocketpp::connection_hdl hdl, message_ptr message);
|
void on_message(websocketpp::connection_hdl hdl, message_ptr message);
|
||||||
void on_close(websocketpp::connection_hdl);
|
void on_close(websocketpp::connection_hdl);
|
||||||
|
|
||||||
private:
|
|
||||||
client c;
|
|
||||||
std::unique_ptr<GatewayHandler> gh;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -4,7 +4,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
#include "http/HTTPHelper.hpp"
|
#include "http/HTTP.hpp"
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
@ -16,7 +16,7 @@ namespace DiscordAPI {
|
|||||||
|
|
||||||
const std::string json_mime_type = "application/json";
|
const std::string json_mime_type = "application/json";
|
||||||
|
|
||||||
void send_message(std::string channel_id, std::string message) {
|
void send_message(std::string channel_id, std::string message, std::string token, std::string ca_location) {
|
||||||
if (message == "") {
|
if (message == "") {
|
||||||
Logger::write("[API] [send_message] Tried to send empty message", Logger::LogLevel::Warning);
|
Logger::write("[API] [send_message] Tried to send empty message", Logger::LogLevel::Warning);
|
||||||
return;
|
return;
|
||||||
@ -31,9 +31,9 @@ namespace DiscordAPI {
|
|||||||
|
|
||||||
std::string first = message.substr(0, 2000);
|
std::string first = message.substr(0, 2000);
|
||||||
std::string second = message.substr(2000);
|
std::string second = message.substr(2000);
|
||||||
send_message(channel_id, first);
|
send_message(channel_id, first, token, ca_location);
|
||||||
std::this_thread::sleep_for(50ms);
|
std::this_thread::sleep_for(50ms);
|
||||||
send_message(channel_id, second);
|
send_message(channel_id, second, token, ca_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,14 +44,14 @@ namespace DiscordAPI {
|
|||||||
|
|
||||||
std::string response;
|
std::string response;
|
||||||
long response_code = 0;
|
long response_code = 0;
|
||||||
response = HTTP::post_request(url, json_mime_type, data.dump(), &response_code);
|
response = HTTP::post_request(url, json_mime_type, data.dump(), &response_code, token, ca_location);
|
||||||
|
|
||||||
int retries = 0;
|
int retries = 0;
|
||||||
while (response_code != 200 && retries < 2) {
|
while (response_code != 200 && retries < 2) {
|
||||||
Logger::write("[API] [send_message] Got non-200 response code, retrying", Logger::LogLevel::Warning);
|
Logger::write("[API] [send_message] Got non-200 response code, retrying", Logger::LogLevel::Warning);
|
||||||
std::this_thread::sleep_for(100ms);
|
std::this_thread::sleep_for(100ms);
|
||||||
// try 3 times. usually enough to prevent 502 bad gateway issues
|
// try 3 times. usually enough to prevent 502 bad gateway issues
|
||||||
response = HTTP::post_request(url, json_mime_type, data.dump(), &response_code);
|
response = HTTP::post_request(url, json_mime_type, data.dump(), &response_code, token, ca_location);
|
||||||
retries++;
|
retries++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,17 +60,17 @@ namespace DiscordAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
json get_gateway() {
|
json get_gateway(std::string ca_location) {
|
||||||
std::string response;
|
std::string response;
|
||||||
long response_code;
|
long response_code;
|
||||||
response = HTTP::get_request(gateway_url, &response_code);
|
response = HTTP::get_request(gateway_url, &response_code, "", ca_location);
|
||||||
|
|
||||||
int retries = 0;
|
int retries = 0;
|
||||||
while (response_code != 200 && retries < 4) {
|
while (response_code != 200 && retries < 4) {
|
||||||
Logger::write("[API] [get_gateway] Got non-200 response code, retrying", Logger::LogLevel::Warning);
|
Logger::write("[API] [get_gateway] Got non-200 response code, retrying", Logger::LogLevel::Warning);
|
||||||
std::this_thread::sleep_for(100ms);
|
std::this_thread::sleep_for(100ms);
|
||||||
// try 3 times. usually enough to prevent 502 bad gateway issues
|
// try 3 times. usually enough to prevent 502 bad gateway issues
|
||||||
response = HTTP::get_request(gateway_url, &response_code);
|
response = HTTP::get_request(gateway_url, &response_code, "", ca_location);
|
||||||
retries++;
|
retries++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,11 +7,11 @@
|
|||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
class HTTPHelper;
|
class BotConfig;
|
||||||
|
|
||||||
namespace DiscordAPI {
|
namespace DiscordAPI {
|
||||||
json get_gateway();
|
json get_gateway(std::string ca_location);
|
||||||
void send_message(std::string channel_id, std::string message);
|
void send_message(std::string channel_id, std::string message, std::string token, std::string ca_location);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -5,10 +5,9 @@
|
|||||||
#include "DiscordAPI.hpp"
|
#include "DiscordAPI.hpp"
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
#include "data_structures/GuildMember.hpp"
|
#include "data_structures/GuildMember.hpp"
|
||||||
|
#include "BotConfig.hpp"
|
||||||
|
|
||||||
extern std::string bot_token;
|
GatewayHandler::GatewayHandler(BotConfig &c) : config(c) {
|
||||||
|
|
||||||
GatewayHandler::GatewayHandler() {
|
|
||||||
last_seq = 0;
|
last_seq = 0;
|
||||||
|
|
||||||
CommandHelper::init();
|
CommandHelper::init();
|
||||||
@ -59,7 +58,7 @@ void GatewayHandler::send_identify(client &c, websocketpp::connection_hdl &hdl)
|
|||||||
json identify = {
|
json identify = {
|
||||||
{ "op", 2 },
|
{ "op", 2 },
|
||||||
{ "d", {
|
{ "d", {
|
||||||
{ "token", bot_token },
|
{ "token", config.token },
|
||||||
{ "properties",{
|
{ "properties",{
|
||||||
{ "$browser", "Microsoft Windows 10" },
|
{ "$browser", "Microsoft Windows 10" },
|
||||||
{ "$device", "TriviaBot-0.0" },
|
{ "$device", "TriviaBot-0.0" },
|
||||||
@ -239,7 +238,7 @@ void GatewayHandler::on_event_guild_create(json data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (v8_instances.count(guild.id) == 0) {
|
if (v8_instances.count(guild.id) == 0) {
|
||||||
v8_instances[guild.id] = std::make_unique<V8Instance>(guild.id, &guilds, &channels, &users, &roles);
|
v8_instances[guild.id] = std::make_unique<V8Instance>(config, guild.id, &guilds, &channels, &users, &roles);
|
||||||
Logger::write("Created v8 instance for guild " + guild.id, Logger::LogLevel::Debug);
|
Logger::write("Created v8 instance for guild " + guild.id, Logger::LogLevel::Debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -465,7 +464,7 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
int delay = 8;
|
int delay = 8;
|
||||||
|
|
||||||
if (words.size() > 3) {
|
if (words.size() > 3) {
|
||||||
DiscordAPI::send_message(channel.id, ":exclamation: Invalid arguments!");
|
DiscordAPI::send_message(channel.id, ":exclamation: Invalid arguments!", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (words.size() > 1) {
|
else if (words.size() > 1) {
|
||||||
@ -475,7 +474,7 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
help += "\\`trivia **stop**: stops the ongoing game.\n";
|
help += "\\`trivia **stop**: stops the ongoing game.\n";
|
||||||
help += "\\`trivia **help**: prints this message\n";
|
help += "\\`trivia **help**: prints this message\n";
|
||||||
|
|
||||||
DiscordAPI::send_message(channel.id, help);
|
DiscordAPI::send_message(channel.id, help, config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (words[1] == "stop" || words[1] == "s") {
|
else if (words[1] == "stop" || words[1] == "s") {
|
||||||
@ -483,7 +482,7 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
delete_game(channel.id);
|
delete_game(channel.id);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
DiscordAPI::send_message(channel.id, ":warning: Couldn't find an ongoing trivia game for this channel.");
|
DiscordAPI::send_message(channel.id, ":warning: Couldn't find an ongoing trivia game for this channel.", config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -495,13 +494,13 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (std::invalid_argument e) {
|
catch (std::invalid_argument e) {
|
||||||
DiscordAPI::send_message(channel.id, ":exclamation: Invalid arguments!");
|
DiscordAPI::send_message(channel.id, ":exclamation: Invalid arguments!", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
games[channel.id] = std::make_unique<TriviaGame>(this, channel.id, questions, delay);
|
games[channel.id] = std::make_unique<TriviaGame>(config, this, channel.id, questions, delay);
|
||||||
games[channel.id]->start();
|
games[channel.id]->start();
|
||||||
}
|
}
|
||||||
else if (words[0] == "`guilds") {
|
else if (words[0] == "`guilds") {
|
||||||
@ -509,10 +508,10 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
for (auto &gu : guilds) {
|
for (auto &gu : guilds) {
|
||||||
m += ":small_orange_diamond: " + gu.second.name + " (" + gu.second.id + ") Channels: " + std::to_string(gu.second.channels.size()) + "\n";
|
m += ":small_orange_diamond: " + gu.second.name + " (" + gu.second.id + ") Channels: " + std::to_string(gu.second.channels.size()) + "\n";
|
||||||
}
|
}
|
||||||
DiscordAPI::send_message(channel.id, m);
|
DiscordAPI::send_message(channel.id, m, config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
else if (words[0] == "`info") {
|
else if (words[0] == "`info") {
|
||||||
DiscordAPI::send_message(channel.id, ":information_source: **trivia-bot** by Jack. <http://github.com/jackb-p/TriviaDiscord>");
|
DiscordAPI::send_message(channel.id, ":information_source: **trivia-bot** by Jack. <http://github.com/jackb-p/TriviaDiscord>", config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
else if (words[0] == "~js" && words.size() > 1) {
|
else if (words[0] == "~js" && words.size() > 1) {
|
||||||
DiscordObjects::GuildMember *member = *std::find_if(guild.members.begin(), guild.members.end(), [sender](DiscordObjects::GuildMember *m) {
|
DiscordObjects::GuildMember *member = *std::find_if(guild.members.begin(), guild.members.end(), [sender](DiscordObjects::GuildMember *m) {
|
||||||
@ -526,12 +525,13 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
}
|
}
|
||||||
else if (words[0] == "~createjs" && words.size() > 1) {
|
else if (words[0] == "~createjs" && words.size() > 1) {
|
||||||
auto &member = *std::find_if(guild.members.begin(), guild.members.end(), [sender](DiscordObjects::GuildMember *m) { return sender.id == m->user->id; });
|
auto &member = *std::find_if(guild.members.begin(), guild.members.end(), [sender](DiscordObjects::GuildMember *m) { return sender.id == m->user->id; });
|
||||||
bool allowed = std::find_if(member->roles.begin(), member->roles.end(), [](DiscordObjects::Role *r) {
|
BotConfig &conf = config;
|
||||||
return r->name == "Admin" || r->name == "Moderator" || r->name == "Coder"; // TODO: customisation here
|
bool disallowed = std::find_if(member->roles.begin(), member->roles.end(), [conf](DiscordObjects::Role *r) -> bool {
|
||||||
|
return conf.createjs_roles.count(r->name);
|
||||||
}) == member->roles.end(); // checks if the user has the required roles
|
}) == member->roles.end(); // checks if the user has the required roles
|
||||||
|
|
||||||
if (!allowed) {
|
if (disallowed) {
|
||||||
DiscordAPI::send_message(channel.id, ":warning: You do not have permission to use this command.");
|
DiscordAPI::send_message(channel.id, ":warning: You do not have permission to use this command.", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -543,16 +543,16 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
int result = CommandHelper::insert_command(channel.guild_id, command_name, script);
|
int result = CommandHelper::insert_command(channel.guild_id, command_name, script);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case 0:
|
case 0:
|
||||||
DiscordAPI::send_message(channel.id, ":warning: Error!"); break;
|
DiscordAPI::send_message(channel.id, ":warning: Error!", config.token, config.cert_location); break;
|
||||||
case 1:
|
case 1:
|
||||||
DiscordAPI::send_message(channel.id, ":new: Command `" + command_name + "` successfully created."); break;
|
DiscordAPI::send_message(channel.id, ":new: Command `" + command_name + "` successfully created.", config.token, config.cert_location); break;
|
||||||
case 2:
|
case 2:
|
||||||
DiscordAPI::send_message(channel.id, ":arrow_heading_up: Command `" + command_name + "` successfully updated."); break;
|
DiscordAPI::send_message(channel.id, ":arrow_heading_up: Command `" + command_name + "` successfully updated.", config.token, config.cert_location); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (words[0] == "`shutdown" && sender.id == "82232146579689472") { // it me
|
else if (words[0] == "`shutdown" && sender.id == "82232146579689472") { // it me
|
||||||
DiscordAPI::send_message(channel.id, ":zzz: Goodbye!");
|
DiscordAPI::send_message(channel.id, ":zzz: Goodbye!", config.token, config.cert_location);
|
||||||
for (auto &game : games) {
|
for (auto &game : games) {
|
||||||
delete_game(game.first);
|
delete_game(game.first);
|
||||||
}
|
}
|
||||||
@ -563,25 +563,25 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
if (words[1] == "channel" && words.size() == 3) {
|
if (words[1] == "channel" && words.size() == 3) {
|
||||||
auto it = channels.find(words[2]);
|
auto it = channels.find(words[2]);
|
||||||
if (it == channels.end()) {
|
if (it == channels.end()) {
|
||||||
DiscordAPI::send_message(channel.id, ":question: Unrecognised channel.");
|
DiscordAPI::send_message(channel.id, ":question: Unrecognised channel.", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiscordAPI::send_message(channel.id, it->second.to_debug_string());
|
DiscordAPI::send_message(channel.id, it->second.to_debug_string(), config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
else if (words[1] == "guild" && words.size() == 3) {
|
else if (words[1] == "guild" && words.size() == 3) {
|
||||||
auto it = guilds.find(words[2]);
|
auto it = guilds.find(words[2]);
|
||||||
if (it == guilds.end()) {
|
if (it == guilds.end()) {
|
||||||
DiscordAPI::send_message(channel.id, ":question: Unrecognised guild.");
|
DiscordAPI::send_message(channel.id, ":question: Unrecognised guild.", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiscordAPI::send_message(channel.id, it->second.to_debug_string());
|
DiscordAPI::send_message(channel.id, it->second.to_debug_string(), config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
else if (words[1] == "member" && words.size() == 4) {
|
else if (words[1] == "member" && words.size() == 4) {
|
||||||
auto it = guilds.find(words[2]);
|
auto it = guilds.find(words[2]);
|
||||||
if (it == guilds.end()) {
|
if (it == guilds.end()) {
|
||||||
DiscordAPI::send_message(channel.id, ":question: Unrecognised guild.");
|
DiscordAPI::send_message(channel.id, ":question: Unrecognised guild.", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -590,27 +590,27 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
return user_id == member->user->id;
|
return user_id == member->user->id;
|
||||||
});
|
});
|
||||||
if (it2 == it->second.members.end()) {
|
if (it2 == it->second.members.end()) {
|
||||||
DiscordAPI::send_message(channel.id, ":question: Unrecognised user.");
|
DiscordAPI::send_message(channel.id, ":question: Unrecognised user.", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiscordAPI::send_message(channel.id, (*it2)->to_debug_string());
|
DiscordAPI::send_message(channel.id, (*it2)->to_debug_string(), config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
else if (words[1] == "role" && words.size() == 3) {
|
else if (words[1] == "role" && words.size() == 3) {
|
||||||
auto it = roles.find(words[2]);
|
auto it = roles.find(words[2]);
|
||||||
if (it == roles.end()) {
|
if (it == roles.end()) {
|
||||||
DiscordAPI::send_message(channel.id, ":question: Unrecognised role.");
|
DiscordAPI::send_message(channel.id, ":question: Unrecognised role.", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiscordAPI::send_message(channel.id, it->second.to_debug_string());
|
DiscordAPI::send_message(channel.id, it->second.to_debug_string(), config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
else if (words[1] == "role" && words.size() == 4) {
|
else if (words[1] == "role" && words.size() == 4) {
|
||||||
std::string role_name = words[3];
|
std::string role_name = words[3];
|
||||||
|
|
||||||
auto it = guilds.find(words[2]);
|
auto it = guilds.find(words[2]);
|
||||||
if (it == guilds.end()) {
|
if (it == guilds.end()) {
|
||||||
DiscordAPI::send_message(channel.id, ":question: Unrecognised guild.");
|
DiscordAPI::send_message(channel.id, ":question: Unrecognised guild.", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -618,14 +618,14 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
return role_name == r->name;
|
return role_name == r->name;
|
||||||
});
|
});
|
||||||
if (it2 == it->second.roles.end()) {
|
if (it2 == it->second.roles.end()) {
|
||||||
DiscordAPI::send_message(channel.id, ":question: Unrecognised role.");
|
DiscordAPI::send_message(channel.id, ":question: Unrecognised role.", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiscordAPI::send_message(channel.id, (*it2)->to_debug_string());
|
DiscordAPI::send_message(channel.id, (*it2)->to_debug_string(), config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
DiscordAPI::send_message(channel.id, ":question: Unknown parameters.");
|
DiscordAPI::send_message(channel.id, ":question: Unknown parameters.", config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (CommandHelper::get_command(channel.guild_id, words[0], custom_command)) {
|
else if (CommandHelper::get_command(channel.guild_id, words[0], custom_command)) {
|
||||||
@ -635,13 +635,13 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (custom_command.script.length() == 0) {
|
if (custom_command.script.length() == 0) {
|
||||||
DiscordAPI::send_message(channel.id, ":warning: Script has 0 length.");
|
DiscordAPI::send_message(channel.id, ":warning: Script has 0 length.", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto it = v8_instances.find(channel.guild_id);
|
auto it = v8_instances.find(channel.guild_id);
|
||||||
if (it == v8_instances.end()) {
|
if (it == v8_instances.end()) {
|
||||||
DiscordAPI::send_message(channel.id, ":warning: No V8 instance exists for this server - it's our fault not yours!");
|
DiscordAPI::send_message(channel.id, ":warning: No V8 instance exists for this server - it's our fault not yours!", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,17 +37,19 @@ using json = nlohmann::json;
|
|||||||
*****************************************************************************************************************************/
|
*****************************************************************************************************************************/
|
||||||
|
|
||||||
class TriviaGame;
|
class TriviaGame;
|
||||||
class APIHelper;
|
class BotConfig;
|
||||||
|
|
||||||
class GatewayHandler {
|
class GatewayHandler {
|
||||||
public:
|
public:
|
||||||
GatewayHandler();
|
GatewayHandler(BotConfig &c);
|
||||||
|
|
||||||
void handle_data(std::string data, client &c, websocketpp::connection_hdl &hdl);
|
void handle_data(std::string data, client &c, websocketpp::connection_hdl &hdl);
|
||||||
|
|
||||||
void delete_game(std::string channel_id);
|
void delete_game(std::string channel_id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
BotConfig &config;
|
||||||
|
|
||||||
int last_seq;
|
int last_seq;
|
||||||
int heartbeat_interval;
|
int heartbeat_interval;
|
||||||
|
|
||||||
|
@ -8,10 +8,15 @@
|
|||||||
#include "ClientConnection.hpp"
|
#include "ClientConnection.hpp"
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
#include "DiscordAPI.hpp"
|
#include "DiscordAPI.hpp"
|
||||||
|
#include "BotConfig.hpp"
|
||||||
std::string bot_token;
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
BotConfig config;
|
||||||
|
if (config.is_new_config) {
|
||||||
|
Logger::write("Since the config.json file is newly generated, the program will exit now to allow you to edit it.", Logger::LogLevel::Info);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||||
|
|
||||||
v8::V8::InitializeICUDefaultLocation(argv[0]);
|
v8::V8::InitializeICUDefaultLocation(argv[0]);
|
||||||
@ -22,16 +27,8 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
Logger::write("Initialised V8 and curl", Logger::LogLevel::Debug);
|
Logger::write("Initialised V8 and curl", Logger::LogLevel::Debug);
|
||||||
|
|
||||||
if (argc == 2) {
|
|
||||||
bot_token = argv[1];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::cout << "Please enter your bot token: " << std::endl;
|
|
||||||
std::cin >> bot_token;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string args = "/?v=5&encoding=json";
|
std::string args = "/?v=5&encoding=json";
|
||||||
std::string url = DiscordAPI::get_gateway().value("url", "wss://gateway.discord.gg");
|
std::string url = DiscordAPI::get_gateway(config.cert_location).value("url", "wss://gateway.discord.gg");
|
||||||
|
|
||||||
bool retry = true;
|
bool retry = true;
|
||||||
int exit_code = 0;
|
int exit_code = 0;
|
||||||
@ -39,7 +36,7 @@ int main(int argc, char *argv[]) {
|
|||||||
retry = false;
|
retry = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ClientConnection conn;
|
ClientConnection conn(config);
|
||||||
conn.start(url + args);
|
conn.start(url + args);
|
||||||
}
|
}
|
||||||
catch (const std::exception &e) {
|
catch (const std::exception &e) {
|
||||||
@ -65,5 +62,6 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
Logger::write("Cleaned up", Logger::LogLevel::Info);
|
Logger::write("Cleaned up", Logger::LogLevel::Info);
|
||||||
|
|
||||||
|
std::getchar();
|
||||||
return exit_code;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,9 @@
|
|||||||
#include "DiscordAPI.hpp"
|
#include "DiscordAPI.hpp"
|
||||||
#include "data_structures/User.hpp"
|
#include "data_structures/User.hpp"
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
|
#include "BotConfig.hpp"
|
||||||
|
|
||||||
TriviaGame::TriviaGame(GatewayHandler *gh, std::string channel_id, int total_questions, int delay) : interval(delay) {
|
TriviaGame::TriviaGame(BotConfig &c, GatewayHandler *gh, std::string channel_id, int total_questions, int delay) : interval(delay), config(c) {
|
||||||
this->gh = gh;
|
this->gh = gh;
|
||||||
this->channel_id = channel_id;
|
this->channel_id = channel_id;
|
||||||
|
|
||||||
@ -26,7 +27,7 @@ TriviaGame::~TriviaGame() {
|
|||||||
current_thread.reset();
|
current_thread.reset();
|
||||||
|
|
||||||
if (scores.size() == 0) {
|
if (scores.size() == 0) {
|
||||||
DiscordAPI::send_message(channel_id, ":red_circle: Game cancelled!");
|
DiscordAPI::send_message(channel_id, ":red_circle: Game cancelled!", config.token, config.cert_location);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +51,7 @@ TriviaGame::~TriviaGame() {
|
|||||||
average_time.pop_back(); average_time.pop_back(); average_time.pop_back();
|
average_time.pop_back(); average_time.pop_back(); average_time.pop_back();
|
||||||
message += ":small_blue_diamond: <@!" + p.first + ">: " + std::to_string(p.second) + " (Avg: " + average_time + " seconds)\n";
|
message += ":small_blue_diamond: <@!" + p.first + ">: " + std::to_string(p.second) + " (Avg: " + average_time + " seconds)\n";
|
||||||
}
|
}
|
||||||
DiscordAPI::send_message(channel_id, message);
|
DiscordAPI::send_message(channel_id, message, config.token, config.cert_location);
|
||||||
|
|
||||||
sqlite3 *db; int rc; std::string sql;
|
sqlite3 *db; int rc; std::string sql;
|
||||||
|
|
||||||
@ -213,7 +214,8 @@ void TriviaGame::question() {
|
|||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
|
|
||||||
questions_asked++;
|
questions_asked++;
|
||||||
DiscordAPI::send_message(channel_id, ":question: **(" + std::to_string(questions_asked) + "/" + std::to_string(total_questions) + ")** " + current_question);
|
DiscordAPI::send_message(channel_id, ":question: **(" + std::to_string(questions_asked) + "/" + std::to_string(total_questions) + ")** " + current_question,
|
||||||
|
config.token, config.cert_location);
|
||||||
question_start = boost::posix_time::microsec_clock::universal_time();
|
question_start = boost::posix_time::microsec_clock::universal_time();
|
||||||
|
|
||||||
give_hint(0, "");
|
give_hint(0, "");
|
||||||
@ -280,11 +282,11 @@ void TriviaGame::give_hint(int hints_given, std::string hint) {
|
|||||||
hints_given++; // now equal to the amount of [hide_char]s that need to be present in each word
|
hints_given++; // now equal to the amount of [hide_char]s that need to be present in each word
|
||||||
|
|
||||||
if (print) {
|
if (print) {
|
||||||
DiscordAPI::send_message(channel_id, ":small_orange_diamond: Hint: **`" + hint + "`**");
|
DiscordAPI::send_message(channel_id, ":small_orange_diamond: Hint: **`" + hint + "`**", config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
boost::this_thread::sleep(interval);
|
boost::this_thread::sleep(interval);
|
||||||
DiscordAPI::send_message(channel_id, ":exclamation: Question failed. Answer: ** `" + *current_answers.begin() + "` **");
|
DiscordAPI::send_message(channel_id, ":exclamation: Question failed. Answer: ** `" + *current_answers.begin() + "` **", config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TriviaGame::handle_answer(std::string answer, DiscordObjects::User sender) {
|
void TriviaGame::handle_answer(std::string answer, DiscordObjects::User sender) {
|
||||||
@ -299,7 +301,7 @@ void TriviaGame::handle_answer(std::string answer, DiscordObjects::User sender)
|
|||||||
// remove the last three 0s
|
// remove the last three 0s
|
||||||
time_taken.pop_back(); time_taken.pop_back(); time_taken.pop_back();
|
time_taken.pop_back(); time_taken.pop_back(); time_taken.pop_back();
|
||||||
|
|
||||||
DiscordAPI::send_message(channel_id, ":heavy_check_mark: <@!" + sender.id + "> You got it! (" + time_taken + " seconds)");
|
DiscordAPI::send_message(channel_id, ":heavy_check_mark: <@!" + sender.id + "> You got it! (" + time_taken + " seconds)", config.token, config.cert_location);
|
||||||
|
|
||||||
increase_score(sender.id);
|
increase_score(sender.id);
|
||||||
update_average_time(sender.id, diff.total_milliseconds());
|
update_average_time(sender.id, diff.total_milliseconds());
|
||||||
|
@ -11,14 +11,14 @@
|
|||||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||||
|
|
||||||
class GatewayHandler;
|
class GatewayHandler;
|
||||||
|
class BotConfig;
|
||||||
namespace DiscordObjects {
|
namespace DiscordObjects {
|
||||||
class User;
|
class User;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class TriviaGame {
|
class TriviaGame {
|
||||||
public:
|
public:
|
||||||
TriviaGame(GatewayHandler *gh, std::string channel_id, int total_questions, int delay);
|
TriviaGame(BotConfig &c, GatewayHandler *gh, std::string channel_id, int total_questions, int delay);
|
||||||
~TriviaGame();
|
~TriviaGame();
|
||||||
|
|
||||||
void start();
|
void start();
|
||||||
@ -26,6 +26,8 @@ public:
|
|||||||
void handle_answer(std::string answer, DiscordObjects::User sender);
|
void handle_answer(std::string answer, DiscordObjects::User sender);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
BotConfig &config;
|
||||||
|
|
||||||
int questions_asked;
|
int questions_asked;
|
||||||
int total_questions;
|
int total_questions;
|
||||||
boost::posix_time::seconds interval;
|
boost::posix_time::seconds interval;
|
||||||
|
@ -1,20 +1,18 @@
|
|||||||
#include "HTTPHelper.hpp"
|
#include "HTTP.hpp"
|
||||||
|
|
||||||
#include "../Logger.hpp"
|
#include "../Logger.hpp"
|
||||||
|
#include "../BotConfig.hpp"
|
||||||
extern std::string bot_token;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Warning: (Awful) C Code
|
* Warning: (Awful) C Code
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace HTTP {
|
namespace HTTP {
|
||||||
size_t write_callback(void *contents, size_t size, size_t nmemb, void *read_buffer) {
|
size_t write_callback(void *contents, size_t size, size_t nmemb, void *read_buffer) {
|
||||||
static_cast<std::string *>(read_buffer)->append(static_cast<char *>(contents), size * nmemb);
|
static_cast<std::string *>(read_buffer)->append(static_cast<char *>(contents), size * nmemb);
|
||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string post_request(std::string url, std::string content_type, std::string data, long *response_code) {
|
std::string post_request(std::string url, std::string content_type, std::string data, long *response_code, std::string token, std::string ca_location) {
|
||||||
CURL *curl;
|
CURL *curl;
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
std::string read_buffer;
|
std::string read_buffer;
|
||||||
@ -25,11 +23,11 @@ namespace HTTP {
|
|||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
|
|
||||||
// Now with real HTTPS!
|
// Now with real HTTPS!
|
||||||
curl_easy_setopt(curl, CURLOPT_CAINFO, "bot/http/DiscordCA.crt");
|
curl_easy_setopt(curl, CURLOPT_CAINFO, ca_location.c_str());
|
||||||
|
|
||||||
std::string header_arr[3];
|
std::string header_arr[3];
|
||||||
header_arr[0] = "Content-Type: " + content_type;
|
header_arr[0] = "Content-Type: " + content_type;
|
||||||
header_arr[1] = "Authorization: Bot " + bot_token;
|
header_arr[1] = "Authorization: Bot " + token;
|
||||||
header_arr[2] = "User-Agent: DiscordBot(http://github.com/jackb-p/triviadiscord, 1.0)";
|
header_arr[2] = "User-Agent: DiscordBot(http://github.com/jackb-p/triviadiscord, 1.0)";
|
||||||
|
|
||||||
for (std::string h : header_arr) {
|
for (std::string h : header_arr) {
|
||||||
@ -59,7 +57,7 @@ namespace HTTP {
|
|||||||
return read_buffer;
|
return read_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_request(std::string url, long *response_code) {
|
std::string get_request(std::string url, long *response_code, std::string token, std::string ca_location) {
|
||||||
CURL *curl;
|
CURL *curl;
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
std::string read_buffer;
|
std::string read_buffer;
|
||||||
@ -70,10 +68,10 @@ namespace HTTP {
|
|||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
|
|
||||||
// Now with real HTTPS!
|
// Now with real HTTPS!
|
||||||
curl_easy_setopt(curl, CURLOPT_CAINFO, "bot/http/DiscordCA.crt");
|
curl_easy_setopt(curl, CURLOPT_CAINFO, ca_location.c_str());
|
||||||
|
|
||||||
std::string header_arr[2];
|
std::string header_arr[2];
|
||||||
header_arr[0] = "Authorization: Bot " + bot_token;
|
header_arr[0] = "Authorization: Bot " + token;
|
||||||
header_arr[1] = "User-Agent: DiscordBot (http://github.com/jackb-p/triviadiscord, 1.0)";
|
header_arr[1] = "User-Agent: DiscordBot (http://github.com/jackb-p/triviadiscord, 1.0)";
|
||||||
|
|
||||||
for (std::string h : header_arr) {
|
for (std::string h : header_arr) {
|
15
TriviaBot/bot/http/HTTP.hpp
Normal file
15
TriviaBot/bot/http/HTTP.hpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef BOT_HTTP_HTTP
|
||||||
|
#define BOT_HTTP_HTTP
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
class BotConfig;
|
||||||
|
|
||||||
|
namespace HTTP {
|
||||||
|
std::string post_request(std::string url, std::string content_type, std::string data, long *response_code, std::string token, std::string ca_location);
|
||||||
|
std::string get_request(std::string url, long *response_code, std::string token, std::string ca_location);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,13 +0,0 @@
|
|||||||
#ifndef BOT_HTTP_HTTPHELPER
|
|
||||||
#define BOT_HTTP_HTTPHELPER
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include <curl/curl.h>
|
|
||||||
|
|
||||||
namespace HTTP {
|
|
||||||
std::string post_request(std::string url, std::string content_type, std::string data, long *response_code);
|
|
||||||
std::string get_request(std::string url, long *response_code);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
@ -6,9 +6,12 @@
|
|||||||
#include "V8Instance.hpp"
|
#include "V8Instance.hpp"
|
||||||
#include "../DiscordAPI.hpp"
|
#include "../DiscordAPI.hpp"
|
||||||
#include "../Logger.hpp"
|
#include "../Logger.hpp"
|
||||||
|
#include "../BotConfig.hpp"
|
||||||
|
|
||||||
V8Instance::V8Instance(std::string guild_id, std::map<std::string, DiscordObjects::Guild> *guilds, std::map<std::string, DiscordObjects::Channel> *channels,
|
using namespace v8;
|
||||||
std::map<std::string, DiscordObjects::User> *users, std::map<std::string, DiscordObjects::Role> *roles) {
|
|
||||||
|
V8Instance::V8Instance(BotConfig &c, std::string guild_id, std::map<std::string, DiscordObjects::Guild> *guilds, std::map<std::string, DiscordObjects::Channel> *channels,
|
||||||
|
std::map<std::string, DiscordObjects::User> *users, std::map<std::string, DiscordObjects::Role> *roles) : config(c) {
|
||||||
|
|
||||||
rng = std::mt19937(std::random_device()());
|
rng = std::mt19937(std::random_device()());
|
||||||
this->guild_id = guild_id;
|
this->guild_id = guild_id;
|
||||||
@ -25,6 +28,7 @@ void V8Instance::create() {
|
|||||||
create_params.array_buffer_allocator = ArrayBuffer::Allocator::NewDefaultAllocator();
|
create_params.array_buffer_allocator = ArrayBuffer::Allocator::NewDefaultAllocator();
|
||||||
|
|
||||||
isolate = Isolate::New(create_params);
|
isolate = Isolate::New(create_params);
|
||||||
|
isolate->Enter();
|
||||||
Logger::write("[v8] Created isolate", Logger::LogLevel::Debug);
|
Logger::write("[v8] Created isolate", Logger::LogLevel::Debug);
|
||||||
|
|
||||||
Isolate::Scope isolate_scope(isolate);
|
Isolate::Scope isolate_scope(isolate);
|
||||||
@ -696,7 +700,7 @@ void V8Instance::exec_js(std::string js, DiscordObjects::Channel *channel, Disco
|
|||||||
|
|
||||||
std::string err_msg = *error;
|
std::string err_msg = *error;
|
||||||
Logger::write("[v8] Compilation error: " + err_msg, Logger::LogLevel::Debug);
|
Logger::write("[v8] Compilation error: " + err_msg, Logger::LogLevel::Debug);
|
||||||
DiscordAPI::send_message(channel->id, ":warning: **Compilation error:** `" + err_msg + "`");
|
DiscordAPI::send_message(channel->id, ":warning: **Compilation error:** `" + err_msg + "`", config.token, config.cert_location);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -708,7 +712,7 @@ void V8Instance::exec_js(std::string js, DiscordObjects::Channel *channel, Disco
|
|||||||
|
|
||||||
std::string err_msg = *error;
|
std::string err_msg = *error;
|
||||||
Logger::write("[v8] Runtime error: " + err_msg, Logger::LogLevel::Debug);
|
Logger::write("[v8] Runtime error: " + err_msg, Logger::LogLevel::Debug);
|
||||||
DiscordAPI::send_message(channel->id, ":warning: **Runtime error:** `" + err_msg + "`");
|
DiscordAPI::send_message(channel->id, ":warning: **Runtime error:** `" + err_msg + "`", config.token, config.cert_location);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto end = std::chrono::steady_clock::now();
|
auto end = std::chrono::steady_clock::now();
|
||||||
@ -719,7 +723,7 @@ void V8Instance::exec_js(std::string js, DiscordObjects::Channel *channel, Disco
|
|||||||
current_channel = nullptr;
|
current_channel = nullptr;
|
||||||
|
|
||||||
if (print_text != "") {
|
if (print_text != "") {
|
||||||
DiscordAPI::send_message(channel->id, print_text);
|
DiscordAPI::send_message(channel->id, print_text, config.token, config.cert_location);
|
||||||
print_text = "";
|
print_text = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,68 +14,68 @@
|
|||||||
#include "../data_structures/GuildMember.hpp"
|
#include "../data_structures/GuildMember.hpp"
|
||||||
#include "../data_structures/User.hpp"
|
#include "../data_structures/User.hpp"
|
||||||
|
|
||||||
class APIHelper;
|
class BotConfig;
|
||||||
|
|
||||||
using namespace v8;
|
|
||||||
|
|
||||||
class V8Instance {
|
class V8Instance {
|
||||||
public:
|
public:
|
||||||
V8Instance(std::string guild_id, std::map<std::string, DiscordObjects::Guild> *guilds,
|
V8Instance(BotConfig &c, std::string guild_id, std::map<std::string, DiscordObjects::Guild> *guilds,
|
||||||
std::map<std::string, DiscordObjects::Channel> *channels, std::map<std::string, DiscordObjects::User> *users, std::map<std::string, DiscordObjects::Role> *roles);
|
std::map<std::string, DiscordObjects::Channel> *channels, std::map<std::string, DiscordObjects::User> *users, std::map<std::string, DiscordObjects::Role> *roles);
|
||||||
void exec_js(std::string js, DiscordObjects::Channel *channel, DiscordObjects::GuildMember *sender, std::string args = "");
|
void exec_js(std::string js, DiscordObjects::Channel *channel, DiscordObjects::GuildMember *sender, std::string args = "");
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void create();
|
BotConfig &config;
|
||||||
Local<Context> create_context();
|
|
||||||
|
|
||||||
void initialise(Local<Context> context);
|
void create();
|
||||||
|
v8::Local<v8::Context> create_context();
|
||||||
|
|
||||||
|
void initialise(v8::Local<v8::Context> context);
|
||||||
|
|
||||||
/* server */
|
/* server */
|
||||||
Global<ObjectTemplate> server_template;
|
v8::Global<v8::ObjectTemplate> server_template;
|
||||||
Local<ObjectTemplate> make_server_template();
|
v8::Local<v8::ObjectTemplate> make_server_template();
|
||||||
Local<Object> wrap_server(DiscordObjects::Guild *guild);
|
v8::Local<v8::Object> wrap_server(DiscordObjects::Guild *guild);
|
||||||
static void js_get_server(Local<Name> property, const PropertyCallbackInfo<Value> &info);
|
static void js_get_server(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value> &info);
|
||||||
|
|
||||||
|
|
||||||
/* user */
|
/* user */
|
||||||
Global<ObjectTemplate> user_template;
|
v8::Global<v8::ObjectTemplate> user_template;
|
||||||
Local<ObjectTemplate> make_user_template();
|
v8::Local<v8::ObjectTemplate> make_user_template();
|
||||||
Local<Object> wrap_user(DiscordObjects::GuildMember *member);
|
v8::Local<v8::Object> wrap_user(DiscordObjects::GuildMember *member);
|
||||||
static void js_get_user(Local<Name> property, const PropertyCallbackInfo<Value> &info);
|
static void js_get_user(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value> &info);
|
||||||
|
|
||||||
Global<ObjectTemplate> user_list_template;
|
v8::Global<v8::ObjectTemplate> user_list_template;
|
||||||
Local<ObjectTemplate> make_user_list_template();
|
v8::Local<v8::ObjectTemplate> make_user_list_template();
|
||||||
Local<Object> wrap_user_list(std::vector<DiscordObjects::GuildMember *> *user_list);
|
v8::Local<v8::Object> wrap_user_list(std::vector<DiscordObjects::GuildMember *> *user_list);
|
||||||
static void js_get_user_list(uint32_t index, const PropertyCallbackInfo<Value> &info);
|
static void js_get_user_list(uint32_t index, const v8::PropertyCallbackInfo<v8::Value> &info);
|
||||||
|
|
||||||
/* channel */
|
/* channel */
|
||||||
Global<ObjectTemplate> channel_template;
|
v8::Global<v8::ObjectTemplate> channel_template;
|
||||||
Local<ObjectTemplate> make_channel_template();
|
v8::Local<v8::ObjectTemplate> make_channel_template();
|
||||||
Local<Object> wrap_channel(DiscordObjects::Channel *channel);
|
v8::Local<v8::Object> wrap_channel(DiscordObjects::Channel *channel);
|
||||||
static void js_get_channel(Local<Name> property, const PropertyCallbackInfo<Value> &info);
|
static void js_get_channel(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value> &info);
|
||||||
|
|
||||||
Global<ObjectTemplate> channel_list_template;
|
v8::Global<v8::ObjectTemplate> channel_list_template;
|
||||||
Local<ObjectTemplate> make_channel_list_template();
|
v8::Local<v8::ObjectTemplate> make_channel_list_template();
|
||||||
Local<Object> wrap_channel_list(std::vector<DiscordObjects::Channel *> *channel_list);
|
v8::Local<v8::Object> wrap_channel_list(std::vector<DiscordObjects::Channel *> *channel_list);
|
||||||
static void js_get_channel_list(uint32_t index, const PropertyCallbackInfo<Value> &info);
|
static void js_get_channel_list(uint32_t index, const v8::PropertyCallbackInfo<v8::Value> &info);
|
||||||
|
|
||||||
/* role */
|
/* role */
|
||||||
Global<ObjectTemplate> role_template;
|
v8::Global<v8::ObjectTemplate> role_template;
|
||||||
Local<ObjectTemplate> make_role_template();
|
v8::Local<v8::ObjectTemplate> make_role_template();
|
||||||
Local<Object> wrap_role(DiscordObjects::Role *role);
|
v8::Local<v8::Object> wrap_role(DiscordObjects::Role *role);
|
||||||
static void js_get_role(Local<Name> property, const PropertyCallbackInfo<Value> &info);
|
static void js_get_role(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value> &info);
|
||||||
|
|
||||||
Global<ObjectTemplate> role_list_template;
|
v8::Global<v8::ObjectTemplate> role_list_template;
|
||||||
Local<ObjectTemplate> make_role_list_template();
|
v8::Local<v8::ObjectTemplate> make_role_list_template();
|
||||||
Local<Object> wrap_role_list(std::vector<DiscordObjects::Role *> *role_list);
|
v8::Local<v8::Object> wrap_role_list(std::vector<DiscordObjects::Role *> *role_list);
|
||||||
static void js_get_role_list(uint32_t index, const PropertyCallbackInfo<Value> &info);
|
static void js_get_role_list(uint32_t index, const v8::PropertyCallbackInfo<v8::Value> &info);
|
||||||
|
|
||||||
/* print function */
|
/* print function */
|
||||||
static void js_print(const FunctionCallbackInfo<Value> &args);
|
static void js_print(const v8::FunctionCallbackInfo<v8::Value> &args);
|
||||||
|
|
||||||
/* randomness functions */
|
/* randomness functions */
|
||||||
static void js_random(const FunctionCallbackInfo<Value> &args);
|
static void js_random(const v8::FunctionCallbackInfo<v8::Value> &args);
|
||||||
static void js_shuffle(const FunctionCallbackInfo<Value> &args);
|
static void js_shuffle(const v8::FunctionCallbackInfo<v8::Value> &args);
|
||||||
|
|
||||||
std::map<std::string, DiscordObjects::Guild> *guilds;
|
std::map<std::string, DiscordObjects::Guild> *guilds;
|
||||||
std::map<std::string, DiscordObjects::Channel> *channels;
|
std::map<std::string, DiscordObjects::Channel> *channels;
|
||||||
@ -83,9 +83,9 @@ private:
|
|||||||
std::map<std::string, DiscordObjects::Role> *roles;
|
std::map<std::string, DiscordObjects::Role> *roles;
|
||||||
|
|
||||||
std::string guild_id;
|
std::string guild_id;
|
||||||
Isolate *isolate;
|
v8::Isolate *isolate;
|
||||||
|
|
||||||
Global<Context> context_;
|
v8::Global<v8::Context> context_;
|
||||||
|
|
||||||
/* random generating variables */
|
/* random generating variables */
|
||||||
std::mt19937 rng;
|
std::mt19937 rng;
|
||||||
|
Loading…
Reference in New Issue
Block a user