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/message.py

354 lines
15 KiB
Python
Raw Normal View History

2021-10-08 11:54:27 +00:00
import asyncio
2022-05-12 14:25:22 +00:00
import datetime
2021-11-12 14:25:53 +00:00
import html
2022-04-23 14:32:08 +00:00
import random
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
2022-02-02 05:16:49 +00:00
import aiocqhttp.exceptions
2022-08-30 12:49:01 +00:00
import ujson as json
2021-10-09 13:32:54 +00:00
from aiocqhttp import MessageSegment
2021-11-12 14:25:53 +00:00
2022-06-12 07:07:53 +00:00
from bots.aiocqhttp.client import bot
2022-07-14 13:23:15 +00:00
from config import Config
2023-03-05 11:15:44 +00:00
from core.builtins import Bot, ErrorMessage
2023-04-17 05:26:35 +00:00
from core.builtins import Plain, Image, Voice, Temp, command_prefix
2022-06-27 17:00:00 +00:00
from core.builtins.message import MessageSession as MS
2023-02-05 14:33:33 +00:00
from core.builtins.message.chain import MessageChain
2021-10-10 14:05:19 +00:00
from core.logger import Logger
2023-02-05 14:33:33 +00:00
from core.types import MsgInfo, Session, FetchTarget as FT, \
FetchedSession as FS, FinishedSession as FinS
2021-10-08 11:54:27 +00:00
from database import BotDBUtil
2022-07-14 13:23:15 +00:00
enable_analytics = Config('enable_analytics')
2023-02-04 14:42:21 +00:00
base_superuser = Config('base_superuser')
2022-07-14 13:23:15 +00:00
2022-01-15 14:28:35 +00:00
class FinishedSession(FinS):
async def delete(self):
"""
用于删除这条消息
"""
2023-02-06 10:48:09 +00:00
if self.session.target.targetFrom in ['QQ|Group', 'QQ']:
try:
for x in self.messageId:
if x != 0:
await bot.call_action('delete_msg', message_id=x)
2023-02-06 10:48:09 +00:00
except Exception:
Logger.error(traceback.format_exc())
2021-10-08 11:54:27 +00:00
2022-05-12 14:25:22 +00:00
last_send_typing_time = {}
2023-02-04 14:42:21 +00:00
Temp.data['is_group_message_blocked'] = False
Temp.data['waiting_for_send_group_message'] = []
2022-05-12 14:25:22 +00:00
2021-10-08 11:54:27 +00:00
class MessageSession(MS):
class Feature:
image = True
voice = True
2022-01-16 13:24:15 +00:00
embed = False
2021-10-11 14:45:28 +00:00
forward = True
2021-11-16 14:19:48 +00:00
delete = True
2022-01-16 13:24:15 +00:00
wait = True
quote = True
2022-08-27 16:32:10 +00:00
async def sendMessage(self, msgchain, quote=True, disable_secret_check=False,
allow_split_image=True) -> FinishedSession:
2021-10-08 11:54:27 +00:00
msg = MessageSegment.text('')
if quote and self.target.targetFrom == 'QQ|Group' and self.session.message:
2021-10-08 11:54:27 +00:00
msg = MessageSegment.reply(self.session.message.message_id)
2021-12-31 14:44:34 +00:00
msgchain = MessageChain(msgchain)
2022-01-08 08:08:25 +00:00
if not msgchain.is_safe and not disable_secret_check:
2023-03-05 11:15:44 +00:00
return await self.sendMessage(Plain(ErrorMessage(self.locale.t("error.message.chain.unsafe"))))
2022-06-26 06:06:26 +00:00
self.sent.append(msgchain)
2021-12-31 14:44:34 +00:00
count = 0
2023-03-05 11:15:44 +00:00
for x in msgchain.asSendable(locale=self.locale.locale, embed=False):
2021-12-31 14:44:34 +00:00
if isinstance(x, Plain):
msg = msg + MessageSegment.text(('\n' if count != 0 else '') + x.text)
elif isinstance(x, Image):
msg = msg + MessageSegment.image(Path(await x.get()).as_uri())
elif isinstance(x, Voice):
2023-02-06 10:39:42 +00:00
if self.target.targetFrom != 'QQ|Guild':
msg = msg + MessageSegment.record(file=Path(x.path).as_uri())
2021-12-31 14:44:34 +00:00
count += 1
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':
2022-02-02 05:16:49 +00:00
try:
send = await bot.send_group_msg(group_id=self.session.target, message=msg)
except aiocqhttp.exceptions.ActionFailed:
2022-04-23 14:32:08 +00:00
anti_autofilter_word_list = ['ffk', '(阻止风向控制)', '(房蜂控)']
msg = msg + MessageSegment.text(random.choice(anti_autofilter_word_list))
2022-02-02 05:16:49 +00:00
send = await bot.send_group_msg(group_id=self.session.target, message=msg)
2023-02-06 10:39:42 +00:00
elif self.target.targetFrom == 'QQ|Guild':
match_guild = re.match(r'(.*)\|(.*)', self.session.target)
send = await bot.call_action('send_guild_channel_msg', guild_id=int(match_guild.group(1)),
channel_id=int(match_guild.group(2)), message=msg)
2021-10-08 11:54:27 +00:00
else:
try:
send = await bot.send_private_msg(user_id=self.session.target, message=msg)
except aiocqhttp.exceptions.ActionFailed as e:
if self.session.message.detail_type == 'private' and self.session.message.sub_type == 'group':
return FinishedSession(self, 0, [{}])
else:
raise e
2023-02-06 10:48:09 +00:00
return FinishedSession(self, send['message_id'], [send])
2021-10-08 11:54:27 +00:00
async def checkPermission(self):
if self.target.targetFrom == 'QQ' \
2023-04-30 03:30:59 +00:00
or self.target.senderId in self.custom_admins \
or self.target.senderInfo.query.isSuperUser:
2021-10-08 11:54:27 +00:00
return True
2023-02-06 10:39:42 +00:00
return await self.checkNativePermission()
2021-10-08 11:54:27 +00:00
async def checkNativePermission(self):
if self.target.targetFrom == 'QQ':
return True
2023-02-06 10:39:42 +00:00
elif self.target.targetFrom == 'QQ|Group':
get_member_info = await bot.call_action('get_group_member_info', group_id=self.session.target,
user_id=self.session.sender)
if get_member_info['role'] in ['owner', 'admin']:
return True
elif self.target.targetFrom == 'QQ|Guild':
match_guild = re.match(r'(.*)\|(.*)', self.session.target)
get_member_info = await bot.call_action('get_guild_member_profile', guild_id=match_guild.group(1),
user_id=self.session.sender)
for m in get_member_info['roles']:
if m['role_id'] == "2":
return True
get_guild_info = await bot.call_action('get_guild_meta_by_guest', guild_id=match_guild.group(1))
if get_guild_info['owner_id'] == self.session.sender:
return True
return False
return False
2023-03-04 09:40:38 +00:00
def asDisplay(self, text_only=False):
m = html.unescape(self.session.message.message)
if text_only:
return ''.join(
re.split(r'\[CQ:.*?]', m)).strip()
m = re.sub(r'\[CQ:at,qq=(.*?)]', r'QQ|\1', m)
2023-02-07 05:27:25 +00:00
m = re.sub(r'\[CQ:forward,id=(.*?)]', r'\[Ke:forward,id=\1]', m)
2022-01-20 12:13:03 +00:00
return ''.join(
re.split(r'\[CQ:.*?]', m)).strip()
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':
2021-11-17 16:17:41 +00:00
await bot.call_action('send_group_forward_msg', group_id=int(self.session.target), messages=nodelist)
2021-10-11 14:45:28 +00:00
2021-10-08 11:54:27 +00:00
async def delete(self):
2023-02-06 10:39:42 +00:00
if self.target.targetFrom in ['QQ', 'QQ|Group']:
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:
await bot.call_action('delete_msg', message_id=self.session.message['message_id'])
except Exception:
Logger.error(traceback.format_exc())
async def get_text_channel_list(self):
match_guild = re.match(r'(.*)\|(.*)', self.session.target)
get_channels_info = await bot.call_action('get_guild_channel_list', guild_id=match_guild.group(1),
no_cache=True)
lst = []
for m in get_channels_info:
if m['channel_type'] == 1:
lst.append(f'{m["owner_guild_id"]}|{m["channel_id"]}')
return lst
2021-10-08 11:54:27 +00:00
2022-03-05 13:34:08 +00:00
async def call_api(self, action, **params):
return await bot.call_action(action, **params)
2021-10-08 11:54:27 +00:00
class Typing:
def __init__(self, msg: MS):
self.msg = msg
async def __aenter__(self):
2022-05-12 14:25:22 +00:00
if self.msg.target.targetFrom == 'QQ|Group':
if self.msg.session.sender in last_send_typing_time:
2022-05-15 15:55:04 +00:00
if datetime.datetime.now().timestamp() - last_send_typing_time[self.msg.session.sender] <= 3600:
2022-05-12 14:25:22 +00:00
return
last_send_typing_time[self.msg.session.sender] = datetime.datetime.now().timestamp()
2021-11-12 14:25:53 +00:00
await bot.send_group_msg(group_id=self.msg.session.target,
2022-05-12 14:25:22 +00:00
message=f'[CQ:poke,qq={self.msg.session.sender}]')
2021-10-08 11:54:27 +00:00
async def __aexit__(self, exc_type, exc_val, exc_tb):
pass
2022-01-15 13:46:23 +00:00
class FetchedSession(FS):
def __init__(self, targetFrom, targetId):
self.target = MsgInfo(targetId=f'{targetFrom}|{targetId}',
senderId=f'{targetFrom}|{targetId}',
targetFrom=targetFrom,
senderFrom=targetFrom,
2022-06-12 14:30:02 +00:00
senderName='',
2022-06-28 06:11:03 +00:00
clientName='QQ',
messageId=0,
replyId=None)
2022-01-15 13:46:23 +00:00
self.session = Session(message=False, target=targetId, sender=targetId)
2023-02-06 10:40:51 +00:00
self.parent = MessageSession(self.target, self.session)
2022-01-15 13:46:23 +00:00
2021-10-08 11:54:27 +00:00
class FetchTarget(FT):
2022-01-05 11:14:45 +00:00
name = 'QQ'
2021-10-08 11:54:27 +00:00
@staticmethod
2022-01-15 13:46:23 +00:00
async def fetch_target(targetId) -> Union[FetchedSession, bool]:
2021-11-16 14:19:48 +00:00
matchTarget = re.match(r'^(QQ\|Group|QQ\|Guild|QQ)\|(.*)', targetId)
2021-10-08 11:54:27 +00:00
if matchTarget:
2022-01-15 13:46:23 +00:00
return FetchedSession(matchTarget.group(1), matchTarget.group(2))
2021-11-16 14:19:48 +00:00
return False
2021-10-08 11:54:27 +00:00
@staticmethod
2022-01-15 13:46:23 +00:00
async def fetch_target_list(targetList: list) -> List[FetchedSession]:
2021-10-08 11:54:27 +00:00
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
2023-03-29 14:42:53 +00:00
async def post_message(module_name, message, user_list: List[FetchedSession] = None, i18n=False, **kwargs):
2023-02-08 15:55:25 +00:00
_tsk = []
blocked = False
2023-03-29 14:42:53 +00:00
async def post_(fetch_: FetchedSession):
2023-02-08 15:55:25 +00:00
nonlocal _tsk
nonlocal blocked
2022-08-29 16:31:41 +00:00
try:
2023-02-04 14:42:21 +00:00
if Temp.data['is_group_message_blocked'] and fetch_.target.targetFrom == 'QQ|Group':
Temp.data['waiting_for_send_group_message'].append({'fetch': fetch_, 'message': message})
else:
2023-03-29 14:42:53 +00:00
if i18n:
2023-04-05 05:36:11 +00:00
if isinstance(message, dict):
if (gm := message.get(fetch.parent.locale.locale)) is not None:
await fetch_.sendDirectMessage(gm)
else:
await fetch_.sendDirectMessage(message.get('fallback'))
else:
await fetch_.sendDirectMessage(fetch_.parent.locale.t(message, **kwargs))
2023-03-29 14:42:53 +00:00
else:
await fetch_.sendDirectMessage(message)
2023-02-08 15:55:25 +00:00
if _tsk:
_tsk = []
2022-08-29 16:31:41 +00:00
if enable_analytics:
BotDBUtil.Analytics(fetch_).add('', module_name, 'schedule')
await asyncio.sleep(0.5)
2023-02-04 14:42:21 +00:00
except aiocqhttp.ActionFailed as e:
if e.result['wording'] == 'send group message failed: blocked by server':
2023-02-08 15:55:25 +00:00
if len(_tsk) >= 3:
blocked = True
if not blocked:
_tsk.append({'fetch': fetch_, 'message': message})
else:
Temp.data['is_group_message_blocked'] = True
Temp.data['waiting_for_send_group_message'].append({'fetch': fetch_, 'message': message})
if _tsk:
for t in _tsk:
Temp.data['waiting_for_send_group_message'].append(t)
_tsk = []
fetch_base_superuser = await FetchTarget.fetch_target(base_superuser)
if fetch_base_superuser:
2023-04-08 07:27:27 +00:00
await fetch_base_superuser.\
2023-04-17 05:26:35 +00:00
sendDirectMessage(fetch_base_superuser.parent.locale.t("error.message.paused",
prefix=command_prefix[0]))
2022-08-29 16:31:41 +00:00
except Exception:
Logger.error(traceback.format_exc())
2023-01-28 05:53:11 +00:00
2021-10-08 11:54:27 +00:00
if user_list is not None:
for x in user_list:
2022-08-29 16:31:41 +00:00
await post_(x)
2021-10-08 11:54:27 +00:00
else:
2022-08-29 15:54:20 +00:00
get_target_id = BotDBUtil.TargetInfo.get_enabled_this(module_name, "QQ")
2021-10-08 11:54:27 +00:00
group_list_raw = await bot.call_action('get_group_list')
2022-08-29 15:54:20 +00:00
group_list = [g['group_id'] for g in group_list_raw]
2021-10-08 11:54:27 +00:00
friend_list_raw = await bot.call_action('get_friend_list')
2022-08-29 15:54:20 +00:00
friend_list = [f['user_id'] for f in friend_list_raw]
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:
2022-06-17 06:02:13 +00:00
try:
get_channel_list = await bot.call_action('get_guild_channel_list', guild_id=g['guild_id'],
no_cache=True)
for channel in get_channel_list:
if channel['channel_type'] == 1:
guild_list.append(f"{str(g['guild_id'])}|{str(channel['channel_id'])}")
except Exception:
traceback.print_exc()
continue
2022-08-29 16:31:41 +00:00
in_whitelist = []
else_ = []
2021-10-08 11:54:27 +00:00
for x in get_target_id:
2022-08-29 15:54:20 +00:00
fetch = await FetchTarget.fetch_target(x.targetId)
2022-08-04 07:52:42 +00:00
Logger.debug(fetch)
2021-10-08 11:54:27 +00:00
if fetch:
if fetch.target.targetFrom == 'QQ|Group':
2022-01-15 13:46:23 +00:00
if int(fetch.session.target) not in group_list:
2021-10-08 11:54:27 +00:00
continue
if fetch.target.targetFrom == 'QQ':
2022-01-15 13:46:23 +00:00
if int(fetch.session.target) not in friend_list:
2021-10-08 11:54:27 +00:00
continue
2021-11-16 14:19:48 +00:00
if fetch.target.targetFrom == 'QQ|Guild':
if fetch.session.target not in guild_list:
continue
2022-08-29 16:31:41 +00:00
2023-02-08 16:01:39 +00:00
if fetch.target.targetFrom in ['QQ', 'QQ|Guild']:
2022-08-29 16:31:41 +00:00
in_whitelist.append(post_(fetch))
else:
2022-08-30 05:05:40 +00:00
load_options: dict = json.loads(x.options)
if load_options.get('in_post_whitelist', False):
in_whitelist.append(post_(fetch))
else:
else_.append(post_(fetch))
2022-08-29 16:31:41 +00:00
if in_whitelist:
for x in in_whitelist:
await x
2022-08-29 16:40:47 +00:00
await asyncio.sleep(random.randint(1, 5))
2022-08-29 16:31:41 +00:00
async def post_not_in_whitelist(lst):
for f in lst:
await f
2023-02-08 16:01:39 +00:00
await asyncio.sleep(random.randint(15, 30))
2022-08-29 16:31:41 +00:00
if else_:
asyncio.create_task(post_not_in_whitelist(else_))
2023-03-29 14:42:53 +00:00
Logger.info(f"The task of posting messages to whitelisted groups is complete. "
f"Posting message to {len(else_)} groups not in whitelist.")
2023-02-04 14:42:21 +00:00
Bot.MessageSession = MessageSession
Bot.FetchTarget = FetchTarget