2021-10-08 11:54:27 +00:00
|
|
|
|
import asyncio
|
2021-11-12 14:25:53 +00:00
|
|
|
|
import html
|
2021-10-08 11:54:27 +00:00
|
|
|
|
import re
|
|
|
|
|
import traceback
|
|
|
|
|
from pathlib import Path
|
2021-11-16 14:19:48 +00:00
|
|
|
|
from typing import List, Union
|
2021-10-08 11:54:27 +00:00
|
|
|
|
|
2021-10-09 13:32:54 +00:00
|
|
|
|
from aiocqhttp import MessageSegment
|
2021-11-12 14:25:53 +00:00
|
|
|
|
|
2021-10-08 11:54:27 +00:00
|
|
|
|
from core.bots.aiocqhttp.client import bot
|
|
|
|
|
from core.bots.aiocqhttp.tasks import MessageTaskManager, FinishedTasks
|
2021-11-16 14:19:48 +00:00
|
|
|
|
from core.bots.aiocqhttp.message_guild import MessageSession as MessageSessionGuild
|
2021-10-24 10:55:45 +00:00
|
|
|
|
from core.elements import Plain, Image, MessageSession as MS, MsgInfo, Session, Voice, FetchTarget as FT, \
|
|
|
|
|
ExecutionLockList
|
2021-10-08 11:54:27 +00:00
|
|
|
|
from core.elements.others import confirm_command
|
2021-11-17 14:14:57 +00:00
|
|
|
|
from core.secret_check import Secret
|
2021-10-10 14:05:19 +00:00
|
|
|
|
from core.logger import Logger
|
2021-10-08 11:54:27 +00:00
|
|
|
|
from database import BotDBUtil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert2lst(s) -> list:
|
|
|
|
|
if isinstance(s, str):
|
|
|
|
|
return [Plain(s)]
|
|
|
|
|
elif isinstance(s, list):
|
|
|
|
|
return s
|
|
|
|
|
elif isinstance(s, tuple):
|
|
|
|
|
return list(s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MessageSession(MS):
|
|
|
|
|
class Feature:
|
|
|
|
|
image = True
|
|
|
|
|
voice = True
|
2021-10-11 14:45:28 +00:00
|
|
|
|
forward = True
|
2021-11-16 14:19:48 +00:00
|
|
|
|
delete = True
|
2021-10-08 11:54:27 +00:00
|
|
|
|
|
|
|
|
|
async def sendMessage(self, msgchain, quote=True):
|
|
|
|
|
msg = MessageSegment.text('')
|
|
|
|
|
if quote:
|
|
|
|
|
msg = MessageSegment.reply(self.session.message.message_id)
|
2021-11-17 14:14:57 +00:00
|
|
|
|
if Secret.find(msgchain):
|
|
|
|
|
return await self.sendMessage('https://wdf.ink/6Oup')
|
2021-10-08 11:54:27 +00:00
|
|
|
|
if isinstance(msgchain, str):
|
2021-11-03 12:33:05 +00:00
|
|
|
|
msg = msg + (MessageSegment.text(msgchain if msgchain != '' else
|
|
|
|
|
'发生错误:机器人尝试发送空文本消息,请联系机器人开发者解决问题。'
|
|
|
|
|
'\n错误汇报地址:https://github.com/Teahouse-Studios/bot/issues/new?assignees=OasisAkari&labels=bug&template=5678.md&title='))
|
2021-10-09 13:32:54 +00:00
|
|
|
|
elif isinstance(msgchain, (list, tuple)):
|
2021-11-03 12:33:05 +00:00
|
|
|
|
count = 0
|
2021-10-08 11:54:27 +00:00
|
|
|
|
for x in msgchain:
|
|
|
|
|
if isinstance(x, Plain):
|
2021-11-03 12:33:05 +00:00
|
|
|
|
msg = msg + MessageSegment.text(('\n' if count != 0 else '') + x.text)
|
2021-10-09 13:32:54 +00:00
|
|
|
|
elif isinstance(x, Image):
|
2021-10-08 11:54:27 +00:00
|
|
|
|
msg = msg + MessageSegment.image(Path(await x.get()).as_uri())
|
2021-10-09 13:32:54 +00:00
|
|
|
|
elif isinstance(x, Voice):
|
2021-10-08 11:54:27 +00:00
|
|
|
|
msg = msg + MessageSegment.record(Path(x.path).as_uri())
|
2021-11-03 12:33:05 +00:00
|
|
|
|
count += 1
|
2021-10-09 13:32:54 +00:00
|
|
|
|
else:
|
|
|
|
|
msg = msg + MessageSegment.text('发生错误:机器人尝试发送非法消息链,请联系机器人开发者解决问题。'
|
|
|
|
|
'\n错误汇报地址:https://github.com/Teahouse-Studios/bot/issues/new?assignees=OasisAkari&labels=bug&template=5678.md&title=')
|
2021-10-10 14:05:19 +00:00
|
|
|
|
Logger.info(f'[Bot] -> [{self.target.targetId}]: {msg}')
|
2021-10-08 11:54:27 +00:00
|
|
|
|
if self.target.targetFrom == 'QQ|Group':
|
|
|
|
|
send = await bot.send_group_msg(group_id=self.session.target, message=msg)
|
|
|
|
|
else:
|
|
|
|
|
send = await bot.send_private_msg(user_id=self.session.target, message=msg)
|
|
|
|
|
|
|
|
|
|
return MessageSession(target=MsgInfo(targetId=0, senderId=0, senderName='', targetFrom='QQ|Bot',
|
|
|
|
|
senderFrom='QQ|Bot'),
|
|
|
|
|
session=Session(message=send,
|
2021-10-08 13:14:09 +00:00
|
|
|
|
target=self.session.target,
|
2021-10-08 11:54:27 +00:00
|
|
|
|
sender=self.session.sender))
|
|
|
|
|
|
|
|
|
|
async def waitConfirm(self, msgchain=None, quote=True):
|
|
|
|
|
send = None
|
2021-10-24 10:55:45 +00:00
|
|
|
|
ExecutionLockList.remove(self)
|
2021-10-08 11:54:27 +00:00
|
|
|
|
if msgchain is not None:
|
|
|
|
|
msgchain = convert2lst(msgchain)
|
|
|
|
|
msgchain.append(Plain('(发送“是”或符合确认条件的词语来确认)'))
|
|
|
|
|
send = await self.sendMessage(msgchain, quote)
|
|
|
|
|
flag = asyncio.Event()
|
|
|
|
|
MessageTaskManager.add_task(self.session.sender, flag)
|
|
|
|
|
await flag.wait()
|
|
|
|
|
if send is not None:
|
|
|
|
|
await send.delete()
|
|
|
|
|
if FinishedTasks.get()[self.session.sender] in confirm_command:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
async def checkPermission(self):
|
|
|
|
|
if self.target.targetFrom == 'QQ' or self.target.senderInfo.check_TargetAdmin(
|
2021-10-19 10:11:47 +00:00
|
|
|
|
self.target.targetId) or self.target.senderInfo.query.isSuperUser:
|
2021-10-08 11:54:27 +00:00
|
|
|
|
return True
|
2021-11-12 14:25:53 +00:00
|
|
|
|
get_member_info = await bot.call_action('get_group_member_info', group_id=self.session.target,
|
|
|
|
|
user_id=self.session.sender)
|
2021-10-08 11:54:27 +00:00
|
|
|
|
if get_member_info['role'] in ['owner', 'admin']:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def checkSuperUser(self):
|
|
|
|
|
return True if self.target.senderInfo.query.isSuperUser else False
|
|
|
|
|
|
|
|
|
|
def asDisplay(self):
|
2021-10-08 12:25:57 +00:00
|
|
|
|
return html.unescape(self.session.message.message)
|
2021-10-08 11:54:27 +00:00
|
|
|
|
|
2021-10-11 14:45:28 +00:00
|
|
|
|
async def fake_forward_msg(self, nodelist):
|
|
|
|
|
if self.target.targetFrom == 'QQ|Group':
|
|
|
|
|
await bot.call_action('send_group_forward_msg', group_id=self.session.target, messages=nodelist)
|
|
|
|
|
|
2021-10-24 10:55:45 +00:00
|
|
|
|
async def sleep(self, s):
|
|
|
|
|
ExecutionLockList.remove(self)
|
|
|
|
|
await asyncio.sleep(s)
|
|
|
|
|
|
2021-10-08 11:54:27 +00:00
|
|
|
|
async def delete(self):
|
|
|
|
|
try:
|
|
|
|
|
if isinstance(self.session.message, list):
|
|
|
|
|
for x in self.session.message:
|
|
|
|
|
await bot.call_action('delete_msg', message_id=x['message_id'])
|
|
|
|
|
else:
|
|
|
|
|
print(self.session.message)
|
|
|
|
|
await bot.call_action('delete_msg', message_id=self.session.message['message_id'])
|
|
|
|
|
except Exception:
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
class Typing:
|
|
|
|
|
def __init__(self, msg: MS):
|
|
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
|
|
async def __aenter__(self):
|
2021-10-09 13:08:18 +00:00
|
|
|
|
if self.msg.target.targetFrom == 'QQ|Group':
|
2021-11-12 14:25:53 +00:00
|
|
|
|
await bot.send_group_msg(group_id=self.msg.session.target,
|
|
|
|
|
message=f'[CQ:poke,qq={self.msg.session.sender}]')
|
2021-10-08 11:54:27 +00:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FetchTarget(FT):
|
|
|
|
|
@staticmethod
|
2021-11-16 14:19:48 +00:00
|
|
|
|
async def fetch_target(targetId) -> Union[MessageSession, bool]:
|
|
|
|
|
matchTarget = re.match(r'^(QQ\|Group|QQ\|Guild|QQ)\|(.*)', targetId)
|
2021-10-08 11:54:27 +00:00
|
|
|
|
if matchTarget:
|
2021-11-16 14:19:48 +00:00
|
|
|
|
print(matchTarget.group(2))
|
|
|
|
|
if matchTarget.group(1) != 'QQ|Guild':
|
|
|
|
|
return MessageSession(MsgInfo(targetId=targetId, senderId=targetId, senderName='',
|
|
|
|
|
targetFrom=matchTarget.group(1), senderFrom=matchTarget.group(1)),
|
|
|
|
|
Session(message=False, target=int(matchTarget.group(2)),
|
|
|
|
|
sender=int(matchTarget.group(2))))
|
|
|
|
|
else:
|
|
|
|
|
return MessageSessionGuild(MsgInfo(targetId=targetId, senderId=targetId, senderName='',
|
|
|
|
|
targetFrom='QQ|Guild', senderFrom='QQ|Guild'),
|
|
|
|
|
Session(message=False, target=matchTarget.group(2),
|
|
|
|
|
sender=matchTarget.group(2)))
|
|
|
|
|
return False
|
2021-10-08 11:54:27 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def fetch_target_list(targetList: list) -> List[MessageSession]:
|
|
|
|
|
lst = []
|
|
|
|
|
group_list_raw = await bot.call_action('get_group_list')
|
|
|
|
|
group_list = []
|
|
|
|
|
for g in group_list_raw:
|
|
|
|
|
group_list.append(g['group_id'])
|
|
|
|
|
friend_list_raw = await bot.call_action('get_friend_list')
|
|
|
|
|
friend_list = []
|
2021-11-16 14:19:48 +00:00
|
|
|
|
guild_list_raw = await bot.call_action('get_guild_list')
|
|
|
|
|
guild_list = []
|
|
|
|
|
for g in guild_list_raw:
|
|
|
|
|
get_channel_list = await bot.call_action('get_guild_channel_list', guild_id=g['guild_id'])
|
|
|
|
|
for channel in get_channel_list:
|
|
|
|
|
if channel['channel_type'] == 1:
|
|
|
|
|
guild_list.append(f"{str(g['guild_id'])}|{str(channel['channel_id'])}")
|
2021-10-08 11:54:27 +00:00
|
|
|
|
for f in friend_list_raw:
|
|
|
|
|
friend_list.append(f)
|
|
|
|
|
for x in targetList:
|
|
|
|
|
fet = await FetchTarget.fetch_target(x)
|
|
|
|
|
if fet:
|
|
|
|
|
if fet.target.targetFrom == 'QQ|Group':
|
|
|
|
|
if fet.session.target not in group_list:
|
|
|
|
|
continue
|
|
|
|
|
if fet.target.targetFrom == 'QQ':
|
|
|
|
|
if fet.session.target not in friend_list:
|
|
|
|
|
continue
|
2021-11-16 14:19:48 +00:00
|
|
|
|
if fet.target.targetFrom == 'QQ|Guild':
|
|
|
|
|
if fet.session.target not in guild_list:
|
|
|
|
|
continue
|
2021-10-08 11:54:27 +00:00
|
|
|
|
lst.append(fet)
|
|
|
|
|
return lst
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def post_message(module_name, message, user_list: List[MessageSession] = None):
|
|
|
|
|
send_list = []
|
|
|
|
|
if user_list is not None:
|
|
|
|
|
for x in user_list:
|
|
|
|
|
try:
|
|
|
|
|
send = await x.sendMessage(message, quote=False)
|
|
|
|
|
send_list.append(send)
|
|
|
|
|
except Exception:
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
else:
|
|
|
|
|
get_target_id = BotDBUtil.Module.get_enabled_this(module_name)
|
|
|
|
|
group_list_raw = await bot.call_action('get_group_list')
|
|
|
|
|
group_list = []
|
|
|
|
|
for g in group_list_raw:
|
|
|
|
|
group_list.append(g['group_id'])
|
|
|
|
|
friend_list_raw = await bot.call_action('get_friend_list')
|
|
|
|
|
friend_list = []
|
|
|
|
|
for f in friend_list_raw:
|
|
|
|
|
friend_list.append(f)
|
2021-11-16 14:19:48 +00:00
|
|
|
|
guild_list_raw = await bot.call_action('get_guild_list')
|
|
|
|
|
guild_list = []
|
|
|
|
|
for g in guild_list_raw:
|
|
|
|
|
get_channel_list = await bot.call_action('get_guild_channel_list', guild_id=g['guild_id'])
|
|
|
|
|
for channel in get_channel_list:
|
|
|
|
|
if channel['channel_type'] == 1:
|
|
|
|
|
guild_list.append(f"{str(g['guild_id'])}|{str(channel['channel_id'])}")
|
2021-10-08 11:54:27 +00:00
|
|
|
|
for x in get_target_id:
|
|
|
|
|
fetch = await FetchTarget.fetch_target(x)
|
2021-11-16 23:08:09 +00:00
|
|
|
|
Logger.info(fetch)
|
2021-10-08 11:54:27 +00:00
|
|
|
|
if fetch:
|
|
|
|
|
if fetch.target.targetFrom == 'QQ|Group':
|
|
|
|
|
if fetch.session.target not in group_list:
|
|
|
|
|
continue
|
|
|
|
|
if fetch.target.targetFrom == 'QQ':
|
|
|
|
|
if fetch.session.target not in friend_list:
|
|
|
|
|
continue
|
2021-11-16 14:19:48 +00:00
|
|
|
|
if fetch.target.targetFrom == 'QQ|Guild':
|
|
|
|
|
if fetch.session.target not in guild_list:
|
|
|
|
|
continue
|
2021-10-08 11:54:27 +00:00
|
|
|
|
try:
|
2021-11-16 14:19:48 +00:00
|
|
|
|
print(fetch)
|
2021-10-08 11:54:27 +00:00
|
|
|
|
send = await fetch.sendMessage(message, quote=False)
|
|
|
|
|
send_list.append(send)
|
|
|
|
|
await asyncio.sleep(0.5)
|
|
|
|
|
except Exception:
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
return send_list
|