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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 07:50:17