feat: 支持禁用某人或某群

This commit is contained in:
Rock Chin 2023-01-07 16:50:34 +08:00
parent 0afc2d5903
commit bf7487fafe
6 changed files with 54 additions and 13 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ __pycache__/
database.db
qchatgpt.log
config.py
banlist.py

11
banlist-template.py Normal file
View File

@ -0,0 +1,11 @@
# 禁用列表
# person为个人其中的QQ号会被禁止与机器人进行私聊或群聊交互
# 示例: person = [2854196310, 1234567890, 9876543210]
# group为群组其中的群号会被禁止与机器人进行交互
# 示例: group = [123456789, 987654321, 1234567890]
# 是否启用禁用列表
enable = True
person = [2854196310] # 2854196310是Q群管家机器人的QQ号默认屏蔽以免出现循环
group = [204785790] # 204785790是本项目交流群的群号默认屏蔽避免在交流群测试机器人

View File

@ -221,6 +221,10 @@ if __name__ == '__main__':
print('请先在config.py中填写配置')
sys.exit(0)
# 检查是否有banlist.py,如果没有就把banlist-template.py复制一份
if not os.path.exists('banlist.py'):
shutil.copy('banlist-template.py', 'banlist.py')
if len(sys.argv) > 1 and sys.argv[1] == 'init_db':
init_db()
sys.exit(0)

View File

@ -55,11 +55,24 @@ class QQBotManager:
reply_filter = None
enable_banlist = False
ban_person = []
ban_group = []
def __init__(self, mirai_http_api_config: dict, timeout: int = 60, retry: int = 3, first_time_init=True):
self.timeout = timeout
self.retry = retry
# 加载禁用列表
if os.path.exists("banlist.py"):
import banlist
self.enable_banlist = banlist.enable
self.ban_person = banlist.person
self.ban_group = banlist.group
logging.info("加载禁用列表: person: {}, group: {}".format(self.ban_person, self.ban_group))
config = pkg.utils.context.get_config()
if os.path.exists("sensitive.json") \
and config.sensitive_word_filter is not None \

View File

@ -100,12 +100,21 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
reply = []
session_name = "{}_{}".format(launcher_type, launcher_id)
# 检查是否被禁言
if launcher_type == 'group':
result = mgr.bot.member_info(target=launcher_id, member_id=mgr.bot.qq).get()
result = asyncio.run(result)
if result.mute_time_remaining > 0:
logging.info("机器人被禁言,跳过消息处理(group_{},剩余{}s)".format(launcher_id,
# 检查发送方是否被禁用
if pkg.utils.context.get_qqbot_manager().enable_banlist:
if sender_id in pkg.utils.context.get_qqbot_manager().ban_person:
logging.info("根据禁用列表忽略用户{}的消息".format(sender_id))
return []
if launcher_type == 'group' and launcher_id in pkg.utils.context.get_qqbot_manager().ban_group:
logging.info("根据禁用列表忽略群{}的消息".format(launcher_id))
return []
# 检查是否被禁言
if launcher_type == 'group':
result = mgr.bot.member_info(target=launcher_id, member_id=mgr.bot.qq).get()
result = asyncio.run(result)
if result.mute_time_remaining > 0:
logging.info("机器人被禁言,跳过消息处理(group_{},剩余{}s)".format(launcher_id,
result.mute_time_remaining))
return reply
@ -229,11 +238,12 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
api_keys = pkg.utils.context.get_openai_manager().key_mgr.api_key
for key_name in api_keys:
text_length = pkg.utils.context.get_openai_manager().audit_mgr\
text_length = pkg.utils.context.get_openai_manager().audit_mgr \
.get_text_length_of_key(api_keys[key_name])
image_count = pkg.utils.context.get_openai_manager().audit_mgr\
image_count = pkg.utils.context.get_openai_manager().audit_mgr \
.get_image_count_of_key(api_keys[key_name])
reply_str += "{}:\n - 文本长度:{}\n - 图片数量:{}\n".format(key_name, int(text_length), int(image_count))
reply_str += "{}:\n - 文本长度:{}\n - 图片数量:{}\n".format(key_name, int(text_length),
int(image_count))
reply = [reply_str]
elif cmd == 'draw':
@ -286,7 +296,7 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
params = [config_item] + params
reply = config_operation("cfg", params)
else:
reply = ["[bot]err:未知的指令或权限不足: "+cmd]
reply = ["[bot]err:未知的指令或权限不足: " + cmd]
except Exception as e:
mgr.notify_admin("{}指令执行失败:{}".format(session_name, e))
logging.exception(e)
@ -313,8 +323,9 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
switched, name = pkg.utils.context.get_openai_manager().key_mgr.auto_switch()
if not switched:
mgr.notify_admin("API调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format(
current_tokens_amt))
mgr.notify_admin(
"API调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format(
current_tokens_amt))
reply = ["[bot]err:API调用额度超额请联系作者或等待修复"]
else:
openai.api_key = pkg.utils.context.get_openai_manager().key_mgr.get_using_key()

View File

@ -31,6 +31,7 @@ def reload_all(notify=True):
walk(pkg)
importlib.reload(__import__('config'))
importlib.reload(__import__('main'))
importlib.reload(__import__('banlist'))
pkg.utils.context.context = context
# 执行启动流程