summaryrefslogtreecommitdiff
path: root/bot/src/modules/chatters.hpp
blob: fe921ab90c0ecf7ddb1bb57d41a60d107fcd6f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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);
        }
      }
  };
}