Archived
1
0
Fork 0
This commit is contained in:
yzhh 2023-08-27 22:51:16 +08:00
parent cdb051b5be
commit b7ad50082f
18 changed files with 146 additions and 121 deletions

View file

@ -6,7 +6,7 @@ import ujson as json
from aiocqhttp import Event, Error
from bots.aiocqhttp.client import bot
from bots.aiocqhttp.client import bot, client_name
from bots.aiocqhttp.message import MessageSession, FetchTarget
from config import Config
from core.builtins import EnableDirtyWordCheck, PrivateAssets, Url
@ -67,7 +67,7 @@ async def message_handler(event: Event):
msg = MessageSession(MsgInfo(targetId=targetId,
senderId=f'QQ|{str(event.user_id)}',
targetFrom='QQ|Group' if event.detail_type == 'group' else 'QQ',
senderFrom='QQ', senderName=event.sender['nickname'], clientName='QQ',
senderFrom='QQ', senderName=event.sender['nickname'], clientName=client_name,
messageId=event.message_id,
replyId=replyId),
Session(message=event,
@ -107,7 +107,7 @@ async def _(event):
msg = MessageSession(MsgInfo(targetId=targetId,
senderId=f'QQ|Tiny|{str(event.user_id)}',
targetFrom='QQ|Guild',
senderFrom='QQ|Tiny', senderName=event.sender['nickname'], clientName='QQ',
senderFrom='QQ|Tiny', senderName=event.sender['nickname'], clientName=client_name,
messageId=event.message_id,
replyId=replyId),
Session(message=event,

View file

@ -50,3 +50,4 @@ class CQHttpModded(CQHttp):
bot = CQHttpModded()
client_name = 'QQ'

View file

@ -11,15 +11,14 @@ import aiocqhttp.exceptions
import ujson as json
from aiocqhttp import MessageSegment
from bots.aiocqhttp.client import bot
from bots.aiocqhttp.client import bot, client_name
from config import Config
from core.builtins import Bot, ErrorMessage, base_superuser_list
from core.builtins import Plain, Image, Voice, Temp, command_prefix
from core.builtins.message import MessageSession as MS
from core.builtins.message.chain import MessageChain
from core.logger import Logger
from core.types import MsgInfo, Session, FetchTarget as FT, \
FetchedSession as FS, FinishedSession as FinS
from core.types import MsgInfo, Session, FetchTarget as FT, FinishedSession as FinS
from core.utils.image import msgchain2image
from database import BotDBUtil
from core.utils.storedata import get_stored_list
@ -242,32 +241,26 @@ 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='',
clientName='QQ',
messageId=0,
replyId=None)
self.session = Session(message=False, target=targetId, sender=targetId)
self.parent = MessageSession(self.target, self.session)
class FetchTarget(FT):
name = 'QQ'
name = client_name
@staticmethod
async def fetch_target(targetId) -> Union[FetchedSession, bool]:
async def fetch_target(targetId, senderId=None) -> Union[Bot.FetchedSession]:
matchTarget = re.match(r'^(QQ\|Group|QQ\|Guild|QQ)\|(.*)', targetId)
if matchTarget:
return FetchedSession(matchTarget.group(1), matchTarget.group(2))
return False
targetFrom = senderFrom = matchTarget.group(1)
if senderId:
matchSender = re.match(r'^(QQ\|Tiny|QQ)\|(.*)', senderId)
if matchSender:
senderFrom = matchSender.group(1)
senderId = matchSender.group(2)
else:
targetId = senderId = matchTarget.group(2)
return Bot.FetchedSession(targetFrom, targetId, senderFrom, senderId)
@staticmethod
async def fetch_target_list(targetList: list) -> List[FetchedSession]:
async def fetch_target_list(targetList: list) -> List[Bot.FetchedSession]:
lst = []
group_list_raw = await bot.call_action('get_group_list')
group_list = []
@ -300,11 +293,11 @@ class FetchTarget(FT):
return lst
@staticmethod
async def post_message(module_name, message, user_list: List[FetchedSession] = None, i18n=False, **kwargs):
async def post_message(module_name, message, user_list: List[Bot.FetchedSession] = None, i18n=False, **kwargs):
_tsk = []
blocked = False
async def post_(fetch_: FetchedSession):
async def post_(fetch_: Bot.FetchedSession):
nonlocal _tsk
nonlocal blocked
try:
@ -410,3 +403,4 @@ class FetchTarget(FT):
Bot.MessageSession = MessageSession
Bot.FetchTarget = FetchTarget
Bot.client_name = client_name

View file

@ -3,7 +3,7 @@ import os
from aiogram import types, executor
from aiogram.types import ContentType
from bots.aiogram.client import dp
from bots.aiogram.client import dp, client_name
from bots.aiogram.message import MessageSession, FetchTarget
from core.builtins import PrivateAssets, Url
from core.parser.message import parser
@ -25,7 +25,7 @@ if dp:
senderId=f'Telegram|User|{message.from_user.id}',
targetFrom=f'Telegram|{message.chat.type}',
senderFrom='Telegram|User', senderName=message.from_user.username,
clientName='Telegram',
clientName=client_name,
messageId=message.message_id,
replyId=replyId),
Session(message=message, target=message.chat.id, sender=message.from_user.id))

View file

@ -3,6 +3,7 @@ from aiogram import Bot, Dispatcher
from config import Config
token = Config('tg_token')
client_name = 'Telegram'
if token:

View file

@ -2,12 +2,12 @@ import re
import traceback
from typing import List, Union
from bots.aiogram.client import dp, bot, token
from bots.aiogram.client import dp, bot, token, client_name
from config import Config
from core.builtins import Bot, Plain, Image, Voice, MessageSession as MS, ErrorMessage
from core.builtins.message.chain import MessageChain
from core.logger import Logger
from core.types import MsgInfo, Session, FetchTarget as FT, FetchedSession as FS, \
from core.types import MsgInfo, Session, FetchTarget as FT,\
FinishedSession as FinS
from core.utils.image import image_split
from database import BotDBUtil
@ -142,31 +142,27 @@ 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='',
clientName='Telegram', messageId=0, replyId=None)
self.session = Session(message=False, target=targetId, sender=targetId)
self.parent = MessageSession(self.target, self.session)
class FetchTarget(FT):
name = 'Telegram'
name = client_name
@staticmethod
async def fetch_target(targetId) -> Union[FetchedSession, bool]:
async def fetch_target(targetId, senderId=None) -> Union[Bot.FetchedSession]:
matchChannel = re.match(r'^(Telegram\|.*?)\|(.*)', targetId)
if matchChannel:
return FetchedSession(matchChannel.group(1), matchChannel.group(2))
else:
return False
targetFrom = senderFrom = matchChannel.group(1)
if senderId:
matchSender = re.match(r'^(Telegram\|User)\|(.*)', senderId)
if matchSender:
senderFrom = matchSender.group(1)
senderId = matchSender.group(2)
else:
targetId = senderId = matchChannel.group(2)
return Bot.FetchedSession(targetFrom, targetId, senderFrom, senderId)
@staticmethod
async def fetch_target_list(targetList: list) -> List[FetchedSession]:
async def fetch_target_list(targetList: list) -> List[Bot.FetchedSession]:
lst = []
for x in targetList:
fet = await FetchTarget.fetch_target(x)
@ -175,7 +171,7 @@ class FetchTarget(FT):
return lst
@staticmethod
async def post_message(module_name, message, user_list: List[FetchedSession] = None, i18n=False, **kwargs):
async def post_message(module_name, message, user_list: List[Bot.FetchedSession] = None, i18n=False, **kwargs):
if user_list is not None:
for x in user_list:
try:
@ -207,3 +203,4 @@ class FetchTarget(FT):
Bot.MessageSession = MessageSession
Bot.FetchTarget = FetchTarget
Bot.client_name = client_name

