discord.py中链式调用多个Discord模态框的实现问题
问题原因与修复方案
核心错误点
- 异步函数拼写错误:两处
asnyc都要改成async - 提交方法名拼写错误:两处
on_sumbit都要改成on_submit - 命令函数定义缺冒号:
async def main(self,interaction:discord.Interaction)末尾必须加:
这些语法错误会导致Discord无法正确识别模态框的提交处理逻辑,交互得不到正确响应,就会弹出"Something went wrong, try again"的提示,同时无法触发下一个模态框。
修复后的完整代码
class Modal_2(discord.ui.Modal, title="Modal_2"): question_1 = discord.ui.TextInput(label="That's the second question") async def on_submit(self, interaction: discord.Interaction): await interaction.response.send_message(content="Thanks for your input") class Modal_1(discord.ui.Modal, title="Modal_1"): question_1 = discord.ui.TextInput(label="That's the first question") async def on_submit(self, interaction: discord.Interaction): await interaction.response.send_modal(Modal_2()) class main(commands.Cog): def __init__(self, bot): self.bot = bot @app_commands.command(name="survey") async def main(self, interaction: discord.Interaction): await interaction.response.send_modal(Modal_1())
额外注意事项
- 确保使用的discord.py版本在v2.0及以上,旧版本对模态框链式调用的支持不完善
- 修复后提交第一个模态框时,Discord会自动关闭当前模态框并弹出第二个,完成流程后会发送感谢消息
内容的提问来源于stack exchange,提问作者AxaQuilPre
相关产品推荐
相关产品推荐

