如何转换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
相关产品推荐
相关产品推荐

