diff --git a/NIAHttpBOT/src/NIAHttpBOT.cpp b/NIAHttpBOT/src/NIAHttpBOT.cpp index 6f0506d..6c9eaf4 100644 --- a/NIAHttpBOT/src/NIAHttpBOT.cpp +++ b/NIAHttpBOT/src/NIAHttpBOT.cpp @@ -178,7 +178,36 @@ signed int main(signed int argc, char** argv) { //初始化文件API init_file_API(svr); + //监听reload命令 + std::thread inputThread([&]() { + std::string command; + bool hasCommand = false; + while (std::cin >> command) { + //重载指令 + if (command == "reload") { + hasCommand = true; + INFO("1s后重启NiaHttp-BOT..."); + std::this_thread::sleep_for(std::chrono::seconds(1)); + std::system((std::string(argv[0]) + " &").c_str()); + exit(0); + } + //停止指令 + if (command == "stop") { + hasCommand = true; + INFO("1s后将关闭NiaHttp-BOT..."); + std::this_thread::sleep_for(std::chrono::seconds(1)); + exit(0); + } + if (!hasCommand) { + WARN("未知指令,请检查后再次输入!"); + } + } + }); + + svr.listen(IPAddress, ServerPort); + inputThread.join(); + return 0; } diff --git a/NIAHttpBOT/src/QQBot.cpp b/NIAHttpBOT/src/QQBot.cpp index c4dc9eb..a408b5e 100644 --- a/NIAHttpBOT/src/QQBot.cpp +++ b/NIAHttpBOT/src/QQBot.cpp @@ -30,6 +30,38 @@ void initialize() { } +std::vector forbiddenWords; + +//读取违禁词列表 +void loadForbiddenWords(const std::string& filename) { + std::ifstream file(filename); + //如果文件没有就创建一个,并写入默认的违禁词 + if (!file) { + std::ofstream file(filename); + file << "test1\n"; + file << "test2\n"; + file << "test3\n"; + file.close(); + //向控制台输出警告 + WARN("违禁词文件不存在,已自动创建!请编辑违禁词文件!文件路径为:" + filename); + } + //文件打开成功,读取文件内容 + std::string word; + while (std::getline(file, word)) { + forbiddenWords.push_back(word); + } +} + +bool containsForbiddenWords(const std::string& input) { + for (const auto& word : forbiddenWords) { + if (input.find(word) != std::string::npos) { + return true; + } + } + return false; +} + + void main_qqbot(httplib::Server &svr) { @@ -45,6 +77,7 @@ void main_qqbot(httplib::Server &svr) if (get_status_res.status && get_status_res.good && get_status_res.online) { INFO("已成功连接到QQ机器人!"); qqbot->send_group_message(QQGroup, "NIAHttpBOT已成功连接到QQ机器人!"); + loadForbiddenWords("ForbiddenWords.txt"); } else { WARN("QQ机器人连接失败!请检查QQ机器人是否已启动&&配置是否正确!"); WARN("如需更多帮助请前往 https://docs.mcnia.com/dev/Http-Bot.html 查看!"); @@ -103,7 +136,7 @@ void main_qqbot(httplib::Server &svr) //接收QQ消息事件 svr.Post(Locate, [](const httplib::Request& req, httplib::Response& res) { - INFO(X("NiaHttp-BOT 客户端接收的数据为 ") << req.body); + //INFO(X("NiaHttp-BOT 客户端接收的数据为 ") << req.body); //解析字符串并创建一个json对象 rapidjson::Document qqEventData; qqEventData.Parse(req.body.c_str()); @@ -146,6 +179,35 @@ void main_qqbot(httplib::Server &svr) return ; } + //判断消息是否包含违禁词 + if (containsForbiddenWords(message)) { + //看发送者是否为管理员或者群主 + std::string user_permission = ""; + user_permission = qqEventData["sender"]["role"].GetString(); + if (user_permission == "admin" || user_permission == "owner" || sender_qq == OwnerQQ) { + //发送警告消息 + qqbot->send_group_message(group_id, "请注意发言规范!详情见 https://docs.mcnia.com/play-guide/regulation.html"); + //提示由于您是管理员或者群主,所以不会被禁言 + qqbot->send_group_message(group_id, "但由于您是管理员或者群主,所以不会被禁言!"); + return ; + } + //发送警告消息 + qqbot->send_group_message(group_id, "请注意发言规范!详情见 https://docs.mcnia.com/play-guide/regulation.html"); + //撤回消息 + auto delete_msg_res = qqbot->delete_msg(qqEventData["message_id"].GetInt()); + //判断撤回消息是否成功 + if (delete_msg_res) { + //向主人qq私聊发送消息,类似qq[123456] 在 qq群[123456] 发送了违禁消息:[违禁消息内容],已成功撤回! + qqbot->send_private_message(OwnerQQ, "qq[" + sender_qq + "] 在 群[" + group_id + "] 发送了违禁消息:" + message + ",已成功撤回!"); + } else { + //向主人qq私聊发送消息,类似qq[123456] 在 qq群[123456] 发送了违禁消息:[违禁消息内容],撤回失败! + qqbot->send_private_message(OwnerQQ, "qq[" + sender_qq + "] 在 群[" + group_id + "] 发送了违禁消息:" + message + ",撤回失败,请手动撤回!"); + } + //禁言发送消息 + qqbot->set_group_ban(group_id, sender_qq, 60); + return ; + } + //判断消息是否以#开头 if (message[0] == '#') { //判断#后直到空格的字符串 diff --git a/NIAHttpBOT/src/QQBot.h b/NIAHttpBOT/src/QQBot.h index 885d01a..15508fa 100644 --- a/NIAHttpBOT/src/QQBot.h +++ b/NIAHttpBOT/src/QQBot.h @@ -1,6 +1,9 @@ #ifndef QQBOT_H #define QQBOT_H +#include +#include +#include #include #include #include diff --git a/NIAHttpBOT/src/QQBot_API.cpp b/NIAHttpBOT/src/QQBot_API.cpp index 955e06b..79ab210 100644 --- a/NIAHttpBOT/src/QQBot_API.cpp +++ b/NIAHttpBOT/src/QQBot_API.cpp @@ -27,6 +27,27 @@ void QQBot::send_group_message(const std::string & group_id, const std::string & } } +bool QQBot::delete_msg(const int32_t &message_id) +{ + auto res = cli.Post("/delete_msg", "{\"message_id\":" + std::to_string(message_id) + "}", "application/json"); + if (res) { + INFO(X("调用API 后,返回的数据:") + res->body); + rapidjson::Document doc; + doc.Parse(res->body.c_str()); + if (doc.HasMember("status") && std::string(doc["status"].GetString()) == "ok") { + return true; + } + else { + WARN(X("调用API 后,返回的数据无法解析:") + res->body); + return false; + } + } + else { + WARN(X("调用API 超时,请检查连接!")); + return false; + } + return false; +} void QQBot::send_like(const std::string & user_id, int times) { auto res = cli.Post("/send_like", "{\"user_id\":\"" + user_id + "\",\"times\":" + std::to_string(times) + "}", "application/json"); diff --git a/NIAHttpBOT/src/QQBot_API.h b/NIAHttpBOT/src/QQBot_API.h index 683eb8b..b282ae9 100644 --- a/NIAHttpBOT/src/QQBot_API.h +++ b/NIAHttpBOT/src/QQBot_API.h @@ -33,6 +33,14 @@ class QQBot { */ void send_group_message(const std::string & group_id, const std::string & message, bool auto_escape = false); + /** + * @brief 撤回消息 + * + * @param message_id 讨论组ID + * @return 是否撤回成功 + */ + bool delete_msg(const int32_t & message_id); + /** * @brief 发送点赞