Apply same permissions from ~createjs to ~js

This commit is contained in:
Jack Bond-Preston 2016-08-19 17:29:56 +01:00
parent c566e7f04f
commit 0498b22c8c
3 changed files with 13 additions and 3 deletions

View File

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

View File

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

View File

@ -517,6 +517,16 @@ void GatewayHandler::on_event_message_create(json data, client &c, websocketpp::
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) {
return sender.id == m->user->id; 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);
}) == 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); std::string js = message.substr(4);
auto it = v8_instances.find(channel.guild_id); auto it = v8_instances.find(channel.guild_id);
if (it != v8_instances.end() && js.length() > 0) { if (it != v8_instances.end() && js.length() > 0) {