Archived
1
0
Fork 0

add FetchedSession

This commit is contained in:
yzhh 2022-01-15 21:46:23 +08:00
parent a1220e5503
commit 39675bf5d4
6 changed files with 150 additions and 65 deletions

View file

@ -11,7 +11,7 @@ from core.bots.aiocqhttp.client import bot
from core.bots.aiocqhttp.tasks import MessageTaskManager, FinishedTasks
from core.bots.aiocqhttp.message_guild import MessageSession as MessageSessionGuild
from core.elements import Plain, Image, MessageSession as MS, MsgInfo, Session, Voice, FetchTarget as FT, \
ExecutionLockList
ExecutionLockList, FetchedSession as FS
from core.elements.message.chain import MessageChain
from core.elements.others import confirm_command
from core.logger import Logger
@ -136,28 +136,43 @@ class MessageSession(MS):
pass
class FetchedSession(FS):
def __init__(self, targetFrom, targetId):
self.target = MsgInfo(targetId=f'{targetFrom}|{targetId}',
senderId=f'{targetFrom}|{targetId}',
targetFrom=targetFrom,
senderFrom=targetFrom,
senderName='')
self.session = Session(message=False, target=targetId, sender=targetId)
if targetFrom == 'QQ|Guild':
self.parent = MessageSessionGuild(self.target, self.session)
else:
self.parent = MessageSession(self.target, self.session)
async def sendMessage(self, msgchain, disable_secret_check=False):
"""
用于向获取对象发送消息
:param msgchain: 消息链若传入str则自动创建一条带有Plain元素的消息链
:param disable_secret_check: 是否禁用消息检查默认为False
:return: 被发送的消息链
"""
send = await self.parent.sendMessage(msgchain, disable_secret_check=disable_secret_check, quote=False)
print(send)
return send
class FetchTarget(FT):
name = 'QQ'
@staticmethod
async def fetch_target(targetId) -> Union[MessageSession, bool]:
async def fetch_target(targetId) -> Union[FetchedSession, bool]:
matchTarget = re.match(r'^(QQ\|Group|QQ\|Guild|QQ)\|(.*)', targetId)
if matchTarget:
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 FetchedSession(matchTarget.group(1), matchTarget.group(2))
return False
@staticmethod
async def fetch_target_list(targetList: list) -> List[MessageSession]:
async def fetch_target_list(targetList: list) -> List[FetchedSession]:
lst = []
group_list_raw = await bot.call_action('get_group_list')
group_list = []
@ -190,12 +205,12 @@ class FetchTarget(FT):
return lst
@staticmethod
async def post_message(module_name, message, user_list: List[MessageSession] = None):
async def post_message(module_name, message, user_list: List[FetchedSession] = None):
send_list = []
if user_list is not None:
for x in user_list:
try:
send = await x.sendMessage(message, quote=False)
send = await x.sendMessage(message)
send_list.append(send)
except Exception:
traceback.print_exc()
@ -208,7 +223,7 @@ class FetchTarget(FT):
friend_list_raw = await bot.call_action('get_friend_list')
friend_list = []
for f in friend_list_raw:
friend_list.append(f)
friend_list.append(f['user_id'])
guild_list_raw = await bot.call_action('get_guild_list')
guild_list = []
for g in guild_list_raw:
@ -221,17 +236,17 @@ class FetchTarget(FT):
Logger.info(fetch)
if fetch:
if fetch.target.targetFrom == 'QQ|Group':
if fetch.session.target not in group_list:
if int(fetch.session.target) not in group_list:
continue
if fetch.target.targetFrom == 'QQ':
if fetch.session.target not in friend_list:
if int(fetch.session.target) not in friend_list:
continue
if fetch.target.targetFrom == 'QQ|Guild':
if fetch.session.target not in guild_list:
continue
try:
print(fetch)
send = await fetch.sendMessage(message, quote=False)
send = await fetch.sendMessage(message)
send_list.append(send)
await asyncio.sleep(0.5)
except Exception:

View file

@ -31,8 +31,8 @@ class MessageSession(MS):
async def sendMessage(self, msgchain, quote=True, disable_secret_check=False):
msg = MessageSegment.text('')
# if quote:
# msg = MessageSegment.reply(self.session.message.message_id)
if quote:
msg = MessageSegment.reply(self.session.message.message_id)
msgchain = MessageChain(msgchain)
if not msgchain.is_safe and not disable_secret_check:
return await self.sendMessage('https://wdf.ink/6Oup')

View file

