mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-18 18:50:05 +08:00
feat✨: PIX图库
This commit is contained in:
@@ -36,3 +36,11 @@ class InsufficientGold(Exception):
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class NotFindSuperuser(Exception):
|
||||
"""
|
||||
未找到超级用户
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import random
|
||||
from typing import Awaitable, Callable, Literal, Set
|
||||
|
||||
import httpx
|
||||
@@ -20,11 +21,13 @@ from nonebot_plugin_saa import (
|
||||
TargetQQPrivate,
|
||||
Text,
|
||||
)
|
||||
from nonebot_plugin_saa.abstract_factories import Receipt
|
||||
from pydantic import BaseModel
|
||||
|
||||
from zhenxun.models.friend_user import FriendUser
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.exception import NotFindSuperuser
|
||||
|
||||
|
||||
class UserData(BaseModel):
|
||||
@@ -47,6 +50,34 @@ class UserData(BaseModel):
|
||||
|
||||
class PlatformUtils:
|
||||
|
||||
@classmethod
|
||||
async def send_superuser(
|
||||
cls,
|
||||
bot: Bot,
|
||||
message: str | MessageFactory | Text | Image,
|
||||
superuser_id: str | None = None,
|
||||
) -> Receipt | None:
|
||||
"""发送消息给超级用户
|
||||
|
||||
参数:
|
||||
bot: Bot
|
||||
message: 消息
|
||||
superuser_id: 指定超级用户id.
|
||||
|
||||
异常:
|
||||
NotFindSuperuser: 未找到超级用户id
|
||||
|
||||
返回:
|
||||
Receipt | None: Receipt
|
||||
"""
|
||||
if not superuser_id:
|
||||
platform = cls.get_platform(bot)
|
||||
platform_superusers = bot.config.PLATFORM_SUPERUSERS.get(platform) or []
|
||||
if not platform_superusers:
|
||||
raise NotFindSuperuser()
|
||||
superuser_id = random.choice(platform_superusers)
|
||||
return await cls.send_message(bot, superuser_id, None, message)
|
||||
|
||||
@classmethod
|
||||
async def get_group_member_list(cls, bot: Bot, group_id: str) -> list[UserData]:
|
||||
"""获取群组/频道成员列表
|
||||
@@ -277,7 +308,7 @@ class PlatformUtils:
|
||||
user_id: str | None,
|
||||
group_id: str | None,
|
||||
message: str | Text | MessageFactory | Image,
|
||||
) -> bool:
|
||||
) -> Receipt | None:
|
||||
"""发送消息
|
||||
|
||||
参数:
|
||||
@@ -287,13 +318,12 @@ class PlatformUtils:
|
||||
message: 消息文本
|
||||
|
||||
返回:
|
||||
bool: 是否发送成功
|
||||
Receipt | None: 是否发送成功
|
||||
"""
|
||||
if target := cls.get_target(bot, user_id, group_id):
|
||||
send_message = Text(message) if isinstance(message, str) else message
|
||||
await send_message.send_to(target, bot)
|
||||
return True
|
||||
return False
|
||||
return await send_message.send_to(target, bot)
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
async def update_group(cls, bot: Bot) -> int:
|
||||
@@ -581,7 +611,10 @@ async def broadcast_group(
|
||||
if is_continue:
|
||||
continue
|
||||
target = PlatformUtils.get_target(
|
||||
_bot, None, group.group_id, group.channel_id
|
||||
_bot,
|
||||
None,
|
||||
group.group_id,
|
||||
# , group.channel_id
|
||||
)
|
||||
if target:
|
||||
_used_group.append(key)
|
||||
|
||||
@@ -9,6 +9,7 @@ import httpx
|
||||
import pypinyin
|
||||
import pytz
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
|
||||
@@ -165,3 +166,50 @@ async def get_group_avatar(gid: int | str) -> bytes | None:
|
||||
except Exception as e:
|
||||
logger.error("获取群头像错误", "Util", target=gid)
|
||||
return None
|
||||
|
||||
|
||||
def change_pixiv_image_links(
|
||||
url: str, size: str | None = None, nginx_url: str | None = None
|
||||
) -> str:
|
||||
"""根据配置改变图片大小和反代链接
|
||||
|
||||
参数:
|
||||
url: 图片原图链接
|
||||
size: 模式
|
||||
nginx_url: 反代
|
||||
|
||||
返回:
|
||||
str: url
|
||||
"""
|
||||
if size == "master":
|
||||
img_sp = url.rsplit(".", maxsplit=1)
|
||||
url = img_sp[0]
|
||||
img_type = img_sp[1]
|
||||
url = url.replace("original", "master") + f"_master1200.{img_type}"
|
||||
if not nginx_url:
|
||||
nginx_url = Config.get_config("pixiv", "PIXIV_NGINX_URL")
|
||||
if nginx_url:
|
||||
url = (
|
||||
url.replace("i.pximg.net", nginx_url)
|
||||
.replace("i.pixiv.cat", nginx_url)
|
||||
.replace("_webp", "")
|
||||
)
|
||||
return url
|
||||
|
||||
|
||||
def change_img_md5(path_file: str | Path) -> bool:
|
||||
"""改变图片MD5
|
||||
|
||||
参数:
|
||||
path_file: 图片路径
|
||||
|
||||
返还:
|
||||
bool: 是否修改成功
|
||||
"""
|
||||
try:
|
||||
with open(path_file, "a") as f:
|
||||
f.write(str(int(time.time() * 1000)))
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"改变图片MD5错误 Path:{path_file}", e=e)
|
||||
return False
|
||||
|
||||
@@ -64,7 +64,11 @@ class WithdrawManager:
|
||||
|
||||
@classmethod
|
||||
async def withdraw_message(
|
||||
cls, bot: Bot, message_id: str | int, time: int | None = None
|
||||
cls,
|
||||
bot: Bot,
|
||||
message_id: str | int,
|
||||
time: int | tuple[int, int] | None = None,
|
||||
session: EventSession | None = None,
|
||||
):
|
||||
"""消息撤回
|
||||
|
||||
@@ -74,8 +78,24 @@ class WithdrawManager:
|
||||
time: 延迟时间
|
||||
"""
|
||||
if time:
|
||||
logger.debug(f"将在 {time}秒 内撤回消息ID: {message_id}", "WithdrawManager")
|
||||
await asyncio.sleep(time)
|
||||
gid = None
|
||||
_time = 1
|
||||
if isinstance(time, tuple):
|
||||
if time[0] == 0:
|
||||
return
|
||||
if session:
|
||||
gid = session.id3 or session.id2
|
||||
if not gid and int(time[1]) not in [0, 2]:
|
||||
return
|
||||
if gid and int(time[1]) not in [1, 2]:
|
||||
return
|
||||
_time = time[0]
|
||||
else:
|
||||
_time = time
|
||||
logger.debug(
|
||||
f"将在 {_time}秒 内撤回消息ID: {message_id}", "WithdrawManager"
|
||||
)
|
||||
await asyncio.sleep(_time)
|
||||
if isinstance(bot, v11Bot):
|
||||
logger.debug(f"v11Bot 撤回消息ID: {message_id}", "WithdrawManager")
|
||||
await bot.delete_msg(message_id=int(message_id))
|
||||
|
||||
Reference in New Issue
Block a user