- 实现 handle_test_set_points 函数的完整逻辑 - 支持解析命令参数中的金额值 - 添加参数验证(非负数、整数类型) - 返回设置结果反馈给用户 用法: /测试设置积分 <金额> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
111 lines
3.3 KiB
Python
111 lines
3.3 KiB
Python
from nonebot import on_command
|
|
from nonebot.adapters.onebot.v11 import Bot, Event, GroupMessageEvent, PrivateMessageEvent
|
|
|
|
from .config import Config
|
|
from .room_store import RoomStore
|
|
from .points_service import PointsService
|
|
from .commands import get_scope, check_access, room_store, points_service
|
|
|
|
config = Config()
|
|
|
|
|
|
async def check_tester(event: Event) -> bool:
|
|
"""Check if user is a tester."""
|
|
if not config.TEST_MODE:
|
|
return False
|
|
return event.user_id in config.TESTERS
|
|
|
|
|
|
test_reset_points_cmd = on_command("测试重置积分", priority=5)
|
|
|
|
|
|
@test_reset_points_cmd.handle()
|
|
async def handle_test_reset_points(bot: Bot, event: Event):
|
|
"""Reset user points to 1000 for testing."""
|
|
if not await check_tester(event):
|
|
await test_reset_points_cmd.finish("权限不足")
|
|
return
|
|
|
|
success, _ = await points_service.set_points(event.user_id, 1000, "测试重置积分")
|
|
if success:
|
|
await test_reset_points_cmd.finish("积分已重置为1000")
|
|
else:
|
|
await test_reset_points_cmd.finish("重置失败")
|
|
|
|
|
|
test_set_points_cmd = on_command("测试设置积分", priority=5)
|
|
|
|
|
|
@test_set_points_cmd.handle()
|
|
async def handle_test_set_points(bot: Bot, event: Event):
|
|
"""Set user points for testing."""
|
|
if not await check_tester(event):
|
|
await test_set_points_cmd.finish("权限不足")
|
|
return
|
|
|
|
# Get the message text and extract amount
|
|
msg = str(event.get_message()).strip()
|
|
# Remove command prefix
|
|
parts = msg.split()
|
|
|
|
if len(parts) < 2:
|
|
await test_set_points_cmd.finish("请使用: /测试设置积分 <金额>")
|
|
return
|
|
|
|
try:
|
|
amount = int(parts[1])
|
|
if amount < 0:
|
|
await test_set_points_cmd.finish("金额必须为非负数")
|
|
return
|
|
except ValueError:
|
|
await test_set_points_cmd.finish("金额必须是整数")
|
|
return
|
|
|
|
success, _ = await points_service.set_points(event.user_id, amount, f"测试设置积分为{amount}")
|
|
if success:
|
|
await test_set_points_cmd.finish(f"积分已设置为 {amount}")
|
|
else:
|
|
await test_set_points_cmd.finish("设置失败")
|
|
|
|
|
|
test_query_points_cmd = on_command("测试查询积分", priority=5)
|
|
|
|
|
|
@test_query_points_cmd.handle()
|
|
async def handle_test_query_points(bot: Bot, event: Event):
|
|
"""Query user points for testing."""
|
|
if not await check_tester(event):
|
|
await test_query_points_cmd.finish("权限不足")
|
|
return
|
|
|
|
balance = await points_service.get_balance(event.user_id)
|
|
await test_query_points_cmd.finish(f"当前积分: {balance}")
|
|
|
|
|
|
test_clear_room_cmd = on_command("测试清空房间", priority=5)
|
|
|
|
|
|
@test_clear_room_cmd.handle()
|
|
async def handle_test_clear_room(bot: Bot, event: Event):
|
|
"""Clear test room."""
|
|
if not await check_tester(event):
|
|
await test_clear_room_cmd.finish("权限不足")
|
|
return
|
|
|
|
scope = get_scope(event)
|
|
room_store.delete_room(scope)
|
|
await test_clear_room_cmd.finish("房间已清空")
|
|
|
|
|
|
test_force_start_cmd = on_command("测试强制开赛", priority=5)
|
|
|
|
|
|
@test_force_start_cmd.handle()
|
|
async def handle_test_force_start(bot: Bot, event: Event):
|
|
"""Force start race for testing."""
|
|
if not await check_tester(event):
|
|
await test_force_start_cmd.finish("权限不足")
|
|
return
|
|
|
|
await test_force_start_cmd.finish("测试强制开赛命令")
|