From 8ebe6c2c4eb6faf6448a8d3bce58dbe2cc5515a5 Mon Sep 17 00:00:00 2001 From: jackb-p Date: Fri, 12 Aug 2016 15:42:03 +0100 Subject: [PATCH] Squash some bugs and fix some warnings. Update CMakeLists --- TriviaBot/CMakeLists.txt | 13 +++++- TriviaBot/bot/TriviaBot.cpp | 2 +- TriviaBot/bot/TriviaBot.cpp.save | 64 ++++++++++++++++++++++++++++++ TriviaBot/bot/js/CommandHelper.hpp | 3 +- TriviaBot/bot/js/V8Instance.cpp | 27 +++++-------- TriviaBot/bot/js/V8Instance.hpp | 4 +- 6 files changed, 90 insertions(+), 23 deletions(-) create mode 100644 TriviaBot/bot/TriviaBot.cpp.save diff --git a/TriviaBot/CMakeLists.txt b/TriviaBot/CMakeLists.txt index 001e4ac..2d7ba87 100644 --- a/TriviaBot/CMakeLists.txt +++ b/TriviaBot/CMakeLists.txt @@ -7,6 +7,8 @@ project(TriviaBot) file(GLOB_RECURSE sources bot/*.cpp bot/*.hpp ../lib/sqlite3/sqlite3.c) +link_directories(../lib/v8/lib) + ############################################################################### ## target definitions ######################################################### ############################################################################### @@ -29,8 +31,14 @@ target_link_libraries(TriviaBot PUBLIC ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${CURL_LIBRARIES} - pthread + v8 + v8_libplatform + v8_libbase + icui18n + icuuc + rt dl + pthread ) include_directories( @@ -39,7 +47,8 @@ include_directories( ${CURL_INCLUDE_DIR} ../lib/websocketpp ../lib/sqlite3 + ../lib/v8 ) # don't know if necessary, too scared to remove -add_definitions(-D_WEBSOCKETPP_CPP11_STL_) \ No newline at end of file +add_definitions(-D_WEBSOCKETPP_CPP11_STL_) diff --git a/TriviaBot/bot/TriviaBot.cpp b/TriviaBot/bot/TriviaBot.cpp index f2d970c..150738c 100644 --- a/TriviaBot/bot/TriviaBot.cpp +++ b/TriviaBot/bot/TriviaBot.cpp @@ -61,4 +61,4 @@ int main(int argc, char *argv[]) { std::getchar(); return 0; -} \ No newline at end of file +} diff --git a/TriviaBot/bot/TriviaBot.cpp.save b/TriviaBot/bot/TriviaBot.cpp.save new file mode 100644 index 0000000..5ebbefd --- /dev/null +++ b/TriviaBot/bot/TriviaBot.cpp.save @@ -0,0 +1,64 @@ +#include +#include +#include + +#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; +} diff --git a/TriviaBot/bot/js/CommandHelper.hpp b/TriviaBot/bot/js/CommandHelper.hpp index a34d6c8..e10aed8 100644 --- a/TriviaBot/bot/js/CommandHelper.hpp +++ b/TriviaBot/bot/js/CommandHelper.hpp @@ -1,6 +1,7 @@ #ifndef BOT_JS_COMMANDHELPER #define BOT_JS_COMMANDHELPER +#include #include namespace CommandHelper { @@ -16,4 +17,4 @@ namespace CommandHelper { bool command_in_db(std::string guild_id, std::string command_name); } -#endif \ No newline at end of file +#endif diff --git a/TriviaBot/bot/js/V8Instance.cpp b/TriviaBot/bot/js/V8Instance.cpp index d372c6b..0d52a38 100644 --- a/TriviaBot/bot/js/V8Instance.cpp +++ b/TriviaBot/bot/js/V8Instance.cpp @@ -48,12 +48,12 @@ void V8Instance::create() { void V8Instance::initialise(Local context) { HandleScope handle_scope(isolate); - Local opts_obj = wrap_server(&(*guilds)[guild_id]); - + Local server_obj = wrap_server(&(*guilds)[guild_id]); + context->Global()->Set( - context, - String::NewFromUtf8(isolate, "server", NewStringType::kNormal).ToLocalChecked(), - opts_obj + context, + String::NewFromUtf8(isolate, "server", NewStringType::kNormal).ToLocalChecked(), + server_obj ).FromJust(); Logger::write("[v8] Bound server template", Logger::LogLevel::Debug); @@ -64,7 +64,7 @@ v8::Local V8Instance::create_context() { Local self = External::New(isolate, (void *) this); global->Set( - String::NewFromUtf8(isolate, "print", NewStringType::kNormal).ToLocalChecked(), + String::NewFromUtf8(isolate, "print", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, V8Instance::js_print, self) ); global->Set( @@ -209,13 +209,6 @@ Local V8Instance::wrap_channel(DiscordObjects::Channel *channel) { } void V8Instance::js_get_channel(Local property, const PropertyCallbackInfo &info) { - void *self_v = info.Data().As()->Value(); - if (!self_v) { - Logger::write("[v8] [js_get_channel] Class pointer empty", Logger::LogLevel::Warning); - return; - } - V8Instance *self = static_cast(self_v); - void *channel_v = info.Holder()->GetInternalField(0).As()->Value(); if (!channel_v) { 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(), user_obj ); - Local channel_obj = wrap_user(sender); + Local channel_obj = wrap_channel(channel); context->Global()->Set( - String::NewFromUtf8(isolate, "user", NewStringType::kNormal).ToLocalChecked(), - user_obj + String::NewFromUtf8(isolate, "channel", NewStringType::kNormal).ToLocalChecked(), + channel_obj ); // 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); print_text = ""; } -} \ No newline at end of file +} diff --git a/TriviaBot/bot/js/V8Instance.hpp b/TriviaBot/bot/js/V8Instance.hpp index 0bc6ec2..c0966dc 100644 --- a/TriviaBot/bot/js/V8Instance.hpp +++ b/TriviaBot/bot/js/V8Instance.hpp @@ -75,7 +75,7 @@ private: /* print function */ static void js_print(const FunctionCallbackInfo &args); - + /* randomness functions */ static void js_random(const FunctionCallbackInfo &args); static void js_shuffle(const FunctionCallbackInfo &args); @@ -99,4 +99,4 @@ private: DiscordObjects::GuildMember *current_sender; }; -#endif \ No newline at end of file +#endif