首页 > 解决方案 > 搜索命令未按预期工作 | 不和谐.py

问题描述

我是经济命令的新手,我仍在弄清楚如何使用bot.wait_for所以如果这是一个简单的修复,我很抱歉。所以我正在制作一个搜索命令,但它不起作用。即使在使用代码块输入我想搜索的位置之后,机器人仍然无法工作。图片

这是代码

  @commands.command()
  async def search(self, ctx):
    await open_account(ctx.author)

    place = [f'`couch`', f"`park`", f"`road`"]
    place1 = [f"`dog`", f"`tree`", "`car`"]
    place2 = [f"`discord`", f"`grass`", f"`pocket`"]

    await ctx.send(f"Where do you wanna search? Pick from the list below.\n {random.choice(place)},{random.choice(place1)}, {random.choice(place2)}")
    
    answer = await self.bot.wait_for('message', check=lambda message: message.author == ctx.author)

    if answer.content.lower() == place or answer.content == place1 or answer.content == place2:
      earnings = random.randrange(301)
      await update_bank(ctx.author, earnings, "wallet")
      await ctx.send(f"You just found {earnings} coins. Cool")
      return
    else:
      await ctx.send("Thats not a part of the list tho?")

标签: discord.py

解决方案


您的 if 语句是问题所在。您正在检查输入的单词是否等于列表之一。您应该列出可用单词的列表,然后检查给定单词是否在该列表中:

@client.command()
async def search(ctx):

    place1 = ["couch", "park", "road"]
    place2 = ["dog", "tree", "car"]
    place3 = ["discord", "grass", "pocket"]

    places = [random.choice(place1), random.choice(place2), random.choice(place3)]
    placesToSearch = ', '.join([f"`{x.title()}`" for x in places])

    await ctx.send(f"Where do you wanna search? Pick from the list below.\n {placesToSearch}")
    response = await client.wait_for('message', check=lambda message: message.author == ctx.author)

    if response.content.lower() in places:
        earnings = random.randrange(301)
        await ctx.send(f"You just found {earnings} coins. Cool")
    else:
        await ctx.send("Thats not a part of the list tho?")

推荐阅读