summaryrefslogtreecommitdiff
path: root/bot/src/modules
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-05-25 00:30:01 +0500
committerilotterytea <iltsu@alright.party>2024-05-25 00:30:01 +0500
commit2a90152bc7b3a9009b3a90cbc021fde8383f4b9b (patch)
tree876190f88c9010c91f2cf81dadbc52c13565afa6 /bot/src/modules
parent59b52bfab39d98a11052497ab387011a9835ef1b (diff)
feat: new command - chatters
Diffstat (limited to 'bot/src/modules')
-rw-r--r--bot/src/modules/chatters.hpp78
1 files changed, 78 insertions, 0 deletions
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);
+ }
+ }
+ };
+}