Most of Boobot's JS object implementation
This commit is contained in:
@ -1,14 +1,20 @@
|
||||
#include <iostream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "V8Instance.hpp"
|
||||
#include "../APIHelper.hpp"
|
||||
#include "../Logger.hpp"
|
||||
|
||||
using namespace v8;
|
||||
V8Instance::V8Instance(std::string guild_id, std::shared_ptr<APIHelper> ah, 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) {
|
||||
|
||||
V8Instance::V8Instance(std::shared_ptr<APIHelper> ah) {
|
||||
this->guild_id = guild_id;
|
||||
this->ah = ah;
|
||||
this->guilds = guilds;
|
||||
this->channels = channels;
|
||||
this->users = users;
|
||||
this->roles = roles;
|
||||
|
||||
create();
|
||||
}
|
||||
|
||||
@ -38,17 +44,91 @@ v8::Local<v8::Context> V8Instance::create_context() {
|
||||
Local<ObjectTemplate> global = ObjectTemplate::New(isolate);
|
||||
// bind print() function
|
||||
Local<External> self = External::New(isolate, (void *) this);
|
||||
global->Set(String::NewFromUtf8(isolate, "print", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, V8Instance::js_print, self));
|
||||
Logger::write("[v8] Created global obj, linked print function", Logger::LogLevel::Debug);
|
||||
|
||||
global->Set(
|
||||
String::NewFromUtf8(isolate, "print", NewStringType::kNormal).ToLocalChecked(),
|
||||
FunctionTemplate::New(isolate, V8Instance::js_print, self)
|
||||
);
|
||||
global->SetAccessor(
|
||||
String::NewFromUtf8(isolate, "server", NewStringType::kNormal).ToLocalChecked(),
|
||||
V8Instance::js_get_server,
|
||||
(AccessorSetterCallback) 0,
|
||||
self
|
||||
);
|
||||
global->SetAccessor(
|
||||
String::NewFromUtf8(isolate, "channel", NewStringType::kNormal).ToLocalChecked(),
|
||||
V8Instance::js_get_channel,
|
||||
(AccessorSetterCallback) 0,
|
||||
self
|
||||
);
|
||||
global->SetAccessor(
|
||||
String::NewFromUtf8(isolate, "user", NewStringType::kNormal).ToLocalChecked(),
|
||||
V8Instance::js_get_user,
|
||||
(AccessorSetterCallback) 0,
|
||||
self
|
||||
);
|
||||
global->SetAccessor(
|
||||
String::NewFromUtf8(isolate, "input", NewStringType::kNormal).ToLocalChecked(),
|
||||
V8Instance::js_get_input,
|
||||
(AccessorSetterCallback) 0,
|
||||
self
|
||||
);
|
||||
|
||||
Logger::write("[v8] Created global obj, linked data and functions", Logger::LogLevel::Debug);
|
||||
|
||||
return Context::New(isolate, NULL, global);
|
||||
}
|
||||
|
||||
void V8Instance::js_get_server(Local<String> property, const PropertyCallbackInfo<Value> &info) {
|
||||
auto data = info.Data().As<External>();
|
||||
V8Instance *self = static_cast<V8Instance *>(data->Value());
|
||||
|
||||
Local<Object> obj = Object::New(info.GetIsolate());
|
||||
self->add_to_obj(obj, (*self->guilds)[self->guild_id]);
|
||||
info.GetReturnValue().Set(obj);
|
||||
}
|
||||
|
||||
void V8Instance::js_get_channel(Local<String> property, const PropertyCallbackInfo<Value> &info) {
|
||||
auto data = info.Data().As<External>();
|
||||
V8Instance *self = static_cast<V8Instance *>(data->Value());
|
||||
|
||||
if (!self->current_channel) {
|
||||
Logger::write("[v8] current_channel is null pointer", Logger::LogLevel::Severe);
|
||||
info.GetReturnValue().SetNull();
|
||||
return;
|
||||
}
|
||||
|
||||
Local<Object> obj = Object::New(info.GetIsolate());
|
||||
self->add_to_obj(obj, (*self->current_channel));
|
||||
info.GetReturnValue().Set(obj);
|
||||
}
|
||||
|
||||
void V8Instance::js_get_user(Local<String> property, const PropertyCallbackInfo<Value> &info) {
|
||||
auto data = info.Data().As<External>();
|
||||
V8Instance *self = static_cast<V8Instance *>(data->Value());
|
||||
|
||||
if (!self->current_sender) {
|
||||
Logger::write("[v8] current_sender is null pointer", Logger::LogLevel::Severe);
|
||||
info.GetReturnValue().SetNull();
|
||||
return;
|
||||
}
|
||||
|
||||
Local<Object> obj = Object::New(info.GetIsolate());
|
||||
self->add_to_obj(obj, (*self->current_sender));
|
||||
info.GetReturnValue().Set(obj);
|
||||
}
|
||||
|
||||
void V8Instance::js_get_input(Local<String> property, const PropertyCallbackInfo<Value> &info) {
|
||||
auto data = info.Data().As<External>();
|
||||
V8Instance *self = static_cast<V8Instance *>(data->Value());
|
||||
|
||||
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), self->current_input.c_str(), NewStringType::kNormal).ToLocalChecked());
|
||||
}
|
||||
|
||||
void V8Instance::clean_up() {
|
||||
Logger::write("[v8] Cleaning up", Logger::LogLevel::Debug);
|
||||
isolate->Exit();
|
||||
isolate->Dispose();
|
||||
delete array_buffer_allocator;
|
||||
}
|
||||
|
||||
void V8Instance::reload() {
|
||||
@ -56,11 +136,15 @@ void V8Instance::reload() {
|
||||
create();
|
||||
}
|
||||
|
||||
void V8Instance::exec_js(std::string js, std::string channel_id) {
|
||||
void V8Instance::exec_js(std::string js, DiscordObjects::Channel *channel, DiscordObjects::GuildMember *sender, std::string args) {
|
||||
HandleScope handle_scope(isolate);
|
||||
Local<Context> context(isolate->GetCurrentContext());
|
||||
|
||||
Logger::write("[v8] Executing JS: " + js, Logger::LogLevel::Debug);
|
||||
current_sender = sender;
|
||||
current_channel = channel;
|
||||
current_input = args;
|
||||
|
||||
Logger::write("[v8] Preparing JS: " + js, Logger::LogLevel::Debug);
|
||||
|
||||
Local<String> source = String::NewFromUtf8(isolate, js.c_str(), NewStringType::kNormal).ToLocalChecked();
|
||||
|
||||
@ -75,7 +159,7 @@ void V8Instance::exec_js(std::string js, std::string channel_id) {
|
||||
|
||||
std::string err_msg = *error;
|
||||
Logger::write("[v8] Compilation error: " + err_msg, Logger::LogLevel::Debug);
|
||||
ah->send_message(channel_id, ":warning: **Compilation error:** `" + err_msg + "`");
|
||||
ah->send_message(channel->id, ":warning: **Compilation error:** `" + err_msg + "`");
|
||||
|
||||
return;
|
||||
}
|
||||
@ -87,21 +171,23 @@ void V8Instance::exec_js(std::string js, std::string channel_id) {
|
||||
|
||||
std::string err_msg = *error;
|
||||
Logger::write("[v8] Runtime error: " + err_msg, Logger::LogLevel::Debug);
|
||||
ah->send_message(channel_id, ":warning: **Runtime error:** `" + err_msg + "`");
|
||||
|
||||
return;
|
||||
ah->send_message(channel->id, ":warning: **Runtime error:** `" + err_msg + "`");
|
||||
}
|
||||
|
||||
Logger::write("[v8] Script compiled and run", Logger::LogLevel::Debug);
|
||||
|
||||
current_sender = nullptr;
|
||||
current_channel = nullptr;
|
||||
current_input = "";
|
||||
|
||||
if (print_text != "") {
|
||||
ah->send_message(channel_id, print_text);
|
||||
ah->send_message(channel->id, print_text);
|
||||
print_text = "";
|
||||
}
|
||||
}
|
||||
|
||||
void V8Instance::js_print(const v8::FunctionCallbackInfo<v8::Value> &args) {
|
||||
auto data = args.Data().As<v8::External>();
|
||||
auto data = args.Data().As<External>();
|
||||
V8Instance *self = static_cast<V8Instance *>(data->Value());
|
||||
|
||||
std::string output = "";
|
||||
@ -110,4 +196,130 @@ void V8Instance::js_print(const v8::FunctionCallbackInfo<v8::Value> &args) {
|
||||
v8::String::Utf8Value str(args[i]);
|
||||
self->print_text += *str;
|
||||
}
|
||||
}
|
||||
|
||||
void V8Instance::add_to_obj(Local<Object> &object, std::string field_name, std::string value) {
|
||||
add_to_obj(object, field_name, value.c_str());
|
||||
}
|
||||
|
||||
void V8Instance::add_to_obj(Local<Object> &object, std::string field_name, const char value[]) {
|
||||
if (value == "null") {
|
||||
object->Set(String::NewFromUtf8(isolate, field_name.c_str(), NewStringType::kNormal).ToLocalChecked(), Null(isolate));
|
||||
return;
|
||||
}
|
||||
|
||||
object->Set(String::NewFromUtf8(isolate, field_name.c_str(), NewStringType::kNormal).ToLocalChecked(),
|
||||
String::NewFromUtf8(isolate, value, NewStringType::kNormal).ToLocalChecked());
|
||||
}
|
||||
|
||||
void V8Instance::add_to_obj(Local<Object> &object, std::string field_name, int32_t value) {
|
||||
object->Set(String::NewFromUtf8(isolate, field_name.c_str(), NewStringType::kNormal).ToLocalChecked(),
|
||||
Integer::New(isolate, value));
|
||||
}
|
||||
|
||||
void V8Instance::add_to_obj(Local<Object> &object, std::string field_name, bool value) {
|
||||
object->Set(String::NewFromUtf8(isolate, field_name.c_str(), NewStringType::kNormal).ToLocalChecked(),
|
||||
Boolean::New(isolate, value));
|
||||
}
|
||||
|
||||
void V8Instance::add_to_obj(Local<Object> &object, std::string field_name, Local<Object> value) {
|
||||
object->Set(String::NewFromUtf8(isolate, field_name.c_str(), NewStringType::kNormal).ToLocalChecked(), value);
|
||||
}
|
||||
|
||||
void V8Instance::add_to_obj(Local<Object> &object, std::string field_name, Local<Array> value) {
|
||||
object->Set(String::NewFromUtf8(isolate, field_name.c_str(), NewStringType::kNormal).ToLocalChecked(), value);
|
||||
}
|
||||
|
||||
void V8Instance::add_to_obj(Local<Object> &object, DiscordObjects::Guild guild) {
|
||||
/* Boobot fields */
|
||||
add_to_obj(object, "Id", guild.id);
|
||||
add_to_obj(object, "Name", guild.name);
|
||||
add_to_obj(object, "IconUrl", "https://discordapp.com/api/guilds/" + guild.id + "/icons/" + guild.icon + ".jpg");
|
||||
|
||||
Local<Object> owner_obj = Object::New(isolate);
|
||||
DiscordObjects::GuildMember &owner = guild.members[guild.owner_id];
|
||||
add_to_obj(owner_obj, owner);
|
||||
add_to_obj(object, "Owner", owner_obj);
|
||||
|
||||
Local<Array> roles_arr = Array::New(isolate, guild.roles.size());
|
||||
for (uint32_t i = 0; i < guild.roles.size(); i++) {
|
||||
Local<Object> obj = Object::New(isolate);
|
||||
DiscordObjects::Role &role = *guild.roles[i];
|
||||
add_to_obj(obj, role);
|
||||
|
||||
roles_arr->Set(i, obj);
|
||||
}
|
||||
add_to_obj(object, "Roles", roles_arr);
|
||||
|
||||
Local<Array> members_arr = Array::New(isolate, guild.members.size());
|
||||
int i = 0;
|
||||
for (auto it : guild.members) {
|
||||
Local<Object> obj = Object::New(isolate);
|
||||
DiscordObjects::GuildMember &member = it.second;
|
||||
add_to_obj(obj, member);
|
||||
|
||||
members_arr->Set(i, obj);
|
||||
i++;
|
||||
}
|
||||
add_to_obj(object, "Users", members_arr);
|
||||
}
|
||||
|
||||
void V8Instance::add_to_obj(Local<Object> &object, DiscordObjects::Channel channel) {
|
||||
/* Boobot fields */
|
||||
add_to_obj(object, "Id", channel.id);
|
||||
add_to_obj(object, "Name", channel.name);
|
||||
add_to_obj(object, "Topic", channel.topic);
|
||||
add_to_obj(object, "IsVoice", channel.type == "voice");
|
||||
|
||||
Local<Array> users = Array::New(isolate, 1);
|
||||
users->Set(0, String::NewFromUtf8(isolate, "NOT IMPLEMENTED", NewStringType::kNormal).ToLocalChecked());
|
||||
add_to_obj(object, "Users", users);
|
||||
|
||||
/* Additional fields */
|
||||
add_to_obj(object, "LastMessageId", channel.last_message_id);
|
||||
add_to_obj(object, "Bitrate", channel.bitrate);
|
||||
add_to_obj(object, "UserLimit", channel.user_limit);
|
||||
}
|
||||
|
||||
void V8Instance::add_to_obj(Local<Object> &object, DiscordObjects::Role role) {
|
||||
/* Boobot fields */
|
||||
add_to_obj(object, "Id", role.id);
|
||||
add_to_obj(object, "Name", role.name);
|
||||
add_to_obj(object, "Position", role.position);
|
||||
add_to_obj(object, "Red", "NOT IMPLEMENTED");
|
||||
add_to_obj(object, "Blue", "NOT IMPLEMENTED");
|
||||
add_to_obj(object, "Green", "NOT IMPLEMENTED");
|
||||
|
||||
/* Additional fields */
|
||||
add_to_obj(object, "Mentionable", role.mentionable);
|
||||
add_to_obj(object, "Mention", "<@&" + role.id + ">");
|
||||
add_to_obj(object, "Hoist", role.hoist);
|
||||
}
|
||||
|
||||
void V8Instance::add_to_obj(Local<Object> &object, DiscordObjects::GuildMember member) {
|
||||
/* Boobot fields */
|
||||
add_to_obj(object, "Id", member.user->id);
|
||||
add_to_obj(object, "Name", member.user->username);
|
||||
add_to_obj(object, "Mention", "<@!" + member.user->id + ">");
|
||||
add_to_obj(object, "AvatarUrl", "https://discordapp.com/api/users/" + member.user->id + "/avatars/" + member.user->avatar + ".jpg");
|
||||
|
||||
Local<Array> roles = Array::New(isolate, member.roles.size());
|
||||
int i = 0;
|
||||
for (DiscordObjects::Role *role : member.roles) {
|
||||
Local<Object> role_obj = Object::New(isolate);
|
||||
add_to_obj(role_obj, *role);
|
||||
|
||||
roles->Set(i, role_obj);
|
||||
i++;
|
||||
}
|
||||
add_to_obj(object, "Roles", roles);
|
||||
|
||||
add_to_obj(object, "State", "NOT IMPLEMENTED");
|
||||
add_to_obj(object, "CurrentGame", "NOT IMPLEMENTED");
|
||||
|
||||
/* Additional fields */
|
||||
add_to_obj(object, "Nick", member.nick);
|
||||
add_to_obj(object, "Deaf", member.deaf);
|
||||
add_to_obj(object, "Mute", member.mute);
|
||||
add_to_obj(object, "JoinedAt", member.joined_at);
|
||||
}
|
@ -2,30 +2,66 @@
|
||||
#define BOT_JS_V8INSTANCE
|
||||
|
||||
#include <memory>
|
||||
#include <map>
|
||||
|
||||
#include <include/v8.h>
|
||||
#include <include/libplatform/libplatform.h>
|
||||
|
||||
#include "../data_structures/Guild.hpp"
|
||||
#include "../data_structures/Channel.hpp"
|
||||
#include "../data_structures/Role.hpp"
|
||||
#include "../data_structures/GuildMember.hpp"
|
||||
#include "../data_structures/User.hpp"
|
||||
|
||||
class APIHelper;
|
||||
|
||||
using namespace v8;
|
||||
|
||||
class V8Instance {
|
||||
public:
|
||||
V8Instance(std::shared_ptr<APIHelper> ah);
|
||||
V8Instance(std::string guild_id, std::shared_ptr<APIHelper> ah, 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);
|
||||
~V8Instance();
|
||||
void reload();
|
||||
void exec_js(std::string js, std::string channel_id);
|
||||
void exec_js(std::string js, DiscordObjects::Channel *channel, DiscordObjects::GuildMember *sender, std::string args = "");
|
||||
|
||||
private:
|
||||
void clean_up();
|
||||
void create();
|
||||
v8::Local<v8::Context> create_context();
|
||||
static void js_print(const v8::FunctionCallbackInfo<v8::Value> &args);
|
||||
Local<Context> create_context();
|
||||
|
||||
v8::ArrayBuffer::Allocator *array_buffer_allocator;
|
||||
v8::Isolate *isolate;
|
||||
void add_to_obj(Local<Object> &object, std::string field_name, std::string value);
|
||||
void add_to_obj(Local<Object> &object, std::string field_name, const char value[]);
|
||||
void add_to_obj(Local<Object> &object, std::string field_name, int32_t value);
|
||||
void add_to_obj(Local<Object> &object, std::string field_name, bool value);
|
||||
void add_to_obj(Local<Object> &object, std::string field_name, Local<Object> value);
|
||||
void add_to_obj(Local<Object> &object, std::string field_name, Local<Array> value);
|
||||
|
||||
void add_to_obj(Local<Object> &object, DiscordObjects::Guild guild);
|
||||
void add_to_obj(Local<Object> &object, DiscordObjects::Channel channel);
|
||||
void add_to_obj(Local<Object> &object, DiscordObjects::Role role);
|
||||
void add_to_obj(Local<Object> &object, DiscordObjects::GuildMember member);
|
||||
|
||||
static void js_print(const FunctionCallbackInfo<Value> &args);
|
||||
static void js_get_server(Local<String> property, const PropertyCallbackInfo<Value> &info);
|
||||
static void js_get_channel(Local<String> property, const PropertyCallbackInfo<Value> &info);
|
||||
static void js_get_user(Local<String> property, const PropertyCallbackInfo<Value> &info);
|
||||
static void js_get_input(Local<String> property, const PropertyCallbackInfo<Value> &info);
|
||||
|
||||
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::string guild_id;
|
||||
Isolate *isolate;
|
||||
std::shared_ptr<APIHelper> ah;
|
||||
|
||||
/* variables which change when a new command is executed */
|
||||
std::string print_text;
|
||||
std::string current_input;
|
||||
DiscordObjects::Channel *current_channel;
|
||||
DiscordObjects::GuildMember *current_sender;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user