summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 3fbf02429e2173f372e5aec95fc90dfaf20ca664 (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
79
80
81
82
83
84
85
#include <iostream>
#include <optional>
#include <pqxx/pqxx>
#include <string>

#include "api/twitch/helix_client.hpp"
#include "bundle.hpp"
#include "commands/command.hpp"
#include "config.hpp"
#include "handlers.hpp"
#include "irc/client.hpp"
#include "irc/message.hpp"
#include "localization/localization.hpp"
#include "stream.hpp"

int main(int argc, char *argv[]) {
  std::cout << "hi world\n";

  std::optional<bot::Configuration> o_cfg =
      bot::parse_configuration_from_file(".env");

  if (!o_cfg.has_value()) {
    return -1;
  }

  bot::Configuration cfg = o_cfg.value();

  if (cfg.bot_password.empty() || cfg.bot_username.empty() ||
      cfg.bot_client_id.empty()) {
    std::cerr
        << "*** BOT_USERNAME, BOT_CLIENT_ID and BOT_PASSWORD must be set!\n";
    return -1;
  }

  if (cfg.database.name.empty() || cfg.database.user.empty() ||
      cfg.database.password.empty() || cfg.database.host.empty() ||
      cfg.database.port.empty()) {
    std::cerr
        << "*** DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT must be set!\n";
    return -1;
  }

  bot::irc::Client client(cfg.bot_client_id, cfg.bot_password);
  bot::command::CommandLoader command_loader;
  bot::loc::Localization localization("localization");

  client.join(cfg.bot_username);

  pqxx::connection conn(GET_DATABASE_CONNECTION_URL(cfg));
  pqxx::work work(conn);

  auto rows = work.exec("SELECT alias_name FROM channels");

  for (const auto &row : rows) {
    auto name = row[0].as<std::string>();
    client.join(name);
  }

  work.commit();
  conn.close();

  bot::api::twitch::HelixClient helix_client(cfg.bot_password,
                                             cfg.bot_client_id);

  bot::stream::StreamListenerClient stream_listener_client(helix_client, client,
                                                           cfg);

  client.on<bot::irc::MessageType::Privmsg>(
      [&client, &command_loader, &localization, &cfg, &helix_client](
          const bot::irc::Message<bot::irc::MessageType::Privmsg> &message) {
        bot::InstanceBundle bundle{client, helix_client, localization};

        pqxx::connection conn(GET_DATABASE_CONNECTION_URL(cfg));

        bot::handlers::handle_private_message(bundle, command_loader, message,
                                              conn);

        conn.close();
      });

  client.run();
  stream_listener_client.run_thread();

  return 0;
}