View file

@ -5,7 +5,7 @@ import traceback
import discord
from bots.discord.client import client
from bots.discord.client import client, client_name
from bots.discord.message import MessageSession, FetchTarget
from config import Config
from core.builtins import PrivateAssets, Url
@ -82,7 +82,7 @@ if dc_token:
senderName=message.author.name,
targetFrom=target,
senderFrom="Discord|Client",
clientName='Discord',
clientName=client_name,
messageId=message.id,
replyId=replyId),
session=Session(

View file

@ -3,3 +3,4 @@ import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Bot(intents=intents)
client_name = 'Discord'

View file

@ -6,14 +6,13 @@ from typing import List, Union
import discord
import filetype
from bots.discord.client import client
from bots.discord.client import client, client_name
from config import Config
from core.builtins import Bot, Plain, Image, MessageSession as MS
from core.builtins.message.chain import MessageChain
from core.builtins.message.internal import Embed, ErrorMessage, Voice
from core.logger import Logger
from core.types import MsgInfo, Session, FetchTarget as FT, \
FetchedSession as FS, FinishedSession as FinS
from core.types import MsgInfo, Session, FetchTarget as FT, FinishedSession as FinS
from core.utils.http import download_to_cache
from database import BotDBUtil
@ -158,15 +157,7 @@ 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='', clientName='Discord', messageId=0, replyId=None)
self.session = Session(message=False, target=targetId, sender=targetId)
self.parent = MessageSession(self.target, self.session)
class FetchedSession(Bot.FetchedSession):
async def sendDirectMessage(self, msgchain, disable_secret_check=False, allow_split_image=True):
try:
@ -178,19 +169,29 @@ class FetchedSession(FS):
return await self.parent.sendDirectMessage(msgchain, disable_secret_check=disable_secret_check)
Bot.FetchedSession = FetchedSession
class FetchTarget(FT):
name = 'Discord'
name = client_name
@staticmethod
async def fetch_target(targetId) -> Union[FetchedSession, bool]:
async def fetch_target(targetId, senderId=None) -> Union[Bot.FetchedSession]:
matchChannel = re.match(r'^(Discord\|(?:DM\||)Channel)\|(.*)', targetId)
if matchChannel:
return FetchedSession(matchChannel.group(1), matchChannel.group(2))
else:
return False
targetFrom = senderFrom = matchChannel.group(1)
if senderId:
matchSender = re.match(r'^(Discord\|Client)\|(.*)', senderId)
if matchSender:
senderFrom = matchSender.group(1)
senderId = matchSender.group(2)
else:
targetId = senderId = matchChannel.group(2)
return Bot.FetchedSession(targetFrom, targetId, senderFrom, senderId)
@staticmethod
async def fetch_target_list(targetList: list) -> List[FetchedSession]:
async def fetch_target_list(targetList: list) -> List[Bot.FetchedSession]:
lst = []
for x in targetList:
fet = await FetchTarget.fetch_target(x)
@ -199,7 +200,7 @@ class FetchTarget(FT):
return lst
@staticmethod
async def post_message(module_name, message, user_list: List[FetchedSession] = None, i18n=False, **kwargs):
async def post_message(module_name, message, user_list: List[Bot.FetchedSession] = None, i18n=False, **kwargs):
if user_list is not None:
for x in user_list:
try:

