Discord.py斜杠命令选项问题:设置数字参数最值触发报错
Discord.py斜杠命令数字参数最值设置报错解决
问题根源
你代码里犯了两个关键错误:
app_commands.Argument.min_value是类属性,不是可调用方法,不能用()传参,这直接导致了TypeError: 'member_descriptor' object is not callable报错。- 你重复写了
min_value,应该分别设置最小值和最大值,而且@app_commands.Argument装饰器的用法完全不符合Discord.py的规范。
正确实现方式
方式一:用app_commands.Range限定参数范围
这是最直观的写法,直接把参数类型指定为带范围的Range:
@bot.tree.command(name="test") async def test(interaction: discord.Interaction, num: app_commands.Range[int, 1, 5]): await interaction.response.send_message(f"You choose number: {num}")
这里app_commands.Range[int, 1, 5]表示参数是int类型,最小值1,最大值5。
方式二:通过Parameter指定最值
如果需要更灵活的参数描述,可以用app_commands.Parameter来设置:
@bot.tree.command(name="test") async def test(interaction: discord.Interaction, num: int = app_commands.Parameter(min_value=1, max_value=5, description="输入1到5之间的数字")): await interaction.response.send_message(f"You choose number: {num}")
效果说明
用上面任意一种写法后,用户输入超出1-5范围的数字时,Discord客户端会自动拦截并提示错误,不用你手动写判断逻辑,同时之前的报错也会消失。
内容的提问来源于stack exchange,提问作者Derek
相关产品推荐
相关产品推荐

