zhenxun_bot/zhenxun/builtin_plugins/superuser/broadcast/_data_source.py

75 lines
2.8 KiB
Python
Raw Normal View History

2024-02-25 03:18:34 +08:00
from nonebot.adapters import Bot
import nonebot_plugin_alconna as alc
2024-08-11 15:57:33 +08:00
# from nonebot.adapters.discord import Bot as DiscordBot
# from nonebot.adapters.dodo import Bot as DodoBot
# from nonebot.adapters.kaiheila import Bot as KaiheilaBot
# from nonebot.adapters.onebot.v11 import Bot as v11Bot
# from nonebot.adapters.onebot.v12 import Bot as v12Bot
from nonebot_plugin_alconna import Image, UniMsg
from nonebot_plugin_session import EventSession
2024-02-25 03:18:34 +08:00
from zhenxun.services.log import logger
from zhenxun.utils.common_utils import CommonUtils
2024-08-11 15:57:33 +08:00
from zhenxun.utils.message import MessageUtils
2024-03-27 20:09:30 +08:00
from zhenxun.utils.platform import PlatformUtils
2024-02-25 03:18:34 +08:00
class BroadcastManage:
@classmethod
async def send(
cls, bot: Bot, message: UniMsg, session: EventSession
) -> tuple[int, int]:
"""发送广播消息
参数:
bot: Bot
message: 消息内容
session: Session
返回:
tuple[int, int]: 发送成功的群组数量, 发送失败的群组数量
"""
message_list = []
for msg in message:
if isinstance(msg, alc.Image) and msg.url:
2024-08-11 15:57:33 +08:00
message_list.append(Image(url=msg.url))
2024-02-25 03:18:34 +08:00
elif isinstance(msg, alc.Text):
2024-08-11 15:57:33 +08:00
message_list.append(msg.text)
2024-03-27 20:09:30 +08:00
group_list, _ = await PlatformUtils.get_group_list(bot)
if group_list:
2024-02-25 03:18:34 +08:00
error_count = 0
for group in group_list:
try:
2024-08-30 02:45:27 +08:00
if not await CommonUtils.task_is_block(
2024-10-18 18:57:55 +08:00
bot,
2024-08-03 00:34:19 +08:00
"broadcast", # group.channel_id
2024-10-18 18:57:55 +08:00
group.group_id,
2024-02-25 03:18:34 +08:00
):
2024-03-27 20:09:30 +08:00
target = PlatformUtils.get_target(
group_id=group.group_id, channel_id=group.channel_id
2024-02-25 03:18:34 +08:00
)
if target:
2024-08-11 15:57:33 +08:00
await MessageUtils.build_message(message_list).send(
target, bot
)
logger.debug(
"发送成功",
"广播",
session=session,
target=f"{group.group_id}:{group.channel_id}",
)
else:
logger.warning("target为空", "广播", session=session)
2024-02-25 03:18:34 +08:00
except Exception as e:
error_count += 1
logger.error(
"发送失败",
"广播",
session=session,
target=f"{group.group_id}:{group.channel_id}",
e=e,
)
return len(group_list) - error_count, error_count
return 0, 0