feat✨: 金币红包

This commit is contained in:
HibiKier
2024-05-04 13:48:12 +08:00
parent afc1dd7377
commit 3fad6fc2ad
12 changed files with 1230 additions and 78 deletions
+80 -1
View File
@@ -1,7 +1,35 @@
from typing import Any
from nonebot.internal.params import Depends
from nonebot.matcher import Matcher
from nonebot.params import Command
from nonebot_plugin_saa import Text
from nonebot_plugin_session import EventSession
from nonebot_plugin_userinfo import EventUserInfo, UserInfo
from zhenxun.configs.config import Config
def CheckUg(check_user: bool = True, check_group: bool = True):
"""检测群组id和用户id是否存在
参数:
check_user: 检查用户id.
check_group: 检查群组id.
"""
async def dependency(session: EventSession):
if check_user:
user_id = session.id1
if not user_id:
await Text("用户id为空").finish()
if check_group:
group_id = session.id3 or session.id2
if not group_id:
await Text("群组id为空").finish()
return Depends(dependency)
def OneCommand():
"""
@@ -24,6 +52,57 @@ def UserName():
async def dependency(user_info: UserInfo = EventUserInfo()):
return (
user_info.user_displayname or user_info.user_remark or user_info.user_name
)
) or ""
return Depends(dependency)
def GetConfig(
module: str | None = None,
config: str = "",
default_value: Any = None,
prompt: str | None = None,
):
"""获取配置项
参数:
module: 模块名,为空时默认使用当前插件模块名
config: 配置项名称
default_value: 默认值
prompt: 为空时提示
"""
async def dependency(matcher: Matcher):
module_ = module or matcher.plugin_name
if module_:
value = Config.get_config(module_, config, default_value)
if value is None and prompt:
# await matcher.finish(prompt or f"配置项 {config} 未填写!")
await matcher.finish(prompt)
return value
return Depends(dependency)
def CheckConfig(
module: str | None = None,
config: str | list[str] = "",
prompt: str | None = None,
):
"""检测配置项在配置文件中是否填写
参数:
module: 模块名,为空时默认使用当前插件模块名
config: 需要检查的配置项名称
prompt: 为空时提示
"""
async def dependency(matcher: Matcher):
module_ = module or matcher.plugin_name
if module_:
config_list = [config] if isinstance(config, str) else config
for c in config_list:
if Config.get_config(module_, c) is None:
await matcher.finish(prompt or f"配置项 {c} 未填写!")
return Depends(dependency)
+1 -1
View File
@@ -88,7 +88,7 @@ class RequestType(StrEnum):
class RequestHandleType(StrEnum):
"""
请求类型
请求处理类型
"""
APPROVE = "APPROVE"
+51 -3
View File
@@ -1,5 +1,6 @@
from typing import Awaitable, Callable, Literal, Set
import httpx
import nonebot
from nonebot.adapters import Bot
from nonebot.adapters.discord import Bot as DiscordBot
@@ -27,6 +28,53 @@ from zhenxun.services.log import logger
class PlatformUtils:
@classmethod
async def get_user_avatar(cls, user_id: str, platform: str) -> bytes | None:
"""快捷获取用户头像
参数:
user_id: 用户id
platform: 平台
"""
if platform == "qq":
url = f"http://q1.qlogo.cn/g?b=qq&nk={user_id}&s=160"
async with httpx.AsyncClient() as client:
for _ in range(3):
try:
return (await client.get(url)).content
except Exception as e:
logger.error(
"获取用户头像错误",
"Util",
target=user_id,
platform=platform,
)
else:
pass
return None
@classmethod
async def get_group_avatar(cls, gid: str, platform: str) -> bytes | None:
"""快捷获取用群头像
参数:
gid: 群组id
platform: 平台
"""
if platform == "qq":
url = f"http://p.qlogo.cn/gh/{gid}/{gid}/640/"
async with httpx.AsyncClient() as client:
for _ in range(3):
try:
return (await client.get(url)).content
except Exception as e:
logger.error(
"获取群头像错误", "Util", target=gid, platform=platform
)
else:
pass
return None
@classmethod
async def send_message(
cls,
@@ -109,7 +157,7 @@ class PlatformUtils:
bot: Bot
返回:
list[GroupConsole]: 群组列表
tuple[list[GroupConsole], str]: 群组列表, 平台
"""
if isinstance(bot, v11Bot):
group_list = await bot.get_group_list()
@@ -239,8 +287,8 @@ class PlatformUtils:
参数:
bot: Bot
group_id: 群组id
channel_id: 频道id或群组id
user_id: 用户id
group_id: 频道id或群组id
返回:
target: 对应平台Target
+2 -3
View File
@@ -99,8 +99,7 @@ class UserBlockLimiter:
def check(self, key: Any) -> bool:
if time.time() - self.time > 30:
self.set_false(key)
return False
return self.flag_data[key]
return not self.flag_data[key]
class FreqLimiter:
@@ -156,7 +155,7 @@ async def get_group_avatar(gid: int | str) -> bytes | None:
"""快捷获取用群头像
参数:
:param gid: 群号
gid: 群号
"""
url = f"http://p.qlogo.cn/gh/{gid}/{gid}/640/"
async with httpx.AsyncClient() as client: