Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/bots/aiocqhttp/bot.py

141 lines
5.5 KiB
Python
Raw Normal View History

2022-06-12 07:07:53 +00:00
import logging
2021-10-08 11:54:27 +00:00
import os
2021-11-05 14:08:51 +00:00
import re
2021-10-08 11:54:27 +00:00
2021-11-12 14:25:53 +00:00
from aiocqhttp import Event
2022-06-12 07:07:53 +00:00
from bots.aiocqhttp.client import bot
from bots.aiocqhttp.message import MessageSession, FetchTarget
2021-10-15 17:52:40 +00:00
from config import Config
2023-02-05 14:33:33 +00:00
from core.builtins import EnableDirtyWordCheck, PrivateAssets, Url
2021-10-08 11:54:27 +00:00
from core.parser.message import parser
2023-02-05 14:33:33 +00:00
from core.types import MsgInfo, Session
2023-03-01 13:41:42 +00:00
from core.utils.bot import load_prompt, init_async
from database import BotDBUtil
2022-06-27 17:00:00 +00:00
2021-10-08 11:54:27 +00:00
PrivateAssets.set(os.path.abspath(os.path.dirname(__file__) + '/assets'))
2022-06-28 10:14:44 +00:00
EnableDirtyWordCheck.status = Config('qq_enable_dirty_check')
Url.disable_mm = not Config('qq_enable_urlmanager')
2022-07-29 10:55:20 +00:00
qq_account = Config("qq_account")
2021-10-08 11:54:27 +00:00
@bot.on_startup
async def startup():
2023-03-01 13:41:42 +00:00
await init_async()
2021-10-09 13:08:18 +00:00
bot.logger.setLevel(logging.WARNING)
2022-01-13 15:50:17 +00:00
@bot.on_websocket_connection
async def _(event: Event):
2021-10-08 11:54:27 +00:00
await load_prompt(FetchTarget)
2021-11-16 14:19:48 +00:00
@bot.on_message('group', 'private')
2021-10-08 11:54:27 +00:00
async def _(event: Event):
2021-11-25 15:40:30 +00:00
if event.detail_type == 'private':
if event.sub_type == 'group':
return await bot.send(event, '请先添加好友后再进行命令交互。')
2021-11-06 12:59:35 +00:00
filter_msg = re.match(r'.*?\[CQ:(?:json|xml).*?].*?|.*?<\?xml.*?>.*?', event.message)
2021-11-05 14:08:51 +00:00
if filter_msg:
return
2022-06-28 06:11:03 +00:00
replyId = None
match_reply = re.match(r'^\[CQ:reply,id=(.*?)].*', event.message)
if match_reply:
replyId = int(match_reply.group(1))
2022-07-29 10:55:20 +00:00
prefix = None
2023-02-08 15:32:08 +00:00
if match_at := re.match(r'^\[CQ:at,qq=(.*?)](.*)', event.message):
2022-07-29 10:55:20 +00:00
if match_at.group(1) == qq_account:
2023-02-08 15:32:08 +00:00
event.message = match_at.group(2)
2023-02-08 16:08:10 +00:00
if event.message == '':
event.message = 'help'
2022-07-29 10:55:20 +00:00
prefix = ['']
2021-10-08 11:54:27 +00:00
targetId = 'QQ|' + (f'Group|{str(event.group_id)}' if event.detail_type == 'group' else str(event.user_id))
2022-06-17 03:50:28 +00:00
2021-10-08 11:54:27 +00:00
msg = MessageSession(MsgInfo(targetId=targetId,
senderId=f'QQ|{str(event.user_id)}',
targetFrom='QQ|Group' if event.detail_type == 'group' else 'QQ',
2023-02-25 06:14:51 +00:00
senderFrom='QQ', senderName=event.sender['nickname'], clientName='QQ',
2022-06-28 06:11:03 +00:00
messageId=event.message_id,
replyId=replyId),
2022-06-12 14:30:02 +00:00
Session(message=event,
target=event.group_id if event.detail_type == 'group' else event.user_id,
sender=event.user_id))
2022-07-29 10:55:20 +00:00
await parser(msg, running_mention=True, prefix=prefix)
2021-10-08 11:54:27 +00:00
2021-11-19 16:40:47 +00:00
class GuildAccountInfo:
tiny_id = None
2021-11-16 14:19:48 +00:00
@bot.on_message('guild')
async def _(event):
2021-11-19 16:40:47 +00:00
if GuildAccountInfo.tiny_id is None:
profile = await bot.call_action('get_guild_service_profile')
GuildAccountInfo.tiny_id = profile['tiny_id']
2021-11-16 14:19:48 +00:00
tiny_id = event.user_id
2021-11-19 16:40:47 +00:00
if tiny_id == GuildAccountInfo.tiny_id:
return
2022-06-28 06:11:03 +00:00
replyId = None
match_reply = re.match(r'^\[CQ:reply,id=(.*?)].*', event.message)
if match_reply:
replyId = int(match_reply.group(1))
2022-06-17 03:50:28 +00:00
targetId = f'QQ|Guild|{str(event.guild_id)}|{str(event.channel_id)}'
2023-02-06 10:39:42 +00:00
msg = MessageSession(MsgInfo(targetId=targetId,
senderId=f'QQ|Tiny|{str(event.user_id)}',
targetFrom='QQ|Guild',
2023-02-25 06:14:51 +00:00
senderFrom='QQ|Tiny', senderName=event.sender['nickname'], clientName='QQ',
messageId=event.message_id,
replyId=replyId),
Session(message=event,
target=f'{str(event.guild_id)}|{str(event.channel_id)}',
sender=event.user_id))
2022-03-16 11:20:30 +00:00
await parser(msg, running_mention=True)
2021-11-16 14:19:48 +00:00
2022-02-04 07:24:02 +00:00
"""@bot.on('request.friend')
2021-10-08 11:54:27 +00:00
async def _(event: Event):
if BotDBUtil.SenderInfo('QQ|' + str(event.user_id)).query.isInBlockList:
return {'approve': False}
2022-02-04 07:24:02 +00:00
return {'approve': True}"""
2021-10-08 11:54:27 +00:00
2021-11-10 05:00:03 +00:00
@bot.on('request.group.invite')
async def _(event: Event):
2023-01-14 05:01:34 +00:00
if BotDBUtil.SenderInfo('QQ|' + str(event.user_id)).query.isSuperUser:
return {'approve': True}
if Config('invite_join_group_notice'):
await bot.send_private_msg(user_id=event.user_id,
message='你好!本机器人暂时不主动同意入群请求。\n'
'请至https://github.com/Teahouse-Studios/bot/issues/new?assignees=OasisAkari&labels=New&template=add_new_group.yaml&title=%5BNEW%5D%3A+申请入群。')
2021-11-10 05:00:03 +00:00
@bot.on_notice('group_ban')
async def _(event: Event):
2022-07-29 10:55:20 +00:00
if event.user_id == int(qq_account):
2022-11-17 01:06:42 +00:00
result = BotDBUtil.UnfriendlyActions(targetId=event.group_id,
senderId=event.operator_id).add_and_check('mute', str(event.duration))
if event.duration >= 259200:
result = True
if result:
await bot.call_action('set_group_leave', group_id=event.group_id)
BotDBUtil.SenderInfo('QQ|' + str(event.operator_id)).edit('isInBlockList', True)
await bot.call_action('delete_friend', friend_id=event.operator_id)
"""
@bot.on_message('group')
async def _(event: Event):
result = BotDBUtil.isGroupInAllowList(f'QQ|Group|{str(event.group_id)}')
if not result:
await bot.send(event=event, message='此群不在白名单中,已自动退群。'
'\n如需申请白名单请至https://github.com/Teahouse-Studios/bot/issues/new/choose发起issue。')
await bot.call_action('set_group_leave', group_id=event.group_id)
"""
qq_host = Config("qq_host")
if qq_host:
host, port = qq_host.split(':')
bot.run(host=host, port=port, debug=False)