diff options
| author | ilotterytea <iltsu@alright.party> | 2025-12-04 01:26:30 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-12-04 01:26:30 +0500 |
| commit | 428998889e9bb60c7dc46388184b07c489ddaeb2 (patch) | |
| tree | 872a11239eebbf930c85d846f7cec9f828a43ab5 | |
| parent | 638622d66dbe58ff96b7d1c2c6ec4b040b27da6d (diff) | |
feat: trusted users
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | bot/src/commands/lua.cpp | 2 | ||||
| -rw-r--r-- | bot/src/commands/lua.hpp | 4 | ||||
| -rw-r--r-- | bot/src/commands/request.cpp | 11 | ||||
| -rw-r--r-- | bot/src/commands/request.hpp | 2 | ||||
| -rw-r--r-- | bot/src/config.cpp | 24 | ||||
| -rw-r--r-- | bot/src/config.hpp | 7 | ||||
| -rw-r--r-- | bot/src/handlers.cpp | 2 | ||||
| -rw-r--r-- | bot/src/main.cpp | 12 | ||||
| -rw-r--r-- | bot/src/schemas/user.hpp | 9 |
10 files changed, 50 insertions, 27 deletions
@@ -66,8 +66,8 @@ Here's example of `.env` file with required parameters. This file should be alon db_name=DB_NAME db_user=DB_USER db_pass=DB_PASS -twitch_credentials.client_id=CLIENT_ID -twitch_credentials.token=TOKEN +twitch.client_id=CLIENT_ID +twitch.token=TOKEN ``` ### 5. Launch diff --git a/bot/src/commands/lua.cpp b/bot/src/commands/lua.cpp index f0d3dab..9e84f8e 100644 --- a/bot/src/commands/lua.cpp +++ b/bot/src/commands/lua.cpp @@ -1005,6 +1005,8 @@ namespace bot::command::lua { this->level = schemas::PermissionLevel::MODERATOR; } else if (rights_text == "broadcaster") { this->level = schemas::PermissionLevel::BROADCASTER; + } else if (rights_text == "trusted") { + this->level = schemas::PermissionLevel::TRUSTED; } else { this->level = schemas::PermissionLevel::USER; } diff --git a/bot/src/commands/lua.hpp b/bot/src/commands/lua.hpp index 2f236f2..1a1c9f9 100644 --- a/bot/src/commands/lua.hpp +++ b/bot/src/commands/lua.hpp @@ -97,7 +97,8 @@ namespace bot::command::lua { command::Response run(const InstanceBundle& bundle, const command::Request& request) const override { - if (!bundle.configuration.lua.allow_arbitrary_scripts) { + if (!bundle.configuration.lua.allow_arbitrary_scripts && + request.requester.user_rights.get_level() < schemas::TRUSTED) { throw ResponseException<ResponseError::ILLEGAL_COMMAND>( request, bundle.localization); } @@ -126,6 +127,7 @@ namespace bot::command::lua { } if (!bundle.configuration.lua.allow_arbitrary_scripts && + request.requester.user_rights.get_level() < schemas::TRUSTED && !std::any_of(bundle.configuration.lua.script_whitelist.begin(), bundle.configuration.lua.script_whitelist.end(), [&request](const std::string& i) { diff --git a/bot/src/commands/request.cpp b/bot/src/commands/request.cpp index d8a91f9..28638ef 100644 --- a/bot/src/commands/request.cpp +++ b/bot/src/commands/request.cpp @@ -6,9 +6,11 @@ #include <string> #include <vector> +#include "config.hpp" #include "constants.hpp" #include "database.hpp" #include "schemas/channel.hpp" +#include "schemas/user.hpp" #include "utils/string.hpp" namespace bot::command { @@ -38,7 +40,7 @@ namespace bot::command { std::optional<Requester> get_requester( const irc::Message<irc::MessageType::Privmsg> &irc_message, - std::unique_ptr<db::BaseDatabase> &conn) { + std::unique_ptr<db::BaseDatabase> &conn, const Configuration &cfg) { // fetching channel std::vector<schemas::Channel> chans = conn->query_all<schemas::Channel>( "SELECT * FROM channels WHERE alias_id = $1", @@ -109,7 +111,12 @@ namespace bot::command { schemas::PermissionLevel level = schemas::PermissionLevel::USER; const auto &badges = irc_message.sender.badges; - if (user.get_alias_id() == channel.get_alias_id()) { + if (std::any_of(cfg.twitch.trusted_user_ids.begin(), + cfg.twitch.trusted_user_ids.end(), [&user](const int &x) { + return x == user.get_alias_id(); + })) { + level = schemas::PermissionLevel::TRUSTED; + } else if (user.get_alias_id() == channel.get_alias_id()) { level = schemas::PermissionLevel::BROADCASTER; } else if (std::any_of(badges.begin(), badges.end(), [&](const auto &x) { return x.first == "moderator"; diff --git a/bot/src/commands/request.hpp b/bot/src/commands/request.hpp index 00d8afc..d76fa6e 100644 --- a/bot/src/commands/request.hpp +++ b/bot/src/commands/request.hpp @@ -43,5 +43,5 @@ namespace bot::command { std::optional<Requester> get_requester( const irc::Message<irc::MessageType::Privmsg> &irc_message, - std::unique_ptr<db::BaseDatabase> &conn); + std::unique_ptr<db::BaseDatabase> &conn, const Configuration &cfg); } diff --git a/bot/src/config.cpp b/bot/src/config.cpp index ccb042a..a2cd2d6 100644 --- a/bot/src/config.cpp +++ b/bot/src/config.cpp @@ -82,7 +82,7 @@ namespace bot { } std::optional<Configuration> parse_configuration_from_file( - const std::string& file_path) { + const std::string &file_path) { std::ifstream ifs(file_path); if (!ifs.is_open()) { @@ -91,7 +91,7 @@ namespace bot { } Configuration cfg; - TwitchCredentialsConfiguration ttv_crd_cfg; + TwitchConfiguration ttv_cfg; KickCredentialsConfiguration kick_crd_cfg; DatabaseConfiguration db_cfg; CommandConfiguration cmd_cfg; @@ -110,15 +110,21 @@ namespace bot { std::getline(iss, key, '='); std::getline(iss, value); - for (char& c : key) { + for (char &c : key) { c = tolower(c); } - if (key == "twitch_credentials.client_id") { - ttv_crd_cfg.client_id = value; - } else if (key == "twitch_credentials.token") { - ttv_crd_cfg.token = value; - } else if (key == "db_name") { + if (key == "twitch.client_id") { + ttv_cfg.client_id = value; + } else if (key == "twitch.token") { + ttv_cfg.token = value; + } else if (key == "twitch.trusted_user_ids") { + for (const std::string &x : utils::string::split_text(value, ',')) { + ttv_cfg.trusted_user_ids.push_back(std::stoi(x)); + } + } + + else if (key == "db_name") { db_cfg.name = value; } else if (key == "db_user") { db_cfg.user = value; @@ -184,7 +190,7 @@ namespace bot { cfg.url = url_cfg; cfg.owner = owner_cfg; cfg.commands = cmd_cfg; - cfg.twitch_credentials = ttv_crd_cfg; + cfg.twitch = ttv_cfg; cfg.kick_credentials = kick_crd_cfg; cfg.database = db_cfg; cfg.tokens = token_cfg; diff --git a/bot/src/config.hpp b/bot/src/config.hpp index 70491bd..020721d 100644 --- a/bot/src/config.hpp +++ b/bot/src/config.hpp @@ -24,9 +24,10 @@ namespace bot { std::string port; }; - struct TwitchCredentialsConfiguration { + struct TwitchConfiguration { std::string client_id; std::string token; + std::vector<int> trusted_user_ids; }; struct KickCredentialsConfiguration { @@ -70,7 +71,7 @@ namespace bot { }; struct Configuration { - TwitchCredentialsConfiguration twitch_credentials; + TwitchConfiguration twitch; KickCredentialsConfiguration kick_credentials; DatabaseConfiguration database; CommandConfiguration commands; @@ -84,5 +85,5 @@ namespace bot { }; std::optional<Configuration> parse_configuration_from_file( - const std::string& file_path); + const std::string &file_path); } diff --git a/bot/src/handlers.cpp b/bot/src/handlers.cpp index 46ae51d..2d19475 100644 --- a/bot/src/handlers.cpp +++ b/bot/src/handlers.cpp @@ -130,7 +130,7 @@ namespace bot::handlers { db::create_connection(bundle.configuration); std::optional<command::Requester> requester = - command::get_requester(message, conn); + command::get_requester(message, conn, bundle.configuration); if (!requester.has_value()) { return; diff --git a/bot/src/main.cpp b/bot/src/main.cpp index 9d694e0..967e36e 100644 --- a/bot/src/main.cpp +++ b/bot/src/main.cpp @@ -39,10 +39,9 @@ int main(int argc, char *argv[]) { bot::Configuration cfg = o_cfg.value(); - if (cfg.twitch_credentials.client_id.empty() || - cfg.twitch_credentials.token.empty()) { + if (cfg.twitch.client_id.empty() || cfg.twitch.token.empty()) { bot::log::error("Main", - "TWITCH_CREDENTIALS.CLIENT_ID and TWITCH_CREDENTIALS.TOKEN " + "TWITCH.CLIENT_ID and TWITCH.TOKEN " "must be set in environmental file!"); return 1; } @@ -56,14 +55,13 @@ int main(int argc, char *argv[]) { return 1; } - bot::irc::Client client(cfg.twitch_credentials.client_id, - cfg.twitch_credentials.token); + bot::irc::Client client(cfg.twitch.client_id, cfg.twitch.token); bot::command::CommandLoader command_loader; command_loader.load_lua_directory("luamods"); bot::loc::Localization localization("localization"); - bot::api::twitch::HelixClient helix_client(cfg.twitch_credentials.token, - cfg.twitch_credentials.client_id); + bot::api::twitch::HelixClient helix_client(cfg.twitch.token, + cfg.twitch.client_id); bot::api::KickAPIClient kick_api_client(cfg.kick_credentials.client_id, cfg.kick_credentials.client_secret); diff --git a/bot/src/schemas/user.hpp b/bot/src/schemas/user.hpp index ee9bd10..2b8de81 100644 --- a/bot/src/schemas/user.hpp +++ b/bot/src/schemas/user.hpp @@ -50,7 +50,14 @@ namespace bot::schemas { std::optional<std::chrono::system_clock::time_point> opted_out_at; }; - enum PermissionLevel { SUSPENDED, USER, VIP, MODERATOR, BROADCASTER }; + enum PermissionLevel { + SUSPENDED, + USER, + VIP, + MODERATOR, + BROADCASTER, + TRUSTED = 50 + }; class UserRights { public: |
