diff --git a/zhenxun/builtin_plugins/shop/__init__.py b/zhenxun/builtin_plugins/shop/__init__.py index 9ce4c51ce..f31af2c6e 100644 --- a/zhenxun/builtin_plugins/shop/__init__.py +++ b/zhenxun/builtin_plugins/shop/__init__.py @@ -15,6 +15,7 @@ from zhenxun.configs.utils import BaseBlock, PluginExtraData from zhenxun.services.log import logger from zhenxun.utils.enum import BlockType, PluginType +from zhenxun.utils.exception import GoodsNotFound from zhenxun.utils.message import MessageUtils from ._data_source import ShopManage @@ -141,9 +142,16 @@ async def _( name: str, num: int, ): - result = await ShopManage.use(bot, event, session, message, name, num, "") - logger.info(f"使用道具 {name}, 数量: {num}", arparma.header_result, session=session) - if isinstance(result, str): - await MessageUtils.build_message(result).send(reply_to=True) - elif isinstance(result, UniMessage): - await result.finish(reply_to=True) + try: + result = await ShopManage.use(bot, event, session, message, name, num, "") + logger.info( + f"使用道具 {name}, 数量: {num}", arparma.header_result, session=session + ) + if isinstance(result, str): + await MessageUtils.build_message(result).send(reply_to=True) + elif isinstance(result, UniMessage): + await result.finish(reply_to=True) + except GoodsNotFound: + await MessageUtils.build_message(f"没有找到道具 {name} 或道具数量不足...").send( + reply_to=True + ) diff --git a/zhenxun/builtin_plugins/shop/_data_source.py b/zhenxun/builtin_plugins/shop/_data_source.py index 38ee3ddbf..19f7fc231 100644 --- a/zhenxun/builtin_plugins/shop/_data_source.py +++ b/zhenxun/builtin_plugins/shop/_data_source.py @@ -260,9 +260,10 @@ async def use( bot, event, session, message, goods, num, text ) if num > param.max_num_limit: - return f"{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) result = await cls.__run(goods, param, session, message, **kwargs) + await UserConsole.use_props(session.id1, goods_info.uuid, num, session.platform) # type: ignore await cls.run_before_after(goods, param, "after", **kwargs) if not result and param.send_success_msg: result = f"使用道具 {goods.name} {num} 次成功!" diff --git a/zhenxun/models/user_console.py b/zhenxun/models/user_console.py index 1b560a11b..55fd021b2 100644 --- a/zhenxun/models/user_console.py +++ b/zhenxun/models/user_console.py @@ -144,7 +144,7 @@ async def add_props( async def add_props_by_name( cls, user_id: str, name: str, num: int = 1, platform: str | None = None ): - """添加道具 + """根据名称添加道具 参数: user_id: 用户id @@ -155,3 +155,41 @@ async def add_props_by_name( if goods := await GoodsInfo.get_or_none(goods_name=name): return await cls.add_props(user_id, goods.uuid, num, platform) raise GoodsNotFound("未找到商品...") + + @classmethod + async def use_props( + cls, user_id: str, goods_uuid: str, num: int = 1, platform: str | None = None + ): + """添加道具 + + 参数: + user_id: 用户id + goods_uuid: 道具uuid + num: 道具数量. + platform: 平台. + """ + user, _ = await cls.get_or_create( + user_id=user_id, + defaults={"platform": platform, "uid": await cls.get_new_uid()}, + ) + + if goods_uuid not in user.props or user.props[goods_uuid] < num: + raise GoodsNotFound("未找到商品或道具数量不足...") + user.props[goods_uuid] -= num + await user.save(update_fields=["props"]) + + @classmethod + async def use_props_by_name( + cls, user_id: str, name: str, num: int = 1, platform: str | None = None + ): + """根据名称添加道具 + + 参数: + user_id: 用户id + name: 道具名称 + num: 道具数量. + platform: 平台. + """ + if goods := await GoodsInfo.get_or_none(goods_name=name): + return await cls.use_props(user_id, goods.uuid, num, platform) + raise GoodsNotFound("未找到商品...")