mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-08 21:30:01 +08:00
perf👌: 广播与全局插件/被动管理
This commit is contained in:
@@ -9,23 +9,13 @@ from nonebot_plugin_alconna import UniMsg
|
||||
from nonebot_plugin_saa import (
|
||||
Image,
|
||||
MessageFactory,
|
||||
TargetDoDoChannel,
|
||||
TargetQQGroup,
|
||||
Text,
|
||||
)
|
||||
from nonebot_plugin_session import EventSession
|
||||
from pydantic import BaseModel
|
||||
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
|
||||
class GroupChannel(BaseModel):
|
||||
|
||||
group_id: str
|
||||
"""群组id"""
|
||||
channel_id: str | None = None
|
||||
"""频道id"""
|
||||
from zhenxun.utils.platform import PlatformManage
|
||||
|
||||
|
||||
class BroadcastManage:
|
||||
@@ -50,24 +40,27 @@ class BroadcastManage:
|
||||
message_list.append(Image(msg.url))
|
||||
elif isinstance(msg, alc.Text):
|
||||
message_list.append(Text(msg.text))
|
||||
if group_list := await cls.__get_group_list(bot):
|
||||
group_list, _ = await PlatformManage.get_group_list(bot)
|
||||
if group_list:
|
||||
error_count = 0
|
||||
for group in group_list:
|
||||
try:
|
||||
if not await GroupConsole.is_block_task(
|
||||
group.group_id, "broadcast", group.channel_id
|
||||
):
|
||||
if isinstance(bot, (v11Bot, v12Bot)):
|
||||
target = TargetQQGroup(group_id=int(group.group_id))
|
||||
elif isinstance(bot, DodoBot):
|
||||
target = TargetDoDoChannel(channel_id=group.channel_id) # type: ignore
|
||||
await MessageFactory(message_list).send_to(target, bot)
|
||||
logger.debug(
|
||||
"发送成功",
|
||||
"广播",
|
||||
session=session,
|
||||
target=f"{group.group_id}:{group.channel_id}",
|
||||
target = PlatformManage.get_target(
|
||||
bot, group.group_id, group.channel_id
|
||||
)
|
||||
if target:
|
||||
await MessageFactory(message_list).send_to(target, bot)
|
||||
logger.debug(
|
||||
"发送成功",
|
||||
"广播",
|
||||
session=session,
|
||||
target=f"{group.group_id}:{group.channel_id}",
|
||||
)
|
||||
else:
|
||||
logger.warning("target为空", "广播", session=session)
|
||||
except Exception as e:
|
||||
error_count += 1
|
||||
logger.error(
|
||||
@@ -79,40 +72,3 @@ class BroadcastManage:
|
||||
)
|
||||
return len(group_list) - error_count, error_count
|
||||
return 0, 0
|
||||
|
||||
@classmethod
|
||||
async def __get_group_list(cls, bot: Bot) -> list[GroupChannel]:
|
||||
"""获取群组id列表
|
||||
|
||||
参数:
|
||||
bot: Bot
|
||||
|
||||
返回:
|
||||
list[str]: 群组id列表
|
||||
"""
|
||||
if isinstance(bot, (v11Bot, v12Bot)):
|
||||
group_list = await bot.get_group_list()
|
||||
return [GroupChannel(group_id=str(g["group_id"])) for g in group_list]
|
||||
if isinstance(bot, DodoBot):
|
||||
island_list = await bot.get_island_list()
|
||||
source_id_list = [
|
||||
g.island_source_id for g in island_list if g.island_source_id
|
||||
]
|
||||
channel_id_list = []
|
||||
for id in source_id_list:
|
||||
channel_list = await bot.get_channel_list(island_source_id=id)
|
||||
channel_id_list += [
|
||||
GroupChannel(group_id=id, channel_id=c.channel_id)
|
||||
for c in channel_list
|
||||
]
|
||||
return channel_id_list
|
||||
if isinstance(bot, KaiheilaBot):
|
||||
# TODO: kaiheila获取群组列表
|
||||
pass
|
||||
# group_list = await bot.guild_list()
|
||||
# if group_list.guilds:
|
||||
# return [g.open_id for g in group_list.guilds if g.open_id]
|
||||
if isinstance(bot, DiscordBot):
|
||||
# TODO: discord获取群组列表
|
||||
pass
|
||||
return []
|
||||
|
||||
Reference in New Issue
Block a user