summaryrefslogtreecommitdiff
path: root/bot
diff options
context:
space:
mode:
Diffstat (limited to 'bot')
-rw-r--r--bot/src/commands/command.cpp5
-rw-r--r--bot/src/commands/lua.hpp63
-rw-r--r--bot/src/modules/lua.hpp73
3 files changed, 65 insertions, 76 deletions
diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp
index 417746c..2ecb80f 100644
--- a/bot/src/commands/command.cpp
+++ b/bot/src/commands/command.cpp
@@ -15,15 +15,14 @@
#include "../bundle.hpp"
#include "../utils/chrono.hpp"
#include "commands/lua.hpp"
-#include "modules/lua.hpp"
#include "request.hpp"
#include "response.hpp"
namespace bot {
namespace command {
CommandLoader::CommandLoader() {
- this->add_command(std::make_unique<mod::LuaExecution>());
- this->add_command(std::make_unique<mod::LuaRemoteExecution>());
+ this->add_command(std::make_unique<lua::mod::LuaExecution>());
+ this->add_command(std::make_unique<lua::mod::LuaRemoteExecution>());
this->luaState = std::make_shared<sol::state>();
this->luaState->open_libraries(sol::lib::base, sol::lib::string,
diff --git a/bot/src/commands/lua.hpp b/bot/src/commands/lua.hpp
index bf48ab6..3d514f3 100644
--- a/bot/src/commands/lua.hpp
+++ b/bot/src/commands/lua.hpp
@@ -11,7 +11,9 @@
#include "bundle.hpp"
#include "commands/command.hpp"
#include "commands/response.hpp"
+#include "commands/response_error.hpp"
#include "config.hpp"
+#include "cpr/api.h"
#include "schemas/user.hpp"
void print_lua_object_type(const sol::object &obj);
@@ -72,4 +74,65 @@ namespace bot::command::lua {
std::shared_ptr<sol::state> luaState;
};
+
+ namespace mod {
+ class LuaExecution : public command::Command {
+ std::string get_name() const override { return "lua"; }
+
+ int get_delay_seconds() const override { return 1; }
+
+ command::Response run(const InstanceBundle &bundle,
+ const command::Request &request) const override {
+ if (!request.message.has_value()) {
+ throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>(
+ request, bundle.localization, command::CommandArgument::VALUE);
+ }
+
+ std::string script = request.message.value();
+
+ return command::lua::run_safe_lua_script(request, bundle, script);
+ }
+ };
+
+ class LuaRemoteExecution : public command::Command {
+ std::string get_name() const override { return "luaimport"; }
+
+ int get_delay_seconds() const override { return 2; }
+
+ command::Response run(const InstanceBundle &bundle,
+ const command::Request &request) const override {
+ if (!request.message.has_value()) {
+ throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>(
+ request, bundle.localization, command::CommandArgument::VALUE);
+ }
+
+ std::string url = request.message.value();
+
+ std::vector<std::string> mimeTypes = {"text/plain", "text/x-lua"};
+
+ cpr::Response response = cpr::Get(
+ cpr::Url{url},
+ cpr::Header{
+ {"Accept", utils::string::join_vector(mimeTypes, ',')},
+ {"User-Agent", "https://github.com/ilotterytea/bot"}});
+
+ if (response.status_code != 200) {
+ throw ResponseException<ResponseError::EXTERNAL_API_ERROR>(
+ request, bundle.localization, response.status_code);
+ }
+
+ std::string contentType = response.header["Content-Type"];
+ if (!std::any_of(
+ mimeTypes.begin(), mimeTypes.end(),
+ [&contentType](const auto &x) { return x == contentType; })) {
+ throw ResponseException<ResponseError::INCORRECT_ARGUMENT>(
+ request, bundle.localization, url);
+ }
+
+ std::string script = response.text;
+
+ return command::lua::run_safe_lua_script(request, bundle, script);
+ }
+ };
+ }
} \ No newline at end of file
diff --git a/bot/src/modules/lua.hpp b/bot/src/modules/lua.hpp
deleted file mode 100644
index 1f4b665..0000000
--- a/bot/src/modules/lua.hpp
+++ /dev/null
@@ -1,73 +0,0 @@
-#pragma once
-
-#include <algorithm>
-#include <optional>
-#include <string>
-#include <vector>
-
-#include "bundle.hpp"
-#include "commands/command.hpp"
-#include "commands/lua.hpp"
-#include "commands/response_error.hpp"
-#include "cpr/api.h"
-#include "utils/string.hpp"
-
-namespace bot::mod {
- class LuaExecution : public command::Command {
- std::string get_name() const override { return "lua"; }
-
- int get_delay_seconds() const override { return 1; }
-
- command::Response run(const InstanceBundle &bundle,
- const command::Request &request) const override {
- if (!request.message.has_value()) {
- throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>(
- request, bundle.localization, command::CommandArgument::VALUE);
- }
-
- std::string script = request.message.value();
-
- return command::lua::run_safe_lua_script(request, bundle, script);
- }
- };
-
- class LuaRemoteExecution : public command::Command {
- std::string get_name() const override { return "luaimport"; }
-
- int get_delay_seconds() const override { return 2; }
-
- command::Response run(const InstanceBundle &bundle,
- const command::Request &request) const override {
- if (!request.message.has_value()) {
- throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>(
- request, bundle.localization, command::CommandArgument::VALUE);
- }
-
- std::string url = request.message.value();
-
- std::vector<std::string> mimeTypes = {"text/plain", "text/x-lua"};
-
- cpr::Response response = cpr::Get(
- cpr::Url{url},
- cpr::Header{{"Accept", utils::string::join_vector(mimeTypes, ',')},
- {"User-Agent", "https://github.com/ilotterytea/bot"}});
-
- if (response.status_code != 200) {
- throw ResponseException<ResponseError::EXTERNAL_API_ERROR>(
- request, bundle.localization, response.status_code);
- }
-
- std::string contentType = response.header["Content-Type"];
- if (!std::any_of(
- mimeTypes.begin(), mimeTypes.end(),
- [&contentType](const auto &x) { return x == contentType; })) {
- throw ResponseException<ResponseError::INCORRECT_ARGUMENT>(
- request, bundle.localization, url);
- }
-
- std::string script = response.text;
-
- return command::lua::run_safe_lua_script(request, bundle, script);
- }
- };
-} \ No newline at end of file