首页 > 解决方案 > 我如何做到这一点,以便如果我的输入较低或资本,我的机器人响应响应

问题描述

我做了一个 pls search(来自 dank memer 的命令)命令,但它只在我输入确切的单词作为字典中的键时才会响应search_list。我试图让我输入的参数不区分大小写,例如在我输入之后pls search,它应该响应Parkor parkor PARK(这是在wait_for()函数期间输入的)。

我的代码:

@client.command()
@commands.cooldown(1, 7, commands.BucketType.user)
async def search(ctx):
  amount_of_coins_found = random.randrange(3000)
  search_list = {
    "Sewer": f'You searched the sewer and found {amount_of_coins_found} coins!',
    'Area51': f'You raided Area51 and found {amount_of_coins_found} coins! NOW RUN THE GOVERNMENT IS CHASING YOU!',
    'Tree': f'You climbed the tree and found {amount_of_coins_found} coins! Good thing you didn\'t fall off and break your neck.',
    'Dog': f'You pet the dog and found {amount_of_coins_found} coins! That poor poor dog... Doge also got mad at you and slapped you! You died!',
    'Street': f'You searched the street and found {amount_of_coins_found} coins! Good thing you didn\'t get run over by a bus!',
    'Hospital': f'You searched the hospital and found {amount_of_coins_found} coins! Did you steal from a sick person?!',
    'Dumpster': f'You climbed inside the dumpster and found {amount_of_coins_found} coins! Good thing the garbage truck only comes on Thursdays.',
    'Air': f'You searched the air and found {amount_of_coins_found} coins!',
    'Attic': f'You searched the attic and found {amount_of_coins_found} coins!',
    'Bank': f'You searched the bank and found {amount_of_coins_found} coins! WTF did you just rob that bank?!',
    'Bed': f'You searched your bed and found {amount_of_coins_found} coins! I wonder how that got there...',
    'Bus': f'You looked under the bus seat and found {amount_of_coins_found} coins! You also found a peice of gum.',
    'Bushes': f'You looked in the bushes and found {amount_of_coins_found} coins! Good thing you didn\'t get pricked, you lucky prick!',
    'Uber': f'You searched the Uber and found {amount_of_coins_found} coins! Did you just steal from your uber driver?',
    'Car': f'You searched the car and found {amount_of_coins_found} coins!',
    'Coat': f'You searched your coat and found {amount_of_coins_found} coins! I wonder how long that has been there...',
    'Couch': f'You couchsurfed and found {amount_of_coins_found} coins! You also found your TV remote from 10 years ago.',
    'Discord': f'You sifted through your dms and found {amount_of_coins_found} coins!',
    'Dresser': f'While looking for your pants, you found {amount_of_coins_found} coins!',
    'Laundromat': f'You searched the laundromat and found {amount_of_coins_found} coins!',
    'Mailbox': f'You searched your mailbox and found {amount_of_coins_found} coins plus a love letter from a creepy stalker.',
    'Pocket': f'You reached into your pocket and found {amount_of_coins_found} coins!',
    'Purse': f'You looked in an old lady\'s purse and found {amount_of_coins_found} coins!',
    'Grass': f'You searched the grass and found {amount_of_coins_found} coins!',
    'Pantry': f'While looking for the cookies, you found {amount_of_coins_found} coins!',
    'Shoe': f'You took off your shoe and found {amount_of_coins_found} coins!',
    'Sink': f'You searched in the sink and found {amount_of_coins_found} coins!'
  }
    
  a = random.choice(list(search_list.keys()))
  b = random.choice(list(search_list.keys()))
  c = random.choice(list(search_list.keys()))
    
  if a == b or a == c or b == c:
    a = random.choice(list(search_list.keys()))
    b = random.choice(list(search_list.keys()))
    c = random.choice(list(search_list.keys()))
    
  message = await ctx.send(f"<@{ctx.author.id}> Where do you want to search?\n Pick from the list below and type it in the chat:\n`{a}`, `{b}`, `{c}`")
  message = await client.wait_for('message', timeout=10, check=lambda message: message.author == ctx.author)
  await open_account(ctx.author)
  if message.content.startswith(a):
    await message.channel.send(str(search_list.get(a)))
    await update_bank(ctx.author, amount_of_coins_found)
  if message.content.startswith(b):
    await message.channel.send(str(search_list.get(b)))
    await update_bank(ctx.author, amount_of_coins_found)
  if message.content.startswith(c):
    await message.channel.send(str(search_list.get(c)))
    await update_bank(ctx.author, amount_of_coins_found)
  if message.content != a or b or c:
    await ctx.send(f"<@{ctx.author.id}> That's not a valid option lmao.")

标签: pythondiscord.py

解决方案


您可以使用该lower()功能。所以你的情况看起来像这样:

if message.content.lower().startswith(a.lower()):
    await message.channel.send(str(search_list.get(a)))
    await update_bank(ctx.author, amount_of_coins_found)