@ -6,7 +6,7 @@ from typing import List, Union
from core.bots.aiogram.client import dp, bot
from core.bots.aiogram.tasks import MessageTaskManager, FinishedTasks
from core.elements import Plain, Image, MessageSession as MS, MsgInfo, Session, Voice, FetchTarget as FT, \
ExecutionLockList
ExecutionLockList, FetchedSession as FS
from core.elements.message.chain import MessageChain
from core.elements.others import confirm_command
from database import BotDBUtil
@ -124,21 +124,39 @@ class MessageSession(MS):
pass
class FetchedSession(FS):
def __init__(self, targetFrom, targetId):
self.target = MsgInfo(targetId=f'{targetFrom}|{targetId}',
senderId=f'{targetFrom}|{targetId}',
targetFrom=targetFrom,
senderFrom=targetFrom,
senderName='')
self.session = Session(message=False, target=targetId, sender=targetId)
self.parent = MessageSession(self.target, self.session)
async def sendMessage(self, msgchain, disable_secret_check=False):
"""
用于向获取对象发送消息
:param msgchain: 消息链若传入str则自动创建一条带有Plain元素的消息链
:param disable_secret_check: 是否禁用消息检查默认为False
:return: 被发送的消息链
"""
return await self.parent.sendMessage(msgchain, disable_secret_check=disable_secret_check, quote=False)
class FetchTarget(FT):
name = 'Telegram'
@staticmethod
async def fetch_target(targetId) -> Union[MessageSession, bool]:
async def fetch_target(targetId) -> Union[FetchedSession, bool]:
matchChannel = re.match(r'^(Telegram\|.*?)\|(.*)', targetId)
if matchChannel:
return MessageSession(MsgInfo(targetId=targetId, senderId=targetId, senderName='',
targetFrom=matchChannel.group(1), senderFrom=matchChannel.group(1)),
Session(message=False, target=matchChannel.group(2), sender=matchChannel.group(2)))
return FetchedSession(matchChannel.group(1), matchChannel.group(2))
else:
return False
@staticmethod
async def fetch_target_list(targetList: list) -> List[MessageSession]:
async def fetch_target_list(targetList: list) -> List[FetchedSession]:
lst = []
for x in targetList:
fet = await FetchTarget.fetch_target(x)
@ -147,12 +165,12 @@ class FetchTarget(FT):
return lst
@staticmethod
async def post_message(module_name, message, user_list: List[MessageSession] = None):
async def post_message(module_name, message, user_list: List[FetchedSession] = None):
send_list = []
if user_list is not None:
for x in user_list:
try:
send = await x.sendMessage(message, quote=False)
send = await x.sendMessage(message)
send_list.append(send)
except Exception:
traceback.print_exc()
@ -162,7 +180,7 @@ class FetchTarget(FT):
fetch = await FetchTarget.fetch_target(x)
if fetch:
try:
send = await fetch.sendMessage(message, quote=False)
send = await fetch.sendMessage(message)
send_list.append(send)
except Exception:
traceback.print_exc()

View file

@ -7,7 +7,8 @@ from typing import List, Union
import discord
from core.bots.discord.client import client
from core.elements import Plain, Image, MessageSession as MS, MsgInfo, Session, FetchTarget as FT, ExecutionLockList
from core.elements import Plain, Image, MessageSession as MS, MsgInfo, Session, FetchTarget as FT, ExecutionLockList,\
FetchedSession as FS
from core.elements.message.chain import MessageChain
from core.elements.message.internal import Embed
from core.elements.others import confirm_command
@ -149,22 +150,45 @@ class MessageSession(MS):
pass
class FetchedSession(FS):
def __init__(self, targetFrom, targetId):
self.target = MsgInfo(targetId=f'{targetFrom}|{targetId}',
senderId=f'{targetFrom}|{targetId}',
targetFrom=targetFrom,
senderFrom=targetFrom,
senderName='')
self.session = Session(message=False, target=targetId, sender=targetId)
self.parent = MessageSession(self.target, self.session)
async def sendMessage(self, msgchain, disable_secret_check=False):
"""
用于向获取对象发送消息
:param msgchain: 消息链若传入str则自动创建一条带有Plain元素的消息链
:param disable_secret_check: 是否禁用消息检查默认为False
:return: 被发送的消息链
"""
try:
getChannel = await client.fetch_channel(self.session.target)
except Exception:
traceback.print_exc()
return False
self.session.target = self.session.sender = self.parent.session.target = self.parent.session.sender = getChannel
return await self.parent.sendMessage(msgchain, disable_secret_check=disable_secret_check, quote=False)
class FetchTarget(FT):
name = 'Discord'
@staticmethod
async def fetch_target(targetId) -> Union[MessageSession, bool]:
async def fetch_target(targetId) -> Union[FetchedSession, bool]:
matchChannel = re.match(r'^(Discord\|(?:DM\||)Channel)\|(.*)', targetId)
if matchChannel:
getChannel = await client.fetch_channel(int(matchChannel.group(2)))
return MessageSession(MsgInfo(targetId=targetId, senderId=targetId, senderName='',
targetFrom=matchChannel.group(1), senderFrom=matchChannel.group(1)),
Session(message=False, target=getChannel, sender=getChannel))
return FetchedSession(matchChannel.group(1), matchChannel.group(2))
else:
return False
@staticmethod
async def fetch_target_list(targetList: list) -> List[MessageSession]:
async def fetch_target_list(targetList: list) -> List[FetchedSession]:
lst = []
for x in targetList:
fet = await FetchTarget.fetch_target(x)
@ -173,7 +197,7 @@ class FetchTarget(FT):
return lst
@staticmethod
async def post_message(module_name, message, user_list: List[MessageSession] = None):
async def post_message(module_name, message, user_list: List[FetchedSession] = None):
send_list = []
if user_list is not None:
for x in user_list:
@ -188,7 +212,7 @@ class FetchTarget(FT):
fetch = await FetchTarget.fetch_target(x)
if fetch:
try:
send = await fetch.sendMessage(message, quote=False)
send = await fetch.sendMessage(message)
send_list.append(send)
except Exception:
traceback.print_exc()