View file

@ -1,6 +1,6 @@
import os
from bots.kook.client import bot
from bots.kook.client import bot, client_name
from khl import Message, MessageTypes
from config import Config
@ -32,7 +32,7 @@ if bot:
senderId=f'Kook|User|{message.author_id}',
targetFrom=f'Kook|{message.channel_type.name}',
senderFrom='Kook|User', senderName=message.author.nickname,
clientName='Kook',
clientName=client_name,
messageId=message.id,
replyId=replyId),
Session(message=message, target=message.target_id, sender=message.author_id))

View file

@ -3,7 +3,7 @@ from khl import Bot
from config import Config
token = Config('kook_token')
client_name = 'Kook'
if token:
bot = Bot(token=token)

View file

@ -6,12 +6,12 @@ from typing import List, Union
import aiohttp
from khl import MessageTypes, Message
from bots.kook.client import bot
from bots.kook.client import bot, client_name
from config import Config
from core.builtins import Bot, Plain, Image, Voice, MessageSession as MS, ErrorMessage
from core.builtins.message.chain import MessageChain
from core.logger import Logger
from core.types import MsgInfo, Session, FetchTarget as FT, FetchedSession as FS, \
from core.types import MsgInfo, Session, FetchTarget as FT, \
FinishedSession as FinS
from core.utils.image import image_split
from database import BotDBUtil
@ -163,16 +163,7 @@ 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='',
clientName='Kook', messageId=0, replyId=None)
self.session = Session(message=False, target=targetId, sender=targetId)
self.parent = MessageSession(self.target, self.session)
class FetchedSession(Bot.FetchedSession):
async def sendDirectMessage(self, msgchain, disable_secret_check=False, allow_split_image=True):
if self.target.targetFrom == 'Kook|GROUP':
@ -204,19 +195,29 @@ class FetchedSession(FS):
Logger.info(f'[Bot] -> [{self.target.targetId}]: Voice: {str(x.__dict__)}')
Bot.FetchedSession = FetchedSession
class FetchTarget(FT):
name = 'Kook'
name = client_name
@staticmethod
async def fetch_target(targetId) -> Union[FetchedSession, bool]:
async def fetch_target(targetId, senderId=None) -> Union[Bot.FetchedSession]:
matchChannel = re.match(r'^(Kook\|.*?)\|(.*)', targetId)
if matchChannel:
return FetchedSession(matchChannel.group(1), matchChannel.group(2))
else:
return False
targetFrom = senderFrom = matchChannel.group(1)
if senderId:
matchSender = re.match(r'^(Kook\|User)\|(.*)', senderId)
if matchSender:
senderFrom = matchSender.group(1)
senderId = matchSender.group(2)
else:
targetId = senderId = matchChannel.group(2)
return Bot.FetchedSession(targetFrom, targetId, senderFrom, senderId)
@staticmethod
async def fetch_target_list(targetList: list) -> List[FetchedSession]:
async def fetch_target_list(targetList: list) -> List[Bot.FetchedSession]:
lst = []
for x in targetList:
fet = await FetchTarget.fetch_target(x)
@ -225,7 +226,7 @@ class FetchTarget(FT):
return lst
@staticmethod
async def post_message(module_name, message, user_list: List[FetchedSession] = None, i18n=False, **kwargs):
async def post_message(module_name, message, user_list: List[Bot.FetchedSession] = None, i18n=False, **kwargs):
if user_list is not None:
for x in user_list:
try:

