Saya membuat perintah pls search (command from dank memer), tetapi hanya merespons ketika saya mengetikkan kata yang tepat sebagai kunci dalam kamus saya search_list. Saya mencoba membuatnya agar argumen yang saya masukkan tidak peka huruf besar-kecil, misalnya setelah saya mengetik pls search, seharusnya merespons Park atau park atau PARK ( Ini diketik selama fungsi wait_for()).

Kode saya:

@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.")
-1
Edward luo 9 Januari 2021, 03:56

3 jawaban

Jawaban Terbaik

Anda dapat menggunakan fungsi lower(). Jadi kondisi Anda akan terlihat seperti ini:

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.")

Saya menemukan beberapa masalah lagi dengan perintah Anda, jadi saya melanjutkan dan memperbaikinya untuk Anda:

@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.")
2
Sujit 9 Januari 2021, 04:03

Untuk menambahkan perintah case-insensitive untuk bot Anda, Anda dapat melakukan seperti ini

bot = commands.Bot(command_prefix='!', case_insensitive=True)

Sehingga setiap perintah akan bekerja dengan kasus apapun['upper'/'lower'].
[dokumentasi]

0
Manoj A 9 Januari 2021, 01:57

Gunakan .lower() dengan a b c diturunkan juga

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)
elif message.content.lower() not in (a.lower(), b.lower(), c.lower()):
    await ctx.send(f"<@{ctx.author.id}> That's not a valid option lmao.")
0
Just for fun 9 Januari 2021, 03:43