summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-07-03 18:05:11 +0500
committerilotterytea <iltsu@alright.party>2025-07-03 18:05:47 +0500
commite23ccfb00c8fa39926b32e94be6bc379e70ba11d (patch)
treead4758e8a1b947782186f11f1739dde1aca9ca23
parent1e8f182b083679b06d8e30fe52b1ed1130a19287 (diff)
feat: separated requester from request
-rw-r--r--bot/src/commands/command.cpp13
-rw-r--r--bot/src/commands/lua.cpp15
-rw-r--r--bot/src/commands/request.cpp27
-rw-r--r--bot/src/commands/request.hpp16
-rw-r--r--bot/src/commands/response_error.hpp3
-rw-r--r--bot/src/irc/client.cpp4
-rw-r--r--bot/src/localization/localization.cpp9
7 files changed, 53 insertions, 34 deletions
diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp
index 533376a..2d34212 100644
--- a/bot/src/commands/command.cpp
+++ b/bot/src/commands/command.cpp
@@ -81,7 +81,7 @@ namespace bot {
}
if ((*command)->get_permission_level() >
- request.user_rights.get_level()) {
+ request.requester.user_rights.get_level()) {
return std::nullopt;
}
@@ -91,8 +91,9 @@ namespace bot {
db::DatabaseRows actions = conn->exec(
"SELECT sent_at FROM actions WHERE user_id = $1 AND channel_id = $2 "
"AND command = $3 ORDER BY sent_at DESC",
- {std::to_string(request.user.get_id()),
- std::to_string(request.channel.get_id()), request.command_id});
+ {std::to_string(request.requester.user.get_id()),
+ std::to_string(request.requester.channel.get_id()),
+ request.command_id});
if (!actions.empty()) {
auto last_sent_at =
@@ -124,9 +125,9 @@ namespace bot {
conn->exec(
"INSERT INTO actions(user_id, channel_id, command, arguments, "
"full_message) VALUES ($1, $2, $3, $4, $5)",
- {std::to_string(request.user.get_id()),
- std::to_string(request.channel.get_id()), request.command_id,
- arguments, request.irc_message.message});
+ {std::to_string(request.requester.user.get_id()),
+ std::to_string(request.requester.channel.get_id()),
+ request.command_id, arguments, request.irc_message.message});
return (*command)->run(bundle, request);
}
diff --git a/bot/src/commands/lua.cpp b/bot/src/commands/lua.cpp
index 7c4f616..a35e605 100644
--- a/bot/src/commands/lua.cpp
+++ b/bot/src/commands/lua.cpp
@@ -641,7 +641,8 @@ namespace bot::command::lua {
// TODO: ratelimits
state->set_function("twitch_get_chatters", [state, &request, &bundle]() {
auto chatters = bundle.helix_client.get_chatters(
- request.channel.get_alias_id(), bundle.irc_client.get_bot_id());
+ request.requester.channel.get_alias_id(),
+ bundle.irc_client.get_bot_id());
sol::table o = state->create_table();
@@ -738,8 +739,8 @@ namespace bot::command::lua {
const std::string &lua_id) {
state->set_function("storage_get", [state, &request, &cfg, &lua_id]() {
std::unique_ptr<db::BaseDatabase> conn = db::create_connection(cfg);
- std::vector<std::string> params{std::to_string(request.user.get_id()),
- lua_id};
+ std::vector<std::string> params{
+ std::to_string(request.requester.user.get_id()), lua_id};
db::DatabaseRows rows = conn->exec(
"SELECT value FROM lua_user_storage WHERE user_id = $1 AND "
@@ -763,8 +764,8 @@ namespace bot::command::lua {
state->set_function("storage_put", [state, &request, &cfg,
&lua_id](const std::string &value) {
std::unique_ptr<db::BaseDatabase> conn = db::create_connection(cfg);
- std::vector<std::string> params{std::to_string(request.user.get_id()),
- lua_id};
+ std::vector<std::string> params{
+ std::to_string(request.requester.user.get_id()), lua_id};
db::DatabaseRows rows = conn->exec(
"SELECT id FROM lua_user_storage WHERE user_id = $1 AND "
@@ -790,7 +791,7 @@ namespace bot::command::lua {
&lua_id]() {
std::unique_ptr<db::BaseDatabase> conn = db::create_connection(cfg);
std::vector<std::string> params{
- std::to_string(request.channel.get_id()), lua_id};
+ std::to_string(request.requester.channel.get_id()), lua_id};
db::DatabaseRows rows = conn->exec(
"SELECT value FROM lua_channel_storage WHERE channel_id = $1 AND "
@@ -816,7 +817,7 @@ namespace bot::command::lua {
[state, &request, &cfg, &lua_id](const std::string &value) {
std::unique_ptr<db::BaseDatabase> conn = db::create_connection(cfg);
std::vector<std::string> params{
- std::to_string(request.channel.get_id()), lua_id};
+ std::to_string(request.requester.channel.get_id()), lua_id};
db::DatabaseRows rows = conn->exec(
"SELECT id FROM lua_channel_storage WHERE channel_id = $1 AND "
diff --git a/bot/src/commands/request.cpp b/bot/src/commands/request.cpp
index 434c222..e5a1e7e 100644
--- a/bot/src/commands/request.cpp
+++ b/bot/src/commands/request.cpp
@@ -27,16 +27,16 @@ namespace bot::command {
o["message"] = sol::lua_nil;
}
- o["sender"] = this->user.as_lua_table(luaState);
- o["channel"] = this->channel.as_lua_table(luaState);
- o["channel_preference"] = this->channel_preferences.as_lua_table(luaState);
- o["rights"] = this->user_rights.as_lua_table(luaState);
+ o["sender"] = requester.user.as_lua_table(luaState);
+ o["channel"] = requester.channel.as_lua_table(luaState);
+ o["channel_preference"] =
+ requester.channel_preferences.as_lua_table(luaState);
+ o["rights"] = requester.user_rights.as_lua_table(luaState);
return o;
}
- std::optional<Request> generate_request(
- const command::CommandLoader &command_loader,
+ std::optional<Requester> get_requester(
const irc::Message<irc::MessageType::Privmsg> &irc_message,
std::unique_ptr<db::BaseDatabase> &conn) {
// fetching channel
@@ -146,12 +146,20 @@ namespace bot::command {
user_right.set_level(level);
}
+ return (Requester){channel, pref, user, user_right};
+ }
+
+ std::optional<Request> generate_request(
+ const command::CommandLoader &command_loader,
+ const irc::Message<irc::MessageType::Privmsg> &irc_message,
+ const Requester &requester, std::unique_ptr<db::BaseDatabase> &conn) {
// --- FETCHING MESSAGES
std::string fullmsg = irc_message.message;
- const std::string &prefix = pref.get_prefix();
+ const std::string &prefix = requester.channel_preferences.get_prefix();
if (fullmsg.empty() || fullmsg.substr(0, prefix.length()) != prefix ||
- std::any_of(pref.get_features().begin(), pref.get_features().end(),
+ std::any_of(requester.channel_preferences.get_features().begin(),
+ requester.channel_preferences.get_features().end(),
[](const schemas::ChannelFeature &f) {
return f == schemas::ChannelFeature::SILENT_MODE;
})) {
@@ -175,8 +183,7 @@ namespace bot::command {
parts.erase(parts.begin());
- Request req{command_id, std::nullopt, std::nullopt, irc_message,
- channel, pref, user, user_right};
+ Request req{command_id, std::nullopt, std::nullopt, irc_message, requester};
if (parts.empty()) {
return req;
diff --git a/bot/src/commands/request.hpp b/bot/src/commands/request.hpp
index 9822fc8..00d8afc 100644
--- a/bot/src/commands/request.hpp
+++ b/bot/src/commands/request.hpp
@@ -18,16 +18,20 @@ namespace bot::command {
#include "database.hpp"
namespace bot::command {
+ struct Requester {
+ schemas::Channel channel;
+ schemas::ChannelPreferences channel_preferences;
+ schemas::User user;
+ schemas::UserRights user_rights;
+ };
+
struct Request {
std::string command_id;
std::optional<std::string> subcommand_id;
std::optional<std::string> message;
const irc::Message<irc::MessageType::Privmsg> &irc_message;
- schemas::Channel channel;
- schemas::ChannelPreferences channel_preferences;
- schemas::User user;
- schemas::UserRights user_rights;
+ const Requester requester;
sol::table as_lua_table(std::shared_ptr<sol::state> luaState) const;
};
@@ -35,5 +39,9 @@ namespace bot::command {
std::optional<Request> generate_request(
const command::CommandLoader &command_loader,
const irc::Message<irc::MessageType::Privmsg> &irc_message,
+ const Requester &requester, std::unique_ptr<db::BaseDatabase> &conn);
+
+ std::optional<Requester> get_requester(
+ const irc::Message<irc::MessageType::Privmsg> &irc_message,
std::unique_ptr<db::BaseDatabase> &conn);
}
diff --git a/bot/src/commands/response_error.hpp b/bot/src/commands/response_error.hpp
index 9e7c612..139385d 100644
--- a/bot/src/commands/response_error.hpp
+++ b/bot/src/commands/response_error.hpp
@@ -209,7 +209,8 @@ namespace bot {
auto arg =
this->localizator
.get_localized_line(
- this->request.channel_preferences.get_locale(), arg_id)
+ this->request.requester.channel_preferences.get_locale(),
+ arg_id)
.value();
this->line =
diff --git a/bot/src/irc/client.cpp b/bot/src/irc/client.cpp
index 06b7a03..4f8fe6c 100644
--- a/bot/src/irc/client.cpp
+++ b/bot/src/irc/client.cpp
@@ -50,7 +50,7 @@ void Client::run() {
[this](const ix::WebSocketMessagePtr &msg) {
switch (msg->type) {
case ix::WebSocketMessageType::Message: {
- log::debug("IRC", "Received message: " + msg->str);
+ log::info("IRC", "Received message: " + msg->str);
std::vector<std::string> lines =
utils::string::split_text(msg->str, '\n');
@@ -120,7 +120,7 @@ void Client::run() {
void Client::say(const std::string &channel_login, const std::string &message) {
this->raw("PRIVMSG #" + channel_login + " :" + message);
- log::debug("IRC", "Sent '" + message + "' in #" + channel_login);
+ log::info("IRC", "Sent '" + message + "' in #" + channel_login);
}
bool Client::join(const std::string &channel_login) {
diff --git a/bot/src/localization/localization.cpp b/bot/src/localization/localization.cpp
index b8659ea..fe7a02a 100644
--- a/bot/src/localization/localization.cpp
+++ b/bot/src/localization/localization.cpp
@@ -105,7 +105,7 @@ namespace bot {
const command::Request &request, const LineId &line_id,
const std::vector<std::string> &args) const {
std::optional<std::string> o_line = this->get_formatted_line(
- request.channel_preferences.get_locale(), line_id, args);
+ request.requester.channel_preferences.get_locale(), line_id, args);
if (!o_line.has_value()) {
return std::nullopt;
@@ -114,10 +114,11 @@ namespace bot {
std::string line = o_line.value();
std::map<std::string, std::string> token_map = {
- {"{sender.alias_name}", request.user.get_alias_name()},
- {"{source.alias_name}", request.channel.get_alias_name()},
+ {"{sender.alias_name}", request.requester.user.get_alias_name()},
+ {"{source.alias_name}", request.requester.channel.get_alias_name()},
{"{default.prefix}", DEFAULT_PREFIX},
- {"{channel.prefix}", request.channel_preferences.get_prefix()}};
+ {"{channel.prefix}",
+ request.requester.channel_preferences.get_prefix()}};
for (const auto &pair : token_map) {
int pos = line.find(pair.first);