Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

discord.py - How do you stop make a discord bot ask something, and then when I type something the command stops so I can't spam it

I'm making a pls search type of command, but I don't know how to make the bot say something like "You found [random amount of coins from 50 - 3000] coins!" after i type the answer to a question. I also don't know how to make it so that the 3 [random_place]s are not the same. So basically I want the bot to do this:

me: pls search

bot: "Where do you want to search? [random_place#1], [random_place#2], [random_place#3]"

me: [random_place#1]

bot: "You found [random amount of coins from 50 - 3000] coins!"

I tried using a @client.event inside the @client.command using on_message, but then when I tried to test it out, I was able to spam the answer and get infinite coins. How do I fix this? This is my code:

@client.command()
async def search(ctx):
  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...',
  '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'',
  'Bed': f'',
  'Bus': f'',
  'Bushes': f'',
  'Uber': f'',
  'Car': f'',
  'Coat': f'',
  'Couch': f'',
  'Discord': f'',
  'Dresser': f'',
  'Laundromat': f'',
  'Mailbox': f'',
  'Pocket': f'',
  'Purse': f'',
  'Grass': f'',
  'Pantry': f'',
  'Shoe': f'',
  'Sink': f'',
  'Pumpkin': f''
  }
  a = random.choice(list(search_list.keys()))
  b = random.choice(list(search_list.keys()))
  c = random.choice(list(search_list.keys()))
  sameness = True
  while sameness == True:
      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:
    sameness=False
    await ctx.send(f"<@{ctx.author.id}> Where do you want to search?
 Pick from the list below and type it in the chat:
`{a}`, `{b}`, `{c}`")
    @client.event
    async def on_message(message):
      if message.author == client.user:
        return

      if message.content.startswith(f'{a}'):
        await message.channel.send(f'{a.value}')
      if message.content.startswith(f'{b}'):
        await message.channel.send(f'{b.value}')
      if message.content.startswith(f'{c}'):
        await message.channel.send(f'{c.value}')
question from:https://stackoverflow.com/questions/65622961/how-do-you-stop-make-a-discord-bot-ask-something-and-then-when-i-type-something

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your on_message would be global and throughout the whole bot when running, this means if anyone says those specific keywords, they would get coins even when the command is not running.

You also cannot have an event used in a command like you have used, simply use a check as shown below:

message = await client.wait_for('message', timeout=10, check=lambda message: message.author == ctx.author)


      if message.content.startswith(f'{a}'):
        await message.channel.send(f'{a.value}')
      if message.content.startswith(f'{b}'):
        await message.channel.send(f'{b.value}')
      if message.content.startswith(f'{c}'):
        await message.channel.send(f'{c.value}')

Simply change this to how you wanted it, hope this helps!

@client.command()
async def search(ctx):
  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...',
  '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'',
  'Bed': f'',
  'Bus': f'',
  'Bushes': f'',
  'Uber': f'',
  'Car': f'',
  'Coat': f'',
  'Couch': f'',
  'Discord': f'',
  'Dresser': f'',
  'Laundromat': f'',
  'Mailbox': f'',
  'Pocket': f'',
  'Purse': f'',
  'Grass': f'',
  'Pantry': f'',
  'Shoe': f'',
  'Sink': f'',
  'Pumpkin': f''
  }
  a = random.choice(list(search_list.keys()))
  b = random.choice(list(search_list.keys()))
  c = random.choice(list(search_list.keys()))
  sameness = True
  while sameness == True:
      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:
    sameness=False
    await ctx.send(f"<@{ctx.author.id}> Where do you want to search?
 Pick from the list below and type it in the chat:
`{a}`, `{b}`, `{c}`")

message = await client.wait_for('message', timeout=10, check=lambda message: message.author == ctx.author)


      if message.content.startswith(f'{a}'):
        await message.channel.send(f'{a.value}')
      if message.content.startswith(f'{b}'):
        await message.channel.send(f'{b.value}')
      if message.content.startswith(f'{c}'):
        await message.channel.send(f'{c.value}')


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...