diff --git a/NIAHttpBOT/src/CommandListener.cpp b/NIAHttpBOT/src/CommandListener.cpp deleted file mode 100644 index c295438..0000000 --- a/NIAHttpBOT/src/CommandListener.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "CommandListener.h" - - -void listenForCommands(const char* programName) { - - using CommandHandler = std::function&)>; - - std::unordered_map commandMap; - - commandMap["help"] = [](const std::vector&) { - std::cout << "可用指令列表:" << std::endl; - std::cout << " reload - 重启程序" << std::endl; - std::cout << " stop - 关闭程序" << std::endl; - std::cout << " setcfg - 设置配置项" << std::endl; - }; - - commandMap["reload"] = [programName](const std::vector&) { - INFO("1s后重启程序..." ); - std::this_thread::sleep_for(std::chrono::seconds(1)); - #ifdef _WIN32 - std::system(("start cmd /k " + std::string(programName)).c_str()); - #else - if (fork() == 0) { - execl(programName, programName, (char*)NULL); - } - #endif - exit(0); - }; - - commandMap["stop"] = [](const std::vector&) { - INFO("1s后将关闭程序..."); - std::this_thread::sleep_for(std::chrono::seconds(1)); - exit(0); - }; - - commandMap["setcfg"] = [](const std::vector& args) { - if (args.size() < 3) { - WARN("setcfg 指令需要两个参数: "); - return; - } - std::string cfgname = args[1]; - std::string cfgdata = args[2]; - }; - - // 启动输入监听线程 - std::thread inputThread([&commandMap]() { - std::string line; - while (std::getline(std::cin, line)) { - std::istringstream iss(line); - std::vector tokens; - std::string token; - while (iss >> token) { - tokens.push_back(token); - } - if (tokens.empty()) { - continue; - } - auto it = commandMap.find(tokens[0]); - if (it != commandMap.end()) { - it->second(tokens); // 调用对应的处理函数 - } else { - std::cout << "未知指令,请检查后再次输入!" << std::endl; - } - } - }); - - // 等待输入线程完成 - inputThread.join(); -} \ No newline at end of file diff --git a/NIAHttpBOT/src/CommandListener.h b/NIAHttpBOT/src/CommandListener.h deleted file mode 100644 index 01f4afc..0000000 --- a/NIAHttpBOT/src/CommandListener.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef COMMANDLISTENER_H -#define COMMANDLISTENER_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include "Logger.hpp" - -#ifdef _WIN32 -#include -#else -#include -#endif - - - - -// 声明命令监听函数 -void listenForCommands(const char* programName); - -#endif \ No newline at end of file diff --git a/NIAHttpBOT/src/NIAHttpBOT.cpp b/NIAHttpBOT/src/NIAHttpBOT.cpp index bdfe149..b73cabc 100644 --- a/NIAHttpBOT/src/NIAHttpBOT.cpp +++ b/NIAHttpBOT/src/NIAHttpBOT.cpp @@ -26,6 +26,11 @@ If you have any problems with this project, please contact the authors. #include #include #include +#include +#include +#include +#include +#include #ifdef WIN32 //only enable TLS in windows #define CPPHTTPLIB_OPENSSL_SUPPORT @@ -46,7 +51,6 @@ If you have any problems with this project, please contact the authors. #include "QQBot.h" #include "File_API.h" #include "Game_API.h" -#include "CommandListener.h" //定义版本号 @@ -322,9 +326,72 @@ signed int main(signed int argc, char** argv) { init_file_API(svr); //监听终端命令输入 - listenForCommands(argv[0]); + using CommandHandler = std::function&)>; + + std::unordered_map commandMap; + + commandMap["help"] = [](const std::vector&) { + std::cout << "可用指令列表:" << std::endl; + std::cout << " reload - 重启程序" << std::endl; + std::cout << " stop - 关闭程序" << std::endl; + std::cout << " setcfg - 设置配置项" << std::endl; + }; + + std::string programName = argv[0]; + commandMap["reload"] = [programName](const std::vector&) { + INFO("1s后重启程序..." ); + std::this_thread::sleep_for(std::chrono::seconds(1)); + #ifdef _WIN32 + std::system(("start cmd /k " + std::string(programName)).c_str()); + #else + if (fork() == 0) { + execl(programName, programName, (char*)NULL); + } + #endif + exit(0); + }; + + commandMap["stop"] = [](const std::vector&) { + INFO("1s后将关闭程序..."); + std::this_thread::sleep_for(std::chrono::seconds(1)); + exit(0); + }; + + commandMap["setcfg"] = [](const std::vector& args) { + if (args.size() < 3) { + WARN("setcfg 指令需要两个参数: "); + return; + } + std::string cfgname = args[1]; + std::string cfgdata = args[2]; + }; + + // 启动输入监听线程 + std::thread inputThread([&commandMap]() { + std::string line; + while (std::getline(std::cin, line)) { + std::istringstream iss(line); + std::vector tokens; + std::string token; + while (iss >> token) { + tokens.push_back(token); + } + if (tokens.empty()) { + continue; + } + auto it = commandMap.find(tokens[0]); + if (it != commandMap.end()) { + it->second(tokens); // 调用对应的处理函数 + } else { + std::cout << "未知指令,请检查后再次输入!" << std::endl; + } + } + }); svr.listen(IPAddress, ServerPort); + // 等待输入线程完成 + inputThread.join(); + return 0; } diff --git a/development_behavior_packs/NIA_V4.0_BP/scripts/qqBot.js b/development_behavior_packs/NIA_V4.0_BP/scripts/qqBot.js index d5a00d0..33657dc 100644 --- a/development_behavior_packs/NIA_V4.0_BP/scripts/qqBot.js +++ b/development_behavior_packs/NIA_V4.0_BP/scripts/qqBot.js @@ -14,11 +14,11 @@ const fs = new ExternalFS(); console.log(`\x1b[33m[\x1b[36mNiaServer-Core\x1b[33m] QQBot has been successfully started on this server!\x1b[0m\n`); -// world.afterEvents.worldInitialize.subscribe((event) => { -// bot.send_group_msg(`服务器已与机器人成功连接!\nBDS当前版本:${BDS_VERSION}\nCore版本:${VERSION}\n上次更新时间:${LAST_UPGRATE}`,"724360499").then((result) => { -// console.log(result); -// }) -// }) +world.afterEvents.worldInitialize.subscribe((event) => { + bot.send_group_msg(`服务器已与机器人成功连接!\nBDS当前版本:${BDS_VERSION}\nCore版本:${VERSION}\n上次更新时间:${LAST_UPGRATE}`,"724360499").then((result) => { + console.log(result); + }) +})