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
|
|
|
|
|
from bots.aiocqhttp.message_guild import MessageSession as MessageSessionGuild
|
2021-10-15 17:52:40 +00:00
|
|
|
|
from config import Config
|
2022-06-28 10:14:44 +00:00
|
|
|
|
from core.elements import MsgInfo, Session, EnableDirtyWordCheck, PrivateAssets, Url
|
2021-10-08 11:54:27 +00:00
|
|
|
|
from core.parser.message import parser
|
2022-08-27 14:02:26 +00:00
|
|
|
|
from core.utils import init, load_prompt, init_async
|
2021-10-16 16:17:21 +00:00
|
|
|
|
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')
|
2021-10-08 11:54:27 +00:00
|
|
|
|
init()
|
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():
|
2022-06-14 12:41:58 +00:00
|
|
|
|
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
|
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
|
|
|
|
|
if match_at := re.match(r'^\[CQ:at,qq=(.*?)].*', event.message):
|
|
|
|
|
if match_at.group(1) == qq_account:
|
|
|
|
|
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',
|
2022-06-28 06:11:03 +00:00
|
|
|
|
senderFrom='QQ', senderName='', clientName='QQ',
|
|
|
|
|
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)}'
|
|
|
|
|
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-28 06:11:03 +00:00
|
|
|
|
senderFrom='QQ|Tiny', senderName='', clientName='QQ', messageId=event.message_id,
|
|
|
|
|
replyId=replyId),
|
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-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):
|
2021-11-15 16:26:11 +00:00
|
|
|
|
if BotDBUtil.SenderInfo('QQ|' + str(event.user_id)).query.isInBlockList:
|
2021-10-16 16:17:21 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
2021-10-16 16:17:21 +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))
|
2021-10-16 16:17:21 +00:00
|
|
|
|
if event.duration >= 259200:
|
|
|
|
|
result = True
|
|
|
|
|
if result:
|
|
|
|
|
await bot.call_action('set_group_leave', group_id=event.group_id)
|
2021-11-15 16:26:11 +00:00
|
|
|
|
BotDBUtil.SenderInfo('QQ|' + str(event.operator_id)).edit('isInBlockList', True)
|
2021-10-16 16:17:21 +00:00
|
|
|
|
await bot.call_action('delete_friend', friend_id=event.operator_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
@bot.on_message('group')
|
|
|
|
|
async def _(event: Event):
|
2021-11-15 16:26:11 +00:00
|
|
|
|
result = BotDBUtil.isGroupInAllowList(f'QQ|Group|{str(event.group_id)}')
|
2021-10-16 16:17:21 +00:00
|
|
|
|
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)
|