summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-05-05 19:45:51 +0500
committerilotterytea <iltsu@alright.party>2024-05-05 19:45:51 +0500
commit84bb9ad288d88ce4777137be402ecddf6c3f9a0e (patch)
tree45420eb2c859f484ca5da7af39e40f4c43bc3fd2 /src
parentb0fed946b38de699cde4ed44243f93bf1fc08811 (diff)
feat: new command - !event
Diffstat (limited to 'src')
-rw-r--r--src/commands/command.cpp2
-rw-r--r--src/localization/line_id.cpp6
-rw-r--r--src/localization/line_id.hpp5
-rw-r--r--src/modules/event.hpp145
4 files changed, 157 insertions, 1 deletions
diff --git a/src/commands/command.cpp b/src/commands/command.cpp
index 60db7c5..46dc03b 100644
--- a/src/commands/command.cpp
+++ b/src/commands/command.cpp
@@ -9,6 +9,7 @@
#include <string>
#include "../bundle.hpp"
+#include "../modules/event.hpp"
#include "../modules/massping.hpp"
#include "../modules/ping.hpp"
#include "../utils/chrono.hpp"
@@ -19,6 +20,7 @@ namespace bot {
CommandLoader::CommandLoader() {
this->add_command(std::make_unique<mod::Ping>());
this->add_command(std::make_unique<mod::Massping>());
+ this->add_command(std::make_unique<mod::Event>());
}
void CommandLoader::add_command(std::unique_ptr<Command> command) {
diff --git a/src/localization/line_id.cpp b/src/localization/line_id.cpp
index 242a61c..d2a2af9 100644
--- a/src/localization/line_id.cpp
+++ b/src/localization/line_id.cpp
@@ -44,6 +44,12 @@ namespace bot {
return LineId::ErrorInsufficientRights;
}
+ else if (str == "event.on") {
+ return LineId::EventOn;
+ } else if (str == "event.off") {
+ return LineId::EventOff;
+ }
+
else {
return std::nullopt;
}
diff --git a/src/localization/line_id.hpp b/src/localization/line_id.hpp
index 6f7a882..3cd46c0 100644
--- a/src/localization/line_id.hpp
+++ b/src/localization/line_id.hpp
@@ -24,7 +24,10 @@ namespace bot {
ErrorExternalAPIError,
ErrorInsufficientRights,
- PingResponse
+ PingResponse,
+
+ EventOn,
+ EventOff
};
std::optional<LineId> string_to_line_id(const std::string &str);
diff --git a/src/modules/event.hpp b/src/modules/event.hpp
new file mode 100644
index 0000000..4242f07
--- /dev/null
+++ b/src/modules/event.hpp
@@ -0,0 +1,145 @@
+#pragma once
+
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "../bundle.hpp"
+#include "../commands/command.hpp"
+#include "../commands/response_error.hpp"
+#include "../schemas/stream.hpp"
+
+namespace bot {
+ namespace mod {
+ class Event : public command::Command {
+ std::string get_name() const override { return "event"; }
+
+ schemas::PermissionLevel get_permission_level() const override {
+ return schemas::PermissionLevel::MODERATOR;
+ }
+
+ std::vector<std::string> get_subcommand_ids() const override {
+ return {"on", "off"};
+ }
+
+ std::variant<std::vector<std::string>, std::string> run(
+ const InstanceBundle &bundle,
+ const command::Request &request) const override {
+ if (!request.subcommand_id.has_value()) {
+ throw ResponseException<NOT_ENOUGH_ARGUMENTS>(
+ request, bundle.localization, command::SUBCOMMAND);
+ }
+
+ const std::string &subcommand_id = request.subcommand_id.value();
+
+ if (!request.message.has_value()) {
+ throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>(
+ request, bundle.localization, command::CommandArgument::TARGET);
+ }
+
+ const std::string &message = request.message.value();
+ std::vector<std::string> s = utils::string::split_text(message, ' ');
+
+ std::string target;
+ schemas::EventType type;
+
+ std::vector<std::string> target_and_type =
+ utils::string::split_text(s[0], ':');
+
+ if (target_and_type.size() != 2) {
+ throw ResponseException<ResponseError::INCORRECT_ARGUMENT>(
+ request, bundle.localization, s[0]);
+ }
+
+ s.erase(s.begin());
+
+ target = target_and_type[0];
+ type = schemas::string_to_event_type(target_and_type[1]);
+
+ std::string t = target_and_type[0] + ":" + target_and_type[1];
+
+ auto channels = bundle.helix_client.get_users({target});
+ api::twitch::schemas::User channel;
+
+ if (channels.empty() && type != schemas::EventType::CUSTOM) {
+ throw ResponseException<ResponseError::NOT_FOUND>(
+ request, bundle.localization, t);
+ }
+
+ pqxx::work work(request.conn);
+ std::string query;
+
+ if (type != schemas::CUSTOM) {
+ channel = channels[0];
+
+ query = "SELECT id FROM events WHERE channel_id = " +
+ std::to_string(request.channel.get_id()) +
+ " AND target_alias_id = " + std::to_string(channel.id) +
+ " AND event_type = " + std::to_string(type);
+ } else {
+ query = "SELECT id FROM events WHERE channel_id = " +
+ std::to_string(request.channel.get_id()) +
+ " AND custom_alias_id = '" + target +
+ "' AND event_type = " + std::to_string(type);
+ }
+
+ pqxx::result event = work.exec(query);
+
+ if (subcommand_id == "on") {
+ if (!event.empty()) {
+ throw ResponseException<ResponseError::NAMESAKE_CREATION>(
+ request, bundle.localization, t);
+ }
+
+ if (s.empty()) {
+ throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>(
+ request, bundle.localization,
+ command::CommandArgument::MESSAGE);
+ }
+
+ std::string m = utils::string::str(s.begin(), s.end(), ' ');
+
+ if (type != schemas::CUSTOM) {
+ query =
+ "INSERT INTO events (channel_id, target_alias_id, "
+ "event_type, "
+ "message) VALUES (" +
+ std::to_string(request.channel.get_id()) + ", " +
+ std::to_string(channel.id) + ", " + std::to_string(type) +
+ ", '" + m + "')";
+ } else {
+ query =
+ "INSERT INTO events (channel_id, custom_alias_id, "
+ "event_type, "
+ "message) VALUES (" +
+ std::to_string(request.channel.get_id()) + ", '" + target +
+ "', " + std::to_string(type) + ", '" + m + "')";
+ }
+
+ work.exec(query);
+ work.commit();
+
+ return bundle.localization
+ .get_formatted_line(request, loc::LineId::EventOn, {t})
+ .value();
+ } else if (subcommand_id == "off") {
+ if (event.empty()) {
+ throw ResponseException<ResponseError::NOT_FOUND>(
+ request, bundle.localization, t);
+ }
+
+ work.exec("DELETE FROM events WHERE id = " +
+ std::to_string(event[0][0].as<int>()));
+ work.commit();
+
+ return bundle.localization
+ .get_formatted_line(request, loc::LineId::EventOff, {t})
+ .value();
+ }
+
+ throw ResponseException<ResponseError::SOMETHING_WENT_WRONG>(
+ request, bundle.localization);
+ }
+ };
+ }
+}