🐛 修复使用道具错误

This commit is contained in:
HibiKier 2024-12-21 16:25:49 +08:00
parent ccf043d670
commit 6f16138e7d
3 changed files with 51 additions and 48 deletions

View File

@ -14,7 +14,6 @@ from nonebot_plugin_alconna import (
on_alconna, on_alconna,
store_true, store_true,
) )
from nonebot_plugin_session import EventSession
from nonebot_plugin_uninfo import Uninfo from nonebot_plugin_uninfo import Uninfo
from zhenxun.configs.utils import BaseBlock, PluginExtraData from zhenxun.configs.utils import BaseBlock, PluginExtraData
@ -23,6 +22,7 @@ from zhenxun.utils.depends import UserName
from zhenxun.utils.enum import BlockType, PluginType from zhenxun.utils.enum import BlockType, PluginType
from zhenxun.utils.exception import GoodsNotFound from zhenxun.utils.exception import GoodsNotFound
from zhenxun.utils.message import MessageUtils from zhenxun.utils.message import MessageUtils
from zhenxun.utils.platform import PlatformUtils
from ._data_source import ShopManage, gold_rank from ._data_source import ShopManage, gold_rank
@ -107,35 +107,29 @@ _matcher.shortcut(
@_matcher.assign("$main") @_matcher.assign("$main")
async def _(session: EventSession, arparma: Arparma): async def _(session: Uninfo, arparma: Arparma):
image = await ShopManage.build_shop_image() image = await ShopManage.build_shop_image()
logger.info("查看商店", arparma.header_result, session=session) logger.info("查看商店", arparma.header_result, session=session)
await MessageUtils.build_message(image).send() await MessageUtils.build_message(image).send()
@_matcher.assign("my-cost") @_matcher.assign("my-cost")
async def _(session: EventSession, arparma: Arparma): async def _(session: Uninfo, arparma: Arparma):
if session.id1:
logger.info("查看金币", arparma.header_result, session=session) logger.info("查看金币", arparma.header_result, session=session)
gold = await ShopManage.my_cost(session.id1, session.platform) gold = await ShopManage.my_cost(
session.user.id, PlatformUtils.get_platform(session)
)
await MessageUtils.build_message(f"你的当前余额: {gold}").send(reply_to=True) await MessageUtils.build_message(f"你的当前余额: {gold}").send(reply_to=True)
else:
await MessageUtils.build_message("用户id为空...").send(reply_to=True)
@_matcher.assign("my-props") @_matcher.assign("my-props")
async def _(session: EventSession, arparma: Arparma, nickname: str = UserName()): async def _(session: Uninfo, arparma: Arparma, nickname: str = UserName()):
if session.id1:
logger.info("查看道具", arparma.header_result, session=session) logger.info("查看道具", arparma.header_result, session=session)
if image := await ShopManage.my_props( if image := await ShopManage.my_props(
session.id1, session.user.id, nickname, PlatformUtils.get_platform(session)
nickname,
session.platform,
): ):
await MessageUtils.build_message(image.pic2bytes()).finish(reply_to=True) await MessageUtils.build_message(image.pic2bytes()).finish(reply_to=True)
return await MessageUtils.build_message("你的道具为空捏...").send(reply_to=True) return await MessageUtils.build_message("你的道具为空捏...").send(reply_to=True)
else:
await MessageUtils.build_message("用户id为空...").send(reply_to=True)
@_matcher.assign("buy") @_matcher.assign("buy")
@ -163,7 +157,7 @@ async def _(
bot: Bot, bot: Bot,
event: Event, event: Event,
message: UniMsg, message: UniMsg,
session: EventSession, session: Uninfo,
arparma: Arparma, arparma: Arparma,
name: Match[str], name: Match[str],
num: Query[int] = AlconnaQuery("num", 1), num: Query[int] = AlconnaQuery("num", 1),
@ -177,7 +171,9 @@ async def _(
bot, event, session, message, name.result, num.result, "" bot, event, session, message, name.result, num.result, ""
) )
logger.info( logger.info(
f"使用道具 {name}, 数量: {num}", arparma.header_result, session=session f"使用道具 {name.result}, 数量: {num.result}",
arparma.header_result,
session=session,
) )
if isinstance(result, str): if isinstance(result, str):
await MessageUtils.build_message(result).send(reply_to=True) await MessageUtils.build_message(result).send(reply_to=True)

View File

@ -7,7 +7,6 @@ from typing import Any, Literal
from nonebot.adapters import Bot, Event from nonebot.adapters import Bot, Event
from nonebot_plugin_alconna import UniMessage, UniMsg from nonebot_plugin_alconna import UniMessage, UniMsg
from nonebot_plugin_session import EventSession
from nonebot_plugin_uninfo import Uninfo from nonebot_plugin_uninfo import Uninfo
from pydantic import BaseModel, create_model from pydantic import BaseModel, create_model
@ -52,8 +51,8 @@ class Goods(BaseModel):
"""单次使用最大次数""" """单次使用最大次数"""
model: Any = None model: Any = None
"""model""" """model"""
session: EventSession | None = None session: Uninfo | None = None
"""EventSession""" """Uninfo"""
class ShopParam(BaseModel): class ShopParam(BaseModel):
@ -75,8 +74,8 @@ class ShopParam(BaseModel):
"""是否发送使用成功信息""" """是否发送使用成功信息"""
max_num_limit: int = 1 max_num_limit: int = 1
"""单次使用最大次数""" """单次使用最大次数"""
session: EventSession | None = None session: Uninfo | None = None
"""EventSession""" """Uninfo"""
message: UniMsg message: UniMsg
"""UniMessage""" """UniMessage"""
@ -143,7 +142,7 @@ class ShopManage:
cls, cls,
bot: Bot, bot: Bot,
event: Event, event: Event,
session: EventSession, session: Uninfo,
message: UniMsg, message: UniMsg,
goods: Goods, goods: Goods,
num: int, num: int,
@ -158,14 +157,19 @@ class ShopManage:
num: 数量 num: 数量
text: 其他信息 text: 其他信息
""" """
group_id = None
if session.group:
group_id = (
session.group.parent.id if session.group.parent else session.group.id
)
_kwargs = goods.params _kwargs = goods.params
model = goods.model( model = goods.model(
**{ **{
"goods_name": goods.name, "goods_name": goods.name,
"bot": bot, "bot": bot,
"event": event, "event": event,
"user_id": session.id1, "user_id": session.user.id,
"group_id": session.id3 or session.id2, "group_id": group_id,
"num": num, "num": num,
"text": text, "text": text,
"session": session, "session": session,
@ -176,8 +180,8 @@ class ShopManage:
**_kwargs, **_kwargs,
"_bot": bot, "_bot": bot,
"event": event, "event": event,
"user_id": session.id1, "user_id": session.user.id,
"group_id": session.id3 or session.id2, "group_id": group_id,
"num": num, "num": num,
"text": text, "text": text,
"goods_name": goods.name, "goods_name": goods.name,
@ -189,7 +193,7 @@ class ShopManage:
cls, cls,
args: MappingProxyType, args: MappingProxyType,
param: ShopParam, param: ShopParam,
session: EventSession, session: Uninfo,
**kwargs, **kwargs,
) -> list[Any]: ) -> list[Any]:
"""解析参数 """解析参数
@ -253,7 +257,7 @@ class ShopManage:
cls, cls,
goods: Goods, goods: Goods,
param: ShopParam, param: ShopParam,
session: EventSession, session: Uninfo,
**kwargs, **kwargs,
) -> str | UniMessage | None: ) -> str | UniMessage | None:
"""运行道具函数 """运行道具函数
@ -285,7 +289,7 @@ class ShopManage:
cls, cls,
bot: Bot, bot: Bot,
event: Event, event: Event,
session: EventSession, session: Uninfo,
message: UniMsg, message: UniMsg,
goods_name: str, goods_name: str,
num: int, num: int,
@ -307,7 +311,7 @@ class ShopManage:
""" """
if goods_name.isdigit(): if goods_name.isdigit():
try: try:
user = await UserConsole.get_user(user_id=session.id1) # type: ignore user = await UserConsole.get_user(user_id=session.user.id)
uuid = list(user.props.keys())[int(goods_name)] uuid = list(user.props.keys())[int(goods_name)]
goods_info = await GoodsInfo.get_or_none(uuid=uuid) goods_info = await GoodsInfo.get_or_none(uuid=uuid)
except IndexError: except IndexError:
@ -328,7 +332,9 @@ class ShopManage:
return f"{goods_info.goods_name} 单次使用最大数量为{param.max_num_limit}..." return f"{goods_info.goods_name} 单次使用最大数量为{param.max_num_limit}..."
await cls.run_before_after(goods, param, "before", **kwargs) await cls.run_before_after(goods, param, "before", **kwargs)
result = await cls.__run(goods, param, session, **kwargs) result = await cls.__run(goods, param, session, **kwargs)
await UserConsole.use_props(session.id1, goods_info.uuid, num, session.platform) # type: ignore await UserConsole.use_props(
session.user.id, goods_info.uuid, num, PlatformUtils.get_platform(session)
)
await cls.run_before_after(goods, param, "after", **kwargs) await cls.run_before_after(goods, param, "after", **kwargs)
if not result and param.send_success_msg: if not result and param.send_success_msg:
result = f"使用道具 {goods.name} {num} 次成功!" result = f"使用道具 {goods.name} {num} 次成功!"

