Squash some bugs and fix some warnings. Update CMakeLists

This commit is contained in:
Jack Bond-Preston 2016-08-12 15:42:03 +01:00
parent 7739404646
commit 8ebe6c2c4e
6 changed files with 90 additions and 23 deletions

View File

@ -7,6 +7,8 @@ project(TriviaBot)
file(GLOB_RECURSE sources bot/*.cpp bot/*.hpp ../lib/sqlite3/sqlite3.c) file(GLOB_RECURSE sources bot/*.cpp bot/*.hpp ../lib/sqlite3/sqlite3.c)
link_directories(../lib/v8/lib)
############################################################################### ###############################################################################
## target definitions ######################################################### ## target definitions #########################################################
############################################################################### ###############################################################################
@ -29,8 +31,14 @@ target_link_libraries(TriviaBot PUBLIC
${Boost_LIBRARIES} ${Boost_LIBRARIES}
${OPENSSL_LIBRARIES} ${OPENSSL_LIBRARIES}
${CURL_LIBRARIES} ${CURL_LIBRARIES}
pthread v8
v8_libplatform
v8_libbase
icui18n
icuuc
rt
dl dl
pthread
) )
include_directories( include_directories(
@ -39,7 +47,8 @@ include_directories(
${CURL_INCLUDE_DIR} ${CURL_INCLUDE_DIR}
../lib/websocketpp ../lib/websocketpp
../lib/sqlite3 ../lib/sqlite3
../lib/v8
) )
# don't know if necessary, too scared to remove # don't know if necessary, too scared to remove
add_definitions(-D_WEBSOCKETPP_CPP11_STL_) add_definitions(-D_WEBSOCKETPP_CPP11_STL_)

View File

@ -61,4 +61,4 @@ int main(int argc, char *argv[]) {
std::getchar(); std::getchar();
return 0; return 0;
} }

View File

@ -0,0 +1,64 @@
#include <curl/curl.h>
#include <include/libplatform/libplatform.>
#include <include/v8.h>
#include "ClientConnection.hpp"
#include "Logger.hpp"
#include "DiscordAPI.hpp"
std::string bot_token;
int main(int argc, char *argv[]) {
curl_global_init(CURL_GLOBAL_DEFAULT);
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
v8::V8::Initialize();
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 url = DiscordAPI::get_gateway().value("url", "wss://gateway.discord.gg");
bool retry = true;
while (retry) {
try {
ClientConnection conn;
conn.start(url + args);
}
catch (const std::exception &e) {
Logger::write("std exception: " + std::string(e.what()), Logger::LogLevel::Severe);
retry = false;
}
catch (websocketpp::lib::error_code e) {
Logger::write("websocketpp exception: " + e.message(), Logger::LogLevel::Severe);
}
catch (...) {
Logger::write("other exception.", Logger::LogLevel::Severe);
retry = false;
}
}
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete platform;
curl_global_cleanup();
Logger::write("Cleaned up", Logger::LogLevel::Info);
std::cout << "Press enter to exit" << std::endl;
std::getchar();
return 0;
}

View File

@ -1,6 +1,7 @@
#ifndef BOT_JS_COMMANDHELPER #ifndef BOT_JS_COMMANDHELPER
#define BOT_JS_COMMANDHELPER #define BOT_JS_COMMANDHELPER
#include <string>
#include <vector> #include <vector>
namespace CommandHelper { namespace CommandHelper {
@ -16,4 +17,4 @@ namespace CommandHelper {
bool command_in_db(std::string guild_id, std::string command_name); bool command_in_db(std::string guild_id, std::string command_name);
} }
#endif #endif

View File

@ -48,12 +48,12 @@ void V8Instance::create() {
void V8Instance::initialise(Local<Context> context) { void V8Instance::initialise(Local<Context> context) {
HandleScope handle_scope(isolate); HandleScope handle_scope(isolate);
Local<Object> opts_obj = wrap_server(&(*guilds)[guild_id]); Local<Object> server_obj = wrap_server(&(*guilds)[guild_id]);
context->Global()->Set( context->Global()->Set(
context, context,
String::NewFromUtf8(isolate, "server", NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "server", NewStringType::kNormal).ToLocalChecked(),
opts_obj server_obj
).FromJust(); ).FromJust();
Logger::write("[v8] Bound server template", Logger::LogLevel::Debug); Logger::write("[v8] Bound server template", Logger::LogLevel::Debug);
@ -64,7 +64,7 @@ v8::Local<v8::Context> V8Instance::create_context() {
Local<External> self = External::New(isolate, (void *) this); Local<External> self = External::New(isolate, (void *) this);
global->Set( global->Set(
String::NewFromUtf8(isolate, "print", NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "print", NewStringType::kNormal).ToLocalChecked(),
FunctionTemplate::New(isolate, V8Instance::js_print, self) FunctionTemplate::New(isolate, V8Instance::js_print, self)
); );
global->Set( global->Set(
@ -209,13 +209,6 @@ Local<Object> V8Instance::wrap_channel(DiscordObjects::Channel *channel) {
} }
void V8Instance::js_get_channel(Local<Name> property, const PropertyCallbackInfo<Value> &info) { void V8Instance::js_get_channel(Local<Name> property, const PropertyCallbackInfo<Value> &info) {
void *self_v = info.Data().As<External>()->Value();
if (!self_v) {
Logger::write("[v8] [js_get_channel] Class pointer empty", Logger::LogLevel::Warning);
return;
}
V8Instance *self = static_cast<V8Instance *>(self_v);
void *channel_v = info.Holder()->GetInternalField(0).As<External>()->Value(); void *channel_v = info.Holder()->GetInternalField(0).As<External>()->Value();
if (!channel_v) { if (!channel_v) {
Logger::write("[v8] [js_get_channel] Channel pointer empty", Logger::LogLevel::Warning); Logger::write("[v8] [js_get_channel] Channel pointer empty", Logger::LogLevel::Warning);
@ -694,10 +687,10 @@ void V8Instance::exec_js(std::string js, DiscordObjects::Channel *channel, Disco
String::NewFromUtf8(isolate, "user", NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "user", NewStringType::kNormal).ToLocalChecked(),
user_obj user_obj
); );
Local<Object> channel_obj = wrap_user(sender); Local<Object> channel_obj = wrap_channel(channel);
context->Global()->Set( context->Global()->Set(
String::NewFromUtf8(isolate, "user", NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, "channel", NewStringType::kNormal).ToLocalChecked(),
user_obj channel_obj
); );
// TODO: 'message' object here too, although it's fairly pointless // TODO: 'message' object here too, although it's fairly pointless
@ -746,4 +739,4 @@ void V8Instance::exec_js(std::string js, DiscordObjects::Channel *channel, Disco
DiscordAPI::send_message(channel->id, print_text); DiscordAPI::send_message(channel->id, print_text);
print_text = ""; print_text = "";
} }
} }

View File

@ -75,7 +75,7 @@ private:
/* print function */ /* print function */
static void js_print(const FunctionCallbackInfo<Value> &args); static void js_print(const FunctionCallbackInfo<Value> &args);
/* randomness functions */ /* randomness functions */
static void js_random(const FunctionCallbackInfo<Value> &args); static void js_random(const FunctionCallbackInfo<Value> &args);
static void js_shuffle(const FunctionCallbackInfo<Value> &args); static void js_shuffle(const FunctionCallbackInfo<Value> &args);
@ -99,4 +99,4 @@ private:
DiscordObjects::GuildMember *current_sender; DiscordObjects::GuildMember *current_sender;
}; };
#endif #endif