Compare commits

...

10 Commits
3 ... release

Author SHA1 Message Date
e5a9b6557d Add TrueName attribute to users, ignores nickname 2016-11-22 23:27:37 +00:00
09830a6736 Remove unused travis.yml 2016-11-14 00:45:16 +00:00
bc63dabb81 Missed one rename 2016-11-14 00:44:57 +00:00
a802e6ae38 Rename (trivia-bot -> Toast) 2016-11-14 00:40:02 +00:00
301d99ca2f Fix bad role name checking code 2016-08-19 17:47:07 +01:00
b68ac1d3b2 Merge pull request #8 from jackb-p/develop
Merge Develop branch
2016-08-19 17:41:54 +01:00
a365456dd8 Update README.md 2016-08-19 17:41:33 +01:00
0498b22c8c Apply same permissions from ~createjs to ~js 2016-08-19 17:30:00 +01:00
c566e7f04f Update build status image 2016-08-16 01:58:24 +01:00
04565187a2 Update README.md 2016-08-16 01:34:17 +01:00
34 changed files with 60 additions and 119 deletions

12
.gitignore vendored
View File

@ -1,14 +1,14 @@
# VS files
/.vs/
/TriviaBot.sln
/TriviaBot.VC*
/TriviaBot/TriviaBot.vcxproj*
/TriviaBot/x64/
/Toast.sln
/Toast.VC*
/Toast/Toast.vcxproj*
/Toast/x64/
/x64/
# Data files
/TriviaBot/data_management/questions
/TriviaBot/bot/db/trivia.db
/Toast/data_management/questions
/Toast/bot/db/trivia.db
# Config file
config.json

View File

@ -1,28 +0,0 @@
sudo: required
dist: trusty
language: cpp
compiler: gcc
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- boost-latest
packages:
- g++-5
- gcc-5
- libboost1.55-all-dev
- libcurl4-openssl-dev
- cmake
install: export CXX="g++-5" CC="gcc-5";
script:
- cd TriviaBot
- cmake .
- make
deploy:
provider: releases
api_key:
secure: sbZntceUqjyFZ13TlwuU0Jdtfma/jXHprv4z+pYrmuO3/YarKMtwlvM6UniagF2wVcHTFtiBmlbxfSyRfpy+P6XGkpvIJtJFrsMaSAoZpLI6mbSPsPxUCvc1VW3EESGIlWTJ97TZmU3opLjLSQIgYK1NB1+1KtLedVnQy6KCtTFthjupNwDIeSIFyPR31BgL0yQ7owPYZ63koS4U32ABPFxTYUPDbkI+Xq02nrzbn3OGKQY1cXmLJtvaTi+QQARfCFGYNTB4Ngt04LtpzBP5eeyj2P3YslZj9Xyr9PgqDt0uS2I5m/hLWsSLf/ssJ06EIBt2mYIORVV/XcgBw13VUBHawbd6hLfxSmSwhYpTewcjVbU1gE09kYmyVJ+KKKcbJjhWknIRd6z9+rLGwGb+zlgFnR9KFa4VqBSxGzfujwtBJVntaz0QVWb8vNL5U1xz1FyOBUT+6jWbr0L9d8QyK0ivPUbiYXwMQAoAEjjq7VbG985eyrTjxB6oxiWg9W8RveS1Mbb++/NWRSvsIfA1oEPrOgJONiOUP1+1XQ0rFLWpVHw5n4yuWFo6XpbfauCujIA4bNPGPqR0cfHj8QbUK9JDoA1SiOWsburQqUNqo0WHTUvrgut3OaP8jysDKsmoh8/tWeKlc6l5IqKQ4qu28sOQLd8RHcKOToKSfIOwRdA=
file: TriviaBot
skip_cleanup: true
on:
tags: true

View File

