diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-06 18:25:46 +0400 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-06 18:25:46 +0400 |
| commit | d1f8efef4a68f81ca104e9b8f51e04497f3bd8aa (patch) | |
| tree | ce13d90a87fdbe25c8871ff1f39322681cd520b4 | |
| parent | 2a49844a95593ac98e919c18651320e62f276fa7 (diff) | |
feat: !ping in lua + bot and time libraries
| -rw-r--r-- | bot/CMakeLists.txt | 7 | ||||
| -rw-r--r-- | bot/src/commands/command.cpp | 8 | ||||
| -rw-r--r-- | bot/src/commands/lua.cpp | 52 | ||||
| -rw-r--r-- | bot/src/commands/lua.hpp | 6 | ||||
| -rw-r--r-- | bot/src/modules/ping.hpp | 58 | ||||
| -rw-r--r-- | luamods/ping.lua | 15 |
6 files changed, 84 insertions, 62 deletions
diff --git a/bot/CMakeLists.txt b/bot/CMakeLists.txt index a01cc7f..51a14fc 100644 --- a/bot/CMakeLists.txt +++ b/bot/CMakeLists.txt @@ -15,6 +15,13 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug") target_compile_definitions(Bot PRIVATE DEBUG_MODE) endif() +string(TIMESTAMP PROJECT_COMPILE_TIME "%s") + +target_compile_definitions(Bot PRIVATE + BOT_VERSION="${PROJECT_VERSION}" + BOT_COMPILED_TIMESTAMP=${PROJECT_COMPILE_TIME} +) + set_target_properties( Bot PROPERTIES DESCRIPTION ${PROJECT_DESCRIPTION} diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp index 97dd88b..baf5a62 100644 --- a/bot/src/commands/command.cpp +++ b/bot/src/commands/command.cpp @@ -21,7 +21,6 @@ #include "../modules/massping.hpp" #include "../modules/mcsrv.hpp" #include "../modules/notify.hpp" -#include "../modules/ping.hpp" #include "../modules/settings.hpp" #include "../modules/spam.hpp" #include "../modules/timer.hpp" @@ -35,7 +34,6 @@ namespace bot { namespace command { CommandLoader::CommandLoader() { - this->add_command(std::make_unique<mod::Ping>()); this->add_command(std::make_unique<mod::Massping>()); this->add_command(std::make_unique<mod::Event>()); this->add_command(std::make_unique<mod::Notify>()); @@ -50,7 +48,11 @@ namespace bot { this->add_command(std::make_unique<mod::MinecraftServerCheck>()); this->luaState = std::make_shared<sol::state>(); - this->luaState->open_libraries(sol::lib::base, sol::lib::string); + this->luaState->open_libraries(sol::lib::base, sol::lib::string, + sol::lib::math); + + lua::library::add_bot_library(this->luaState); + lua::library::add_time_library(this->luaState); } void CommandLoader::load_lua_directory(const std::string &folder_path) { diff --git a/bot/src/commands/lua.cpp b/bot/src/commands/lua.cpp index cd7bc84..db89eff 100644 --- a/bot/src/commands/lua.cpp +++ b/bot/src/commands/lua.cpp @@ -7,8 +7,58 @@ #include "commands/request.hpp" #include "commands/response.hpp" #include "schemas/user.hpp" +#include "utils/chrono.hpp" +#include "utils/string.hpp" namespace bot::command::lua { + namespace library { + void add_bot_library(std::shared_ptr<sol::state> state) { + state->set_function("bot_get_compiler_version", []() { + std::string info; +#ifdef __cplusplus + info.append("C++" + std::to_string(__cplusplus).substr(2, 2)); +#endif +#ifdef __VERSION__ + info.append(" (gcc " + + bot::utils::string::split_text(__VERSION__, ' ')[0] + ")"); +#endif + return info; + }); + + state->set_function("bot_get_uptime", []() { + auto now = std::chrono::steady_clock::now(); + auto duration = now - START_TIME; + auto seconds = + std::chrono::duration_cast<std::chrono::seconds>(duration).count(); + return static_cast<long long>(seconds); + }); + + state->set_function("bot_get_memory_usage", []() { + struct rusage usage; + getrusage(RUSAGE_SELF, &usage); + return usage.ru_maxrss; + }); + + state->set_function("bot_get_compile_time", + []() { return BOT_COMPILED_TIMESTAMP; }); + + state->set_function("bot_get_version", []() { return BOT_VERSION; }); + } + + void add_time_library(std::shared_ptr<sol::state> state) { + state->set_function("time_current", []() { + return static_cast<long long>( + std::chrono::duration_cast<std::chrono::seconds>( + std::chrono::steady_clock::now().time_since_epoch()) + .count()); + }); + + state->set_function("time_humanize", [](const int ×tamp) { + return utils::chrono::format_timestamp(timestamp); + }); + } + } + LuaCommand::LuaCommand(std::shared_ptr<sol::state> luaState, const std::string &script) { this->luaState = luaState; @@ -40,7 +90,7 @@ namespace bot::command::lua { this->level = schemas::PermissionLevel::USER; } - this->handle = data["run"]; + this->handle = data["handle"]; } Response LuaCommand::run(const InstanceBundle &bundle, diff --git a/bot/src/commands/lua.hpp b/bot/src/commands/lua.hpp index 11e98d3..b858759 100644 --- a/bot/src/commands/lua.hpp +++ b/bot/src/commands/lua.hpp @@ -1,5 +1,6 @@ #pragma once +#include <memory> #include <sol/sol.hpp> #include <sol/state.hpp> #include <sol/table.hpp> @@ -14,6 +15,11 @@ void print_lua_object_type(const sol::object &obj); namespace bot::command::lua { + namespace library { + void add_bot_library(std::shared_ptr<sol::state> state); + void add_time_library(std::shared_ptr<sol::state> state); + } + class LuaCommand : public Command { public: LuaCommand(std::shared_ptr<sol::state> luaState, diff --git a/bot/src/modules/ping.hpp b/bot/src/modules/ping.hpp deleted file mode 100644 index cc982f6..0000000 --- a/bot/src/modules/ping.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include <sys/resource.h> -#include <sys/types.h> -#include <unistd.h> - -#include <chrono> -#include <string> -#include <vector> - -#include "../bundle.hpp" -#include "../commands/command.hpp" -#include "../utils/chrono.hpp" - -namespace bot { - namespace mod { - class Ping : public command::Command { - std::string get_name() const override { return "ping"; } - - command::Response run(const InstanceBundle &bundle, - const command::Request &request) const override { - auto now = std::chrono::steady_clock::now(); - auto duration = now - START_TIME; - auto seconds = - std::chrono::duration_cast<std::chrono::seconds>(duration); - std::string uptime = utils::chrono::format_timestamp(seconds.count()); - - struct rusage usage; - getrusage(RUSAGE_SELF, &usage); - - int used_memory = usage.ru_maxrss / 1024; - - std::string cpp_info; - -#ifdef __cplusplus - cpp_info.append("C++" + std::to_string(__cplusplus).substr(2, 2)); -#endif - -#ifdef __VERSION__ - cpp_info.append(" (gcc " + - bot::utils::string::split_text(__VERSION__, ' ')[0] + - ")"); -#endif - - if (!cpp_info.empty()) { - cpp_info.append(" · "); - } - - return command::Response( - bundle.localization - .get_formatted_line( - request, loc::LineId::PingResponse, - {cpp_info, uptime, std::to_string(used_memory)}) - .value()); - } - }; - } -} diff --git a/luamods/ping.lua b/luamods/ping.lua new file mode 100644 index 0000000..8e1578e --- /dev/null +++ b/luamods/ping.lua @@ -0,0 +1,15 @@ +return { + name = "ping", + delay_sec = 5, + options = {}, + subcommands = {}, + minimal_rights = "user", + handle = function(request) + return request.sender.alias_name .. ": funnywhitecat Pong! " .. + bot_get_compiler_version() .. + " · Uptime: " .. time_humanize(bot_get_uptime()) .. + " · Used memory: " .. math.ceil(bot_get_memory_usage() / 1024 / 1024) .. + "MB · Bot running on v" .. bot_get_version() .. + " (Last updated " .. time_humanize(time_current() - bot_get_compile_time()) .. " ago)" + end +} |
