summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-04-30 21:37:17 +0500
committerilotterytea <iltsu@alright.party>2024-04-30 21:37:17 +0500
commitdbbcaf84e9a86f0381dff83c1c1a1e32ae4856c0 (patch)
tree136359667881c3e852d6674f2a5fdc7089730c76 /src
parent6d74bb52954156d6324d27455712e0d008ab938a (diff)
feat: Channel in Request struct
Diffstat (limited to 'src')
-rw-r--r--src/commands/request.hpp4
-rw-r--r--src/commands/request_util.cpp34
2 files changed, 36 insertions, 2 deletions
diff --git a/src/commands/request.hpp b/src/commands/request.hpp
index efa0655..6149dee 100644
--- a/src/commands/request.hpp
+++ b/src/commands/request.hpp
@@ -5,6 +5,7 @@
#include <string>
#include "../irc/message.hpp"
+#include "../schemas/channel.hpp"
namespace bot::command {
struct Request {
@@ -12,6 +13,9 @@ namespace bot::command {
std::optional<std::string> subcommand_id;
std::optional<std::string> message;
const irc::Message<irc::MessageType::Privmsg> &irc_message;
+
+ schemas::Channel channel;
+
pqxx::connection &conn;
};
}
diff --git a/src/commands/request_util.cpp b/src/commands/request_util.cpp
index 08e958a..a0965c3 100644
--- a/src/commands/request_util.cpp
+++ b/src/commands/request_util.cpp
@@ -2,9 +2,11 @@
#include <optional>
#include <pqxx/pqxx>
+#include <string>
#include "../constants.hpp"
#include "../irc/message.hpp"
+#include "../schemas/channel.hpp"
#include "command.hpp"
#include "request.hpp"
@@ -13,6 +15,10 @@ namespace bot::command {
const command::CommandLoader &command_loader,
const irc::Message<irc::MessageType::Privmsg> &irc_message,
pqxx::connection &conn) {
+ pqxx::work *work;
+
+ work = new pqxx::work(conn);
+
std::vector<std::string> parts =
utils::string::split_text(irc_message.message, ' ');
@@ -36,8 +42,32 @@ namespace bot::command {
parts.erase(parts.begin());
+ pqxx::result channel_query =
+ work->exec("SELECT * FROM channels WHERE alias_id = " +
+ std::to_string(irc_message.source.id));
+
+ // Create new channel data in the database if it didn't exist b4
+ if (channel_query.empty()) {
+ work->exec("INSERT INTO channels (alias_id, alias_name) VALUES (" +
+ std::to_string(irc_message.source.id) + ", '" +
+ irc_message.source.login + "')");
+
+ work->commit();
+
+ delete work;
+ work = new pqxx::work(conn);
+
+ channel_query = work->exec("SELECT * FROM channels WHERE alias_id = " +
+ std::to_string(irc_message.source.id));
+ }
+
+ schemas::Channel channel(channel_query[0]);
+
+ delete work;
+
if (parts.empty()) {
- Request req{command_id, std::nullopt, std::nullopt, irc_message, work};
+ Request req{command_id, std::nullopt, std::nullopt,
+ irc_message, channel, conn};
return req;
}
@@ -54,7 +84,7 @@ namespace bot::command {
message = std::nullopt;
}
- Request req{command_id, subcommand_id, message, irc_message, work};
+ Request req{command_id, subcommand_id, message, irc_message, channel, conn};
return req;
}
}