Skip to content

Commit

Permalink
QQBOT新增违禁词系统
Browse files Browse the repository at this point in the history
  • Loading branch information
NIANIANKNIA committed Apr 12, 2024
1 parent 019358f commit 96e266c
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
29 changes: 29 additions & 0 deletions NIAHttpBOT/src/NIAHttpBOT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
64 changes: 63 additions & 1 deletion NIAHttpBOT/src/QQBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,38 @@ void initialize() {
}


std::vector<std::string> 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)
{
Expand All @@ -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 查看!");
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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] == '#') {
//判断#后直到空格的字符串
Expand Down
3 changes: 3 additions & 0 deletions NIAHttpBOT/src/QQBot.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef QQBOT_H
#define QQBOT_H

#include <vector>
#include <string>
#include <algorithm>
#include <httplib.h>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
Expand Down
21 changes: 21 additions & 0 deletions NIAHttpBOT/src/QQBot_API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <delete_msg()> 后,返回的数据:") + 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 <delete_msg()> 后,返回的数据无法解析:") + res->body);
return false;
}
}
else {
WARN(X("调用API <delete_msg()> 超时,请检查连接!"));
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");
Expand Down
8 changes: 8 additions & 0 deletions NIAHttpBOT/src/QQBot_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 发送点赞
Expand Down

0 comments on commit 96e266c

Please sign in to comment.