View file

@ -2,7 +2,7 @@ from typing import List
from PIL import Image
from core.elements import MessageSession, Plain, Image as BImage, Session, MsgInfo, FetchTarget as FT, Voice, Embed
from core.elements import MessageSession, Plain, Image as BImage, Session, MsgInfo, FetchTarget as FT, Voice, Embed, FetchedSession as FS
from core.elements.others import confirm_command
from core.elements.message.chain import MessageChain
from core.logger import Logger
@ -15,7 +15,7 @@ class Template(MessageSession):
forward = False
delete = True
async def sendMessage(self, msgchain, quote=True) -> MessageSession:
async def sendMessage(self, msgchain, quote=True, disable_secret_check=False) -> MessageSession:
Logger.info(msgchain)
msgchain = MessageChain(msgchain)
Logger.info(msgchain)
@ -70,27 +70,34 @@ class Template(MessageSession):
pass
class FetchedSession(FS):
def __init__(self, targetFrom, targetId):
self.target = MsgInfo(targetId=f'{targetFrom}|{targetId}',
senderId=f'{targetFrom}|{targetId}',
targetFrom=targetFrom,
senderFrom=targetFrom,
senderName='')
self.session = Session(message=False, target=targetId, sender=targetId)
self.parent = Template(self.target, self.session)
async def sendMessage(self, msgchain, disable_secret_check=False):
"""
用于向获取对象发送消息
:param msgchain: 消息链若传入str则自动创建一条带有Plain元素的消息链
:param disable_secret_check: 是否禁用消息检查默认为False
:return: 被发送的消息链
"""
return await self.parent.sendMessage(msgchain, disable_secret_check=disable_secret_check, quote=False)
class FetchTarget(FT):
name = 'TEST'
@staticmethod
async def fetch_target(targetId):
return Template(target=MsgInfo(targetId=targetId,
senderId=targetId,
senderName='',
targetFrom='TEST|Console',
senderFrom='TEST|Console'),
session=Session(message=False, target=targetId, sender=targetId))
async def fetch_target(targetId) -> FetchedSession:
return FetchedSession('TEST|Console', targetId)
@staticmethod
async def post_message(module_name, message, user_list: List[MessageSession] = None):
if isinstance(message, str):
print(message)
elif isinstance(message, list):
for x in message:
if isinstance(x, Plain):
print(x.text)
if isinstance(x, BImage):
img = Image.open(await x.get())
img.show()
return message
async def post_message(module_name, message, user_list: List[FetchedSession] = None):
fetch = await FetchTarget.fetch_target('0')
await fetch.sendMessage(message)

View file

@ -1,3 +1,4 @@
import asyncio
from typing import List, Union
@ -106,7 +107,7 @@ class MessageSession:
...
async def sleep(self, s):
...
await asyncio.sleep(s)
class Feature:
"""
@ -118,28 +119,48 @@ class MessageSession:
delete = ...
class FetchedSession:
def __init__(self, targetFrom, targetId):
self.target = MsgInfo(targetId=f'{targetFrom}|{targetId}',
senderId=f'{targetFrom}|{targetId}',
targetFrom=targetFrom,
senderFrom=targetFrom,
senderName='')
self.session = Session(message=False, target=targetId, sender=targetId)
self.parent = MessageSession(self.target, self.session)
async def sendMessage(self, msgchain, disable_secret_check=False):
"""
用于向获取对象发送消息
:param msgchain: 消息链若传入str则自动创建一条带有Plain元素的消息链
:param disable_secret_check: 是否禁用消息检查默认为False
:return: 被发送的消息链
"""
...
class FetchTarget:
name = ''
@staticmethod
async def fetch_target(targetId) -> MessageSession:
async def fetch_target(targetId) -> FetchedSession:
"""
尝试从数据库记录的对象ID中取得对象消息会话实际此会话中的消息文本会被设为False因为本来就没有
"""
...
@staticmethod
async def fetch_target_list(targetList: list) -> List[MessageSession]:
async def fetch_target_list(targetList: list) -> List[FetchedSession]:
"""
尝试从数据库记录的对象ID中取得对象消息会话实际此会话中的消息文本会被设为False因为本来就没有
"""
...
@staticmethod
async def post_message(module_name, message, user_list: List[MessageSession] = None):
async def post_message(module_name, message, user_list: List[FetchedSession] = None):
"""
尝试向开启此模块的对象发送一条消息
"""
__all__ = ["FetchTarget", "MsgInfo", "MessageSession", "Session"]
__all__ = ["FetchTarget", "MsgInfo", "MessageSession", "Session", "FetchedSession"]