You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何转换Discord模态文本输入类型为字符串或忽略类型比较值?

解决discord.py模态框TextInput值匹配问题

你错误地直接使用了TextInput组件实例self.code来做匹配,self.code是组件对象而非用户输入的文本,str(self.code)得到的只是组件的对象描述,不是用户输入的验证码,所以匹配无效。

正确的方式是访问TextInput的value属性,它存储了用户输入的字符串内容:

修改后的核心代码片段:

async def on_submit(self, interaction: discord.Interaction):
    refresh()
    # 获取用户输入的验证码字符串
    input_code = self.code.value
    print(type(input_code))  # 此时类型为str
    if input_code in client.monthly_codes:
        if interaction.user.id in client.monthly_user or interaction.user.id in client.yearly_user:
            premium_status = "Yearly" if interaction.user.id in client.yearly_user else "Monthly"
            # 可在此添加后续交互逻辑,如向用户反馈结果

说明:

  • self.code.value是用户在输入框中填写的原生字符串,无需额外转换即可直接与client.monthly_codes数组进行匹配。
  • 替换原代码中self.code或str(self.code)的位置为self.code.value即可解决问题。

内容的提问来源于stack exchange,提问作者Paul Bao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 10:44:56