blob: a7d768d8baf88c10c8c24a12ec28f8ef993bd205 (
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
|
#include "handlers.hpp"
#include <exception>
#include <optional>
#include <pqxx/pqxx>
#include <string>
#include <vector>
#include "bundle.hpp"
#include "commands/command.hpp"
#include "commands/request.hpp"
#include "commands/request_util.hpp"
#include "irc/message.hpp"
#include "localization/line_id.hpp"
namespace bot::handlers {
void handle_private_message(
const InstanceBundle &bundle,
const command::CommandLoader &command_loader,
const irc::Message<irc::MessageType::Privmsg> &message,
pqxx::connection &conn) {
std::optional<command::Request> request =
command::generate_request(command_loader, message, conn);
if (request.has_value()) {
try {
auto response = command_loader.run(bundle, request.value());
if (response.has_value()) {
try {
auto str = std::get<std::string>(*response);
bundle.irc_client.say(message.source.login, str);
} catch (const std::exception &e) {
}
try {
auto strs = std::get<std::vector<std::string>>(*response);
for (const std::string &str : strs) {
bundle.irc_client.say(message.source.login, str);
}
} catch (const std::exception &e) {
}
}
} catch (const std::exception &e) {
std::string line =
bundle.localization
.get_formatted_line(request.value(), loc::LineId::ErrorTemplate,
{e.what()})
.value();
bundle.irc_client.say(message.source.login, line);
}
}
pqxx::work work(conn);
pqxx::result channels =
work.exec("SELECT id FROM channels WHERE alias_id = " +
std::to_string(message.source.id));
if (!channels.empty()) {
int channel_id = channels[0][0].as<int>();
pqxx::result cmds =
work.exec("SELECT message FROM custom_commands WHERE name = '" +
message.message + "' AND channel_id = '" +
std::to_string(channel_id) + "'");
if (!cmds.empty()) {
std::string msg = cmds[0][0].as<std::string>();
bundle.irc_client.say(message.source.login, msg);
}
}
work.commit();
}
}
|