vkbottle使用BaseStateGroup遇弃用警告,寻求解决方案
解决VKBottle中BaseStateGroup弃用警告问题
问题代码
我编写了以下代码尝试创建状态:
from vkbottle.bot import Bot, Message from vkbottle import Keyboard, KeyboardButtonColor, Text from vkbottle_types import BaseStateGroup class SuperStates(BaseStateGroup): NAME = 0 @bot.on.message(state=SuperStates.NAME) # StateRule(SuperStates.AWKWARD_STATE) async def awkward_handler(message: Message): await message.answer("oi awkward") @bot.on.message(lev="/die") async def die_handler(message: Message): await bot.state_dispenser.set(message.peer_id, SuperStates.NAME) return "ok"
错误信息
运行后出现如下警告:
raise DeprecationWarning( DeprecationWarning: BaseStateGroup from vkbottle_types is deprecated and will be removed in future releases, use vkbottle.BaseStateGroup instead
解决方法
这个警告的含义很直接:vkbottle_types模块中的BaseStateGroup已被弃用,后续版本会彻底移除,需要改用vkbottle核心包中的BaseStateGroup。
只需修改导入语句:
- 将
from vkbottle_types import BaseStateGroup替换为from vkbottle import BaseStateGroup
修改后的完整代码:
from vkbottle.bot import Bot, Message from vkbottle import Keyboard, KeyboardButtonColor, Text, BaseStateGroup class SuperStates(BaseStateGroup): NAME = 0 @bot.on.message(state=SuperStates.NAME) async def awkward_handler(message: Message): await message.answer("oi awkward") @bot.on.message(lev="/die") async def die_handler(message: Message): await bot.state_dispenser.set(message.peer_id, SuperStates.NAME) return "ok"
保存代码后重新运行,警告就会消失。
内容的提问来源于stack exchange,提问作者Kirill Shishkov
相关产品推荐
相关产品推荐