elif message.content.lower().startswith(b.lower()):
    await message.channel.send(str(search_list.get(b)))
    await update_bank(ctx.author, amount_of_coins_found)
elif message.content.lower().startswith(c.lower()):
    await message.channel.send(str(search_list.get(c)))
    await update_bank(ctx.author, amount_of_coins_found)
else:
    await ctx.send(f"{ctx.author.mention} That's not a valid option lmao.")

我发现你的命令还有一些问题,所以我继续为你修复它们:

@client.command()
@commands.cooldown(1, 7, commands.BucketType.user)
async def search(ctx):
    amount_of_coins_found = random.randrange(3000)
    search_list = {
        'Sewer': f'You searched the sewer and found {amount_of_coins_found} coins!',
        'Area51': f'You raided Area51 and found {amount_of_coins_found} coins! NOW RUN THE GOVERNMENT IS CHASING YOU!',
        'Tree': f'You climbed the tree and found {amount_of_coins_found} coins! Good thing you didn\'t fall off and break your neck.',
        'Dog': f'You pet the dog and found {amount_of_coins_found} coins! That poor poor dog... Doge also got mad at you and slapped you! You died!',
        'Street': f'You searched the street and found {amount_of_coins_found} coins! Good thing you didn\'t get run over by a bus!',
        'Hospital': f'You searched the hospital and found {amount_of_coins_found} coins! Did you steal from a sick person?!',
        'Dumpster': f'You climbed inside the dumpster and found {amount_of_coins_found} coins! Good thing the garbage truck only comes on Thursdays.',
        'Air': f'You searched the air and found {amount_of_coins_found} coins!',
        'Attic': f'You searched the attic and found {amount_of_coins_found} coins!',
        'Bank': f'You searched the bank and found {amount_of_coins_found} coins! WTF did you just rob that bank?!',
        'Bed': f'You searched your bed and found {amount_of_coins_found} coins! I wonder how that got there...',
        'Bus': f'You looked under the bus seat and found {amount_of_coins_found} coins! You also found a peice of gum.',
        'Bushes': f'You looked in the bushes and found {amount_of_coins_found} coins! Good thing you didn\'t get pricked, you lucky prick!',
        'Uber': f'You searched the Uber and found {amount_of_coins_found} coins! Did you just steal from your uber driver?',
        'Car': f'You searched the car and found {amount_of_coins_found} coins!',
        'Coat': f'You searched your coat and found {amount_of_coins_found} coins! I wonder how long that has been there...',
        'Couch': f'You couchsurfed and found {amount_of_coins_found} coins! You also found your TV remote from 10 years ago.',
        'Discord': f'You sifted through your dms and found {amount_of_coins_found} coins!',
        'Dresser': f'While looking for your pants, you found {amount_of_coins_found} coins!',
        'Laundromat': f'You searched the laundromat and found {amount_of_coins_found} coins!',
        'Mailbox': f'You searched your mailbox and found {amount_of_coins_found} coins plus a love letter from a creepy stalker.',
        'Pocket': f'You reached into your pocket and found {amount_of_coins_found} coins!',
        'Purse': f'You looked in an old lady\'s purse and found {amount_of_coins_found} coins!',
        'Grass': f'You searched the grass and found {amount_of_coins_found} coins!',
        'Pantry': f'While looking for the cookies, you found {amount_of_coins_found} coins!',
        'Shoe': f'You took off your shoe and found {amount_of_coins_found} coins!',
        'Sink': f'You searched in the sink and found {amount_of_coins_found} coins!'
    }

    a = random.choice(list(search_list.keys()))
    search_list.pop(a)  # Removing a so that that b and c can never get it
    b = random.choice(list(search_list.keys()))
    search_list.pop(b)  # Removing b so that that c can never get it
    c = random.choice(list(search_list.keys()))

    await ctx.send(f"<@{ctx.author.id}> Where do you want to search?\n Pick from the list below and type it in the chat:\n`{a}`, `{b}`, `{c}`")

    try:
        message = await client.wait_for('message', timeout = 10, check = lambda message: message.author == ctx.author)
    except:
        await ctx.send("Sorry, you took too long to respond")
        return

    await open_account(ctx.author)

    if message.content.lower().startswith(a.lower()):
        await message.channel.send(str(search_list.get(a)))
        await update_bank(ctx.author, amount_of_coins_found)
    elif message.content.lower().startswith(b.lower()):
        await message.channel.send(str(search_list.get(b)))
        await update_bank(ctx.author, amount_of_coins_found)
    elif message.content.lower().startswith(c.lower()):
        await message.channel.send(str(search_list.get(c)))
        await update_bank(ctx.author, amount_of_coins_found)
    else:
        await ctx.send(f"{ctx.author.mention} That's not a valid option lmao.")

推荐阅读