Python开发Discord宝可梦猜谜Bot:正确答案误判的修复求助
问题修复方案
你的代码核心问题是直接将Message对象与字符串进行比较:await client.wait_for("message")返回的是discord.Message实例,而非用户输入的文本内容,所以无论输入正确与否都会判定为错误。
另外建议添加大小写忽略逻辑,避免用户因大小写输入偏差导致判定失败。
修复后的完整代码:
@client.command() async def rp(ctx): ranpoke = random.randint(1, 905) url = "https://pokeapi.co/api/v2/pokemon/" secs = 5 try: r = requests.get(f"{url}{ranpoke}") packages_json = r.json() napo = packages_json["name"] embed = discord.Embed(title = "Who's that pokemon?!", color = discord.Color.random()) embed.set_image(url = f"https://play.pokemonshowdown.com/sprites/ani/{napo}.gif") await ctx.send(embed = embed) def check(msg): return msg.author == ctx.author and msg.channel == ctx.channel user_ans = await client.wait_for("message", check=check) # 提取用户输入文本,去除空格并统一小写 user_input = user_ans.content.strip().lower() if user_input == napo: await ctx.send("You are correct! :)") else: await ctx.send(f"You are incorrect :v\nThe answer is {napo}") except Exception as e: print(f"调试信息:{e}") await ctx.send("bruh")
关键修改点
- 提取用户实际输入的文本:使用
user_ans.content获取消息内容,通过strip()去除前后空格、lower()统一转为小写,避免大小写和空格导致的判定误差 - 移除了无意义的
packages_json.keys调用(该行代码未产生实际作用) - 优化异常处理,添加调试打印便于排查问题
- 将冗余的
elif逻辑简化为else
内容的提问来源于stack exchange,提问作者ThaRiAn043
相关产品推荐
相关产品推荐