@ -1,4 +1,4 @@
#trivia-bot <img src="https://cdn.discordapp.com/attachments/164732409919569920/205700949304541184/emoji.png" width="30" height="30" /> <img src="https://travis-ci.org/jackb-p/trivia-bot.svg?branch=release" />
# Toast <img src="https://www.ahealthiermichigan.org/wp-content/uploads/2014/09/Transform-toast-into-breakfast.jpg" width="30" height="30" />
A bot which provides a Trivia game for [Discord](https://discordapp.com/).
@ -10,15 +10,27 @@ It requires no special permissions at this time (only read/write to channels).
### Installation
[Releases](https://github.com/jackb-p/trivia-bot/releases) are available for tagged versions. These are compiled on Ubuntu by Travis CI. To run on other systems you will need to compile yourself - a CMake configuration is included. Windows releases will happen one day.
[Releases](https://github.com/jackb-p/trivia-bot/releases) are available for tagged versions. These are compiled on Debian Jessie by Jenkins. Note that you still require the dependency library files, so you will still have to build V8 and add it to your `LD_LIBRARY_PATH`. To run on other systems you will need to compile yourself - a CMake configuration is included. Windows releases will happen one day.
If you want to install a version for which a release does not exist, you will also have to compile manually. Compilation instructions are available for Linux below.
### Running
To run simply execute the program: `./TriviaBot`
To run simply execute the program: `./Toast`
If you do not want to be prompted for your token every launch, provide it as an argument: `./TriviaBot {TOKEN}`
#### Configuration
The config file is automatically generated if it is not present. The JSON format is used. You must edit the config file for the bot to work correctly, the bot token is required.
The current configuration options are as follows:
1. **General**
| Field | Description |
| --- | --- |
| `api_cert_file` | The path to the Discord API .crt file for HTTPS. |
| `bot_token` | Your Discord bot token. |
| `owner_id` | The user ID of the owner of the bot. This allows owner-only (maintenance) commands, such as `shutdown`. |
| `js_allowed_roles` | List of role names which are allowed to use the `createjs` ands `js` commands. |
### Trivia Questions
Questions are obtained from [trivia-db on Sourceforge](https://sourceforge.net/projects/triviadb/).
@ -30,6 +42,7 @@ LoadDB.cpp takes some time to execute.
### Commands
#### Trivia Game
`` `trivia`` is the base command.
| Argument | Description |
@ -38,6 +51,11 @@ LoadDB.cpp takes some time to execute.
| stop | Stops the trivia game currently in the channel the message is sent from, if there is one. |
| help | Prints a help list, similar to this table. |
#### Javascript Commands
The Javascript system is designed to mirror the old [Boobot implementation](https://www.boobot.party/). For now there are some exceptions:
1. Message objects aren't implemented.
2. Properties *are* case sensitive. You must use `server.Name`, not `server.name`. This will not be changed.
### Compiling
#### Dependencies
@ -48,15 +66,16 @@ LoadDB.cpp takes some time to execute.
| cURL | [curl.haxx.se](https://curl.haxx.se/) | |
| sqlite3 | [sqlite.org](https://www.sqlite.org/) | Included as submodule. Uses a [different repo](https://github.com/azadkuh/sqlite-amalgamation/). |
| nlohmann/json | [nlohmann/json](https://github.com/nlohmann/json) | (Slightly modified) source file included in repo. |
| V8 | [Google V8](https://developers.google.com/v8/) | Debian/Ubuntu `libv8` packages are too outdated. |
| V8 | [Google V8](https://developers.google.com/v8/) | Debian/Ubuntu `libv8` packages are too outdated. Must be built manually. |
#### Linux (debian)
c++14 support is required. gcc 5 and above recommended.
#### Linux (Debian)
c++14 support is required. gcc 5 and above recommended, however it compiles on 4.9.2 (and possibly some versions below.)
1. Clone the github repo: `git clone https://github.com/jackb-p/TriviaDiscord.git TriviaDiscord`
2. Navigate to repository directory: `cd TriviaDiscord`
1. Clone the github repo: `git clone https://github.com/jackb-p/Toast.git ToastBot`
2. Navigate to repository directory: `cd ToastBot`
3. Clone the submodules: `git submodule init` and `git submodule update`
4. Install other dependencies: `sudo apt-get install cmake libboost-all-dev libcurl-dev` (Package managers and names may vary, but all of these should be easy to find through a simple Google search.)
5. `cd TriviaBot`
6. `cmake .`
7. `make`
4. Install other dependencies: `sudo apt-get install build-essential cmake libboost-all-dev libcurl4-openssl-dev libssl-dev` (Package managers and names may vary, but all of these should be easy to find through a simple Google search.) V8 may require other dependencies.
5. Build V8. Put the library files into lib/v8/lib/ and the include files into lib/v8/include. More instructions will be added at some point for this step.
6. `cd Toast`
7. `cmake .`
8. `make`

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 2.8.7)
project(TriviaBot)
project(Toast)
###############################################################################
## get source ## ##############################################################
@ -14,7 +14,7 @@ link_directories(../lib/v8/lib)
###############################################################################
# add the data to the target, so it becomes visible in some IDE
add_executable(TriviaBot ${sources})
add_executable(Toast ${sources})
# add some compiler flags
set (CMAKE_CXX_FLAGS "-std=c++14 -Wall ${CMAKE_CXX_FLAGS}")
@ -27,7 +27,7 @@ find_package(Boost COMPONENTS system thread regex REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(CURL REQUIRED)
target_link_libraries(TriviaBot PUBLIC
target_link_libraries(Toast PUBLIC
${Boost_LIBRARIES}
${OPENSSL_LIBRARIES}
${CURL_LIBRARIES}

View File

@ -34,7 +34,7 @@ void BotConfig::load_from_json(std::string data) {
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" });
js_allowed_roles = parsed["v8"].value("js_allowed_roles", std::unordered_set<std::string> { "Admin", "Coder" });
Logger::write("config.json file loaded", Logger::LogLevel::Info);
}
@ -45,7 +45,7 @@ void BotConfig::create_new_file() {
{ "owner_id", "" },
{ "api_cert_file", "bot/http/DiscordCA.crt" },
{ "v8", {
{ "createjs_allowed_roles", {
{ "js_allowed_roles", {
"Admin", "Coder", "Bot Commander"
} }
} }

View File

@ -13,7 +13,7 @@ public:
std::string token;
std::string owner_id;
std::string cert_location;
std::unordered_set<std::string> createjs_roles;
std::unordered_set<std::string> js_allowed_roles;
private:
void load_from_json(std::string data);

View File

@ -511,12 +511,22 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
DiscordAPI::send_message(channel.id, m, config.token, config.cert_location);
}
else if (words[0] == "`info") {
DiscordAPI::send_message(channel.id, ":information_source: **trivia-bot** by Jack. <http://github.com/jackb-p/TriviaDiscord>", config.token, config.cert_location);
DiscordAPI::send_message(channel.id, ":information_source: **toast** by Jack. <http://github.com/jackb-p/Toast>", config.token, config.cert_location);
}
else if (words[0] == "~js" && words.size() > 1) {
DiscordObjects::GuildMember *member = *std::find_if(guild.members.begin(), guild.members.end(), [sender](DiscordObjects::GuildMember *m) {
return sender.id == m->user->id;
});
BotConfig &conf = config;
bool disallowed = std::find_if(member->roles.begin(), member->roles.end(), [conf](DiscordObjects::Role *r) -> bool {
return conf.js_allowed_roles.count(r->name);
}) == member->roles.end(); // checks if the user has the required roles
if (disallowed) {
DiscordAPI::send_message(channel.id, ":warning: You do not have permission to use this command.", config.token, config.cert_location);
return;
}
std::string js = message.substr(4);
auto it = v8_instances.find(channel.guild_id);
if (it != v8_instances.end() && js.length() > 0) {
@ -527,7 +537,7 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
auto &member = *std::find_if(guild.members.begin(), guild.members.end(), [sender](DiscordObjects::GuildMember *m) { return sender.id == m->user->id; });
BotConfig &conf = config;
bool disallowed = std::find_if(member->roles.begin(), member->roles.end(), [conf](DiscordObjects::Role *r) -> bool {
return conf.createjs_roles.count(r->name);
return conf.js_allowed_roles.count(r->name);
}) == member->roles.end(); // checks if the user has the required roles
if (disallowed) {

View File

@ -368,6 +368,10 @@ void V8Instance::js_get_user(Local<Name> property, const PropertyCallbackInfo<Va
std::string name = member->nick == "null" ? member->user->username : member->nick;
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), name.c_str(), NewStringType::kNormal).ToLocalChecked());
}
else if (property_s == "TrueName") { // ignores nick
std::string name = member->user->username;
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), name.c_str(), NewStringType::kNormal).ToLocalChecked());
}
else if (property_s == "Mention") {
std::string mention = "<@" + member->user->id + ">";
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), mention.c_str(), NewStringType::kNormal).ToLocalChecked());

View File

@ -1,64 +0,0 @@
#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;
}