Skip to content

Commit

Permalink
修复NIAHttpBOT无法正常接收http事件
Browse files Browse the repository at this point in the history
  • Loading branch information
NIANIANKNIA committed Jul 9, 2024
1 parent d17eae7 commit 34e211a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 102 deletions.
69 changes: 0 additions & 69 deletions NIAHttpBOT/src/CommandListener.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions NIAHttpBOT/src/CommandListener.h

This file was deleted.

71 changes: 69 additions & 2 deletions NIAHttpBOT/src/NIAHttpBOT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ If you have any problems with this project, please contact the authors.
#include <syncstream>
#include <cstdlib>
#include <cstdio>
#include <unordered_map>
#include <chrono>
#include <unordered_map>
#include <functional>
#include <sstream>

#ifdef WIN32 //only enable TLS in windows
#define CPPHTTPLIB_OPENSSL_SUPPORT
Expand All @@ -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"


//定义版本号
Expand Down Expand Up @@ -322,9 +326,72 @@ signed int main(signed int argc, char** argv) {
init_file_API(svr);

//监听终端命令输入
listenForCommands(argv[0]);
using CommandHandler = std::function<void(const std::vector<std::string>&)>;

std::unordered_map<std::string, CommandHandler> commandMap;

commandMap["help"] = [](const std::vector<std::string>&) {
std::cout << "可用指令列表:" << std::endl;
std::cout << " reload - 重启程序" << std::endl;
std::cout << " stop - 关闭程序" << std::endl;
std::cout << " setcfg <cfgname> <cfgdata> - 设置配置项" << std::endl;
};

std::string programName = argv[0];
commandMap["reload"] = [programName](const std::vector<std::string>&) {
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<std::string>&) {
INFO("1s后将关闭程序...");
std::this_thread::sleep_for(std::chrono::seconds(1));
exit(0);
};

commandMap["setcfg"] = [](const std::vector<std::string>& args) {
if (args.size() < 3) {
WARN("setcfg 指令需要两个参数: <cfgname> <cfgdata>");
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<std::string> 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;
}
10 changes: 5 additions & 5 deletions development_behavior_packs/NIA_V4.0_BP/scripts/qqBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
})



Expand Down

0 comments on commit 34e211a

Please sign in to comment.