summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-04-21 03:25:12 +0500
committerilotterytea <iltsu@alright.party>2024-04-21 03:25:12 +0500
commitb4fca15aec26332c1315c8c3030169bbf342da3e (patch)
treef2529eeb93f20520f378049dce90173f2f42ce74
parent59084cf514386726df20c3b8e334f4f77685b97e (diff)
feat: command loader
-rw-r--r--src/commands/command.cpp30
-rw-r--r--src/commands/command.hpp18
-rw-r--r--src/main.cpp2
-rw-r--r--src/modules/ping.hpp20
4 files changed, 70 insertions, 0 deletions
diff --git a/src/commands/command.cpp b/src/commands/command.cpp
new file mode 100644
index 0000000..b6852cc
--- /dev/null
+++ b/src/commands/command.cpp
@@ -0,0 +1,30 @@
+#include "command.hpp"
+
+#include <iostream>
+#include <memory>
+#include <optional>
+
+#include "../modules/ping.hpp"
+
+namespace bot {
+ namespace command {
+ CommandLoader::CommandLoader() {
+ this->add_command(std::make_unique<mod::Ping>());
+ }
+
+ void CommandLoader::add_command(std::unique_ptr<Command> command) {
+ this->commands.push_back(std::move(command));
+ }
+
+ std::optional<std::variant<std::vector<std::string>, std::string>>
+ CommandLoader::run(const irc::Message<irc::MessageType::Privmsg> &msg) {
+ for (const std::unique_ptr<Command> &command : this->commands) {
+ if (command->get_name() == msg.message) {
+ return command->run(msg);
+ }
+ }
+
+ return std::nullopt;
+ }
+ }
+}
diff --git a/src/commands/command.hpp b/src/commands/command.hpp
index 6e05779..6efe505 100644
--- a/src/commands/command.hpp
+++ b/src/commands/command.hpp
@@ -1,3 +1,7 @@
+#pragma once
+
+#include <memory>
+#include <optional>
#include <string>
#include <variant>
#include <vector>
@@ -7,9 +11,23 @@
namespace bot {
namespace command {
class Command {
+ public:
virtual std::string get_name() = 0;
virtual std::variant<std::vector<std::string>, std::string> run(
const irc::Message<irc::MessageType::Privmsg> &msg) = 0;
};
+
+ class CommandLoader {
+ public:
+ CommandLoader();
+ ~CommandLoader() = default;
+
+ void add_command(std::unique_ptr<Command> cmd);
+ std::optional<std::variant<std::vector<std::string>, std::string>> run(
+ const irc::Message<irc::MessageType::Privmsg> &msg);
+
+ private:
+ std::vector<std::unique_ptr<Command>> commands;
+ };
}
}
diff --git a/src/main.cpp b/src/main.cpp
index 129b074..b437708 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,6 +1,7 @@
#include <iostream>
#include <optional>
+#include "commands/command.hpp"
#include "config.hpp"
#include "irc/client.hpp"
@@ -22,6 +23,7 @@ int main(int argc, char *argv[]) {
}
bot::irc::Client client(cfg.bot_username, cfg.bot_password);
+ bot::command::CommandLoader command_loader;
client.run();
diff --git a/src/modules/ping.hpp b/src/modules/ping.hpp
new file mode 100644
index 0000000..cf59d66
--- /dev/null
+++ b/src/modules/ping.hpp
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "../commands/command.hpp"
+
+namespace bot {
+ namespace mod {
+ class Ping : public command::Command {
+ std::string get_name() override { return "ping"; }
+
+ std::variant<std::vector<std::string>, std::string> run(
+ const irc::Message<irc::MessageType::Privmsg> &msg) override {
+ return "pong";
+ }
+ };
+ }
+}