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

139 lines
5.3 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 logging
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
from bots.aiocqhttp.message_guild import MessageSession as MessageSessionGuild
from bots.aiocqhttp.tasks import MessageTaskManager, FinishedTasks
2021-10-15 17:52:40 +00:00
from config import Config
2022-06-12 07:07:53 +00:00
from core.elements import MsgInfo, Session, EnableDirtyWordCheck, PrivateAssets
2021-10-08 11:54:27 +00:00
from core.parser.message import parser
from core.utils import init, load_prompt, init_async
from database import BotDBUtil
from database.logging_message import UnfriendlyActions
2021-10-08 11:54:27 +00:00
PrivateAssets.set(os.path.abspath(os.path.dirname(__file__) + '/assets'))
2021-10-11 14:45:28 +00:00
EnableDirtyWordCheck.status = True
2021-10-08 11:54:27 +00:00
init()
@bot.on_startup
async def startup():
await init_async(FetchTarget)
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
2021-10-08 11:54:27 +00:00
all_tsk = MessageTaskManager.get()
user_id = event.user_id
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',
2022-06-12 14:30:02 +00:00
senderFrom='QQ', senderName='', clientName='QQ'),
Session(message=event,
target=event.group_id if event.detail_type == 'group' else event.user_id,
sender=event.user_id))
2022-06-17 05:59:15 +00:00
if targetId in all_tsk:
add_ = None
if user_id in all_tsk[targetId]:
add_ = user_id
if 'all' in all_tsk[targetId]:
add_ = 'all'
if add_:
FinishedTasks.add_task(targetId, add_, msg)
all_tsk[targetId][add_].set()
MessageTaskManager.del_task(targetId, add_)
2022-03-16 11:20:30 +00:00
await parser(msg, running_mention=True)
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-17 03:50:28 +00:00
targetId = f'QQ|Guild|{str(event.guild_id)}|{str(event.channel_id)}'
msg = MessageSessionGuild(MsgInfo(targetId=targetId,
2021-11-16 14:19:48 +00:00
senderId=f'QQ|Tiny|{str(event.user_id)}',
targetFrom='QQ|Guild',
2022-06-12 14:30:02 +00:00
senderFrom='QQ|Tiny', senderName='', clientName='QQ'),
2021-11-16 14:19:48 +00:00
Session(message=event,
target=f'{str(event.guild_id)}|{str(event.channel_id)}',
sender=event.user_id))
2022-06-17 05:59:15 +00:00
all_tsk = MessageTaskManager.guild_get()
if targetId in all_tsk:
if tiny_id in all_tsk[targetId]:
FinishedTasks.add_guild_task(targetId, tiny_id, msg)
all_tsk[targetId][tiny_id].set()
MessageTaskManager.del_guild_task(targetId, tiny_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):
await bot.send_private_msg(user_id=event.user_id,
message='你好!本机器人暂时不主动同意入群请求。\n'
2021-11-20 15:30:03 +00:00
'请至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):
if event.user_id == int(Config("qq_account")):
if event.duration >= 259200:
result = True
else:
result = UnfriendlyActions(targetId=event.group_id,
senderId=event.operator_id).add_and_check('mute', str(event.duration))
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)