View File

@ -2,11 +2,12 @@ from decimal import Decimal
import nonebot import nonebot
from nonebot.drivers import Driver from nonebot.drivers import Driver
from nonebot_plugin_session import EventSession from nonebot_plugin_uninfo import Uninfo
from zhenxun.models.sign_user import SignUser from zhenxun.models.sign_user import SignUser
from zhenxun.models.user_console import UserConsole from zhenxun.models.user_console import UserConsole
from zhenxun.utils.decorator.shop import shop_register from zhenxun.utils.decorator.shop import shop_register
from zhenxun.utils.platform import PlatformUtils
driver: Driver = nonebot.get_driver() driver: Driver = nonebot.get_driver()
@ -38,12 +39,12 @@ driver: Driver = nonebot.get_driver()
"好感度双倍加持卡Ⅲ_prob": 0.3, "好感度双倍加持卡Ⅲ_prob": 0.3,
}, # type: ignore }, # type: ignore
) )
async def _(session: EventSession, user_id: int, prob: float): async def _(session: Uninfo, user_id: int, prob: float):
if session.id1: platform = PlatformUtils.get_platform(session)
user_console = await UserConsole.get_user(session.id1, session.platform) user_console = await UserConsole.get_user(session.user.id, platform)
user, _ = await SignUser.get_or_create( user, _ = await SignUser.get_or_create(
user_id=user_id, user_id=user_id,
defaults={"platform": session.platform, "user_console": user_console}, defaults={"platform": platform, "user_console": user_console},
) )
user.add_probability = Decimal(prob) user.add_probability = Decimal(prob)
await user.save(update_fields=["add_probability"]) await user.save(update_fields=["add_probability"])