summaryrefslogtreecommitdiff
path: root/bot/src
diff options
context:
space:
mode:
Diffstat (limited to 'bot/src')
-rw-r--r--bot/src/commands/command.cpp2
-rw-r--r--bot/src/config.cpp2
-rw-r--r--bot/src/config.hpp1
-rw-r--r--bot/src/localization/line_id.cpp4
-rw-r--r--bot/src/localization/line_id.hpp4
-rw-r--r--bot/src/modules/chatters.hpp78
6 files changed, 90 insertions, 1 deletions
diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp
index e3b45b1..b656254 100644
--- a/bot/src/commands/command.cpp
+++ b/bot/src/commands/command.cpp
@@ -9,6 +9,7 @@
#include <string>
#include "../bundle.hpp"
+#include "../modules/chatters.hpp"
#include "../modules/custom_command.hpp"
#include "../modules/event.hpp"
#include "../modules/help.hpp"
@@ -31,6 +32,7 @@ namespace bot {
this->add_command(std::make_unique<mod::CustomCommand>());
this->add_command(std::make_unique<mod::Timer>());
this->add_command(std::make_unique<mod::Help>());
+ this->add_command(std::make_unique<mod::Chatters>());
}
void CommandLoader::add_command(std::unique_ptr<Command> command) {
diff --git a/bot/src/config.cpp b/bot/src/config.cpp
index ec55913..6c8e6d6 100644
--- a/bot/src/config.cpp
+++ b/bot/src/config.cpp
@@ -68,6 +68,8 @@ namespace bot {
else if (key == "url.help") {
url_cfg.help = value;
+ } else if (key == "url.chatters.paste_service") {
+ url_cfg.paste_service = value;
}
}
diff --git a/bot/src/config.hpp b/bot/src/config.hpp
index 5c437d6..8707d02 100644
--- a/bot/src/config.hpp
+++ b/bot/src/config.hpp
@@ -39,6 +39,7 @@ namespace bot {
struct UrlConfiguration {
std::optional<std::string> help = std::nullopt;
+ std::optional<std::string> paste_service = std::nullopt;
};
struct Configuration {
diff --git a/bot/src/localization/line_id.cpp b/bot/src/localization/line_id.cpp
index 567a3ba..dabd85a 100644
--- a/bot/src/localization/line_id.cpp
+++ b/bot/src/localization/line_id.cpp
@@ -92,6 +92,10 @@ namespace bot {
return LineId::HelpResponse;
}
+ else if (str == "chatters.response") {
+ return LineId::ChattersResponse;
+ }
+
else {
return std::nullopt;
}
diff --git a/bot/src/localization/line_id.hpp b/bot/src/localization/line_id.hpp
index 41ceec6..6d7729e 100644
--- a/bot/src/localization/line_id.hpp
+++ b/bot/src/localization/line_id.hpp
@@ -48,7 +48,9 @@ namespace bot {
TimerNew,
TimerDelete,
- HelpResponse
+ HelpResponse,
+
+ ChattersResponse
};
std::optional<LineId> string_to_line_id(const std::string &str);
diff --git a/bot/src/modules/chatters.hpp b/bot/src/modules/chatters.hpp
new file mode 100644
index 0000000..12d641f
--- /dev/null
+++ b/bot/src/modules/chatters.hpp
@@ -0,0 +1,78 @@
+#pragma once
+
+#include <ctime>
+#include <iomanip>
+#include <sstream>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "../bundle.hpp"
+#include "../commands/command.hpp"
+#include "../commands/response_error.hpp"
+#include "cpr/api.h"
+#include "cpr/multipart.h"
+#include "cpr/response.h"
+#include "nlohmann/json.hpp"
+
+namespace bot::mod {
+ class Chatters : public command::Command {
+ std::string get_name() const override { return "chatters"; }
+
+ schemas::PermissionLevel get_permission_level() const override {
+ return schemas::PermissionLevel::USER;
+ }
+
+ int get_delay_seconds() const override { return 10; }
+
+ std::variant<std::vector<std::string>, std::string> run(
+ const InstanceBundle &bundle,
+ const command::Request &request) const override {
+ if (!bundle.configuration.url.paste_service.has_value()) {
+ throw ResponseException<ResponseError::ILLEGAL_COMMAND>(
+ request, bundle.localization);
+ }
+
+ auto chatters = bundle.helix_client.get_chatters(
+ request.channel.get_alias_id(), bundle.irc_client.get_bot_id());
+
+ std::string body;
+
+ for (const auto &chatter : chatters) {
+ body += chatter.login + '\n';
+ }
+
+ std::time_t t = std::time(nullptr);
+ std::tm *now = std::localtime(&t);
+
+ std::ostringstream oss;
+
+ oss << std::put_time(now, "%d.%m.%Y %H:%M:%s");
+
+ cpr::Multipart multipart = {
+ {"paste", body},
+ {"title", request.channel.get_alias_name() + "'s chatter list on " +
+ oss.str()}};
+
+ cpr::Response response = cpr::Post(
+ cpr::Url{*bundle.configuration.url.paste_service + "/paste"},
+ multipart);
+
+ if (response.status_code == 201) {
+ nlohmann::json j = nlohmann::json::parse(response.text);
+
+ std::string id = j["data"]["id"];
+
+ std::string url = *bundle.configuration.url.paste_service + "/" + id;
+
+ return bundle.localization
+ .get_formatted_line(request, loc::LineId::ChattersResponse, {url})
+ .value();
+ } else {
+ throw ResponseException<ResponseError::EXTERNAL_API_ERROR>(
+ request, bundle.localization, response.status_code,
+ response.status_line);
+ }
+ }
+ };
+}