View file

@ -3,7 +3,7 @@ import os
from tracemalloc import start
from bots.matrix import client
from bots.matrix.client import bot
from bots.matrix.client import bot, client_name
import nio
from core.builtins import PrivateAssets, Url
@ -62,7 +62,7 @@ async def on_message(room: nio.MatrixRoom, event: nio.RoomMessageFormatted):
targetFrom=f'Matrix',
senderFrom='Matrix',
senderName=senderName,
clientName='Matrix',
clientName=client_name,
messageId=event.event_id,
replyId=replyId),
Session(message=event.source, target=room.room_id, sender=event.sender))

View file

@ -5,6 +5,8 @@ import urllib3
from config import Config
from core.logger import Logger
client_name = 'Matrix'
homeserver: str = Config('matrix_homeserver')
user: str = Config('matrix_user')
token: str = Config('matrix_token')

View file

@ -5,12 +5,12 @@ import re
import traceback
from typing import List, Union
from bots.matrix.client import bot, homeserver_host
from bots.matrix.client import bot, homeserver_host, client_name
from config import Config
from core.builtins import Bot, Plain, Image, Voice, MessageSession as MS, ErrorMessage
from core.builtins.message.chain import MessageChain
from core.logger import Logger
from core.types import MsgInfo, Session, FetchTarget as FT, FetchedSession as FS, \
from core.types import MsgInfo, Session, FetchTarget as FT, \
FinishedSession as FinS
from core.utils.image import image_split
from database import BotDBUtil
@ -215,16 +215,7 @@ 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='',
clientName='Matrix', messageId=None, replyId=None)
self.session = Session(message=False, target=targetId, sender=targetId)
self.parent = MessageSession(self.target, self.session)
class FetchedSession(Bot.FetchedSession):
async def _resolve_matrix_room_(self):
targetId: str = self.session.target
@ -252,18 +243,27 @@ class FetchedSession(FS):
self.session.target = room
Bot.FetchedSession = FetchedSession
class FetchTarget(FT):
name = 'Matrix'
name = client_name
@staticmethod
async def fetch_target(targetId) -> Union[FetchedSession, bool]:
async def fetch_target(targetId, senderId=None) -> Union[FetchedSession]:
matchChannel = re.match(r'^(Matrix)\|(.*)', targetId)
if matchChannel:
session = FetchedSession(matchChannel.group(1), matchChannel.group(2))
targetFrom = senderFrom = matchChannel.group(1)
if senderId:
matchSender = re.match(r'^(Matrix)\|(.*)', senderId)
if matchSender:
senderFrom = matchSender.group(1)
senderId = matchSender.group(2)
else:
targetId = senderId = matchChannel.group(2)
session = Bot.FetchedSession(targetFrom, targetId, senderFrom, senderId)
await session._resolve_matrix_room_()
return session
else:
return False
@staticmethod
async def fetch_target_list(targetList: list) -> List[FetchedSession]:

View file

@ -1,7 +1,7 @@
from typing import Union, List
from config import Config
from core.types.message import FetchTarget, FetchedSession
from core.types.message import FetchTarget, FetchedSession as FS, MsgInfo, Session
from database import BotDBUtil
from .message import *
from .message.chain import *
@ -14,9 +14,11 @@ from .utils import *
class Bot:
MessageSession = MessageSession
FetchTarget = FetchTarget
client_name = FetchTarget.name
FetchedSession = FS
@staticmethod
async def sendMessage(target: Union[FetchedSession, MessageSession, str], msg: Union[MessageChain, list],
async def sendMessage(target: Union[FS, MessageSession, str], msg: Union[MessageChain, list],
disable_secret_check=False,
allow_split_image=True):
if isinstance(target, str):
@ -32,7 +34,7 @@ class Bot:
return Bot.FetchTarget.fetch_target(target)
@staticmethod
async def get_enabled_this_module(module: str) -> List[FetchedSession]:
async def get_enabled_this_module(module: str) -> List[FS]:
lst = BotDBUtil.TargetInfo.get_enabled_this(module)
fetched = []
for x in lst:
@ -42,6 +44,27 @@ class Bot:
return fetched
class FetchedSession(FS):
def __init__(self, targetFrom, targetId, senderFrom=None, senderId=None):
if senderFrom is None:
senderFrom = targetFrom
if senderId is None:
senderId = targetId
self.target = MsgInfo(targetId=f'{targetFrom}|{targetId}',
senderId=f'{targetFrom}|{senderId}',
targetFrom=targetFrom,
senderFrom=senderFrom,
senderName='',
clientName=Bot.client_name,
messageId=0,
replyId=None)
self.session = Session(message=False, target=targetId, sender=senderId)
self.parent = Bot.MessageSession(self.target, self.session)
Bot.FetchedSession = FetchedSession
base_superuser_list = Config("base_superuser")
if isinstance(base_superuser_list, str):

View file

@ -264,13 +264,17 @@ class MessageSession:
class FetchedSession:
def __init__(self, targetFrom, targetId):
def __init__(self, targetFrom, targetId, senderFrom=None, senderId=None):
if senderFrom is None:
senderFrom = targetFrom
if senderId is None:
senderId = targetId
self.target = MsgInfo(targetId=f'{targetFrom}|{targetId}',
senderId=f'{targetFrom}|{targetId}',
senderId=f'{targetFrom}|{senderId}',
targetFrom=targetFrom,
senderFrom=targetFrom,
senderFrom=senderFrom,
senderName='', clientName='', replyId=None, messageId=0)
self.session = Session(message=False, target=targetId, sender=targetId)
self.session = Session(message=False, target=targetId, sender=senderId)
self.parent = MessageSession(self.target, self.session)
async def sendDirectMessage(self, msgchain, disable_secret_check=False, allow_split_image=True):

View file

@ -79,8 +79,8 @@ class UnfriendlyActionsTable(Base):
class JobQueueTable(Base):
__tablename__ = "job_queue"
taskid = Column(String(512))
__tablename__ = "job_queues"
taskid = Column(String(512), primary_key=True)
targetClient = Column(String(512))
hasDone = Column(Boolean, default=False)
action = Column(String(512))