【问题标题】: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你如何停止让不和谐的机器人问一些东西,然后当我输入一些东西时,命令会停止,所以我不能向它发送垃圾邮件
【发布时间】:2021-01-08 11:38:37
【问题描述】:

我正在执行 pls search 类型的命令,但我不知道如何让机器人说出类似“你找到了 [50 - 3000 个随机数量的硬币] 硬币!”之类的话。在我输入问题的答案后。我也不知道如何使 3 个 [random_place] 不一样。所以基本上我希望机器人这样做:

我:请搜索

bot:“你想在哪里搜索?[random_place#1]、[random_place#2]、[random_place#3]”

我:[random_place#1]

bot:“你找到了 [50 - 3000 随机数量的硬币]!”

我尝试使用 on_message 在@client.command 中使用@client.event,但是当我尝试对其进行测试时,我能够发送垃圾邮件并获得无限硬币。我该如何解决?这是我的代码:

@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?\n Pick from the list below and type it in the chat:\n`{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}')

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    您的 on_message 将是全局的,并且在运行时贯穿整个机器人,这意味着如果有人说出这些特定关键字,即使命令未运行,他们也会获得硬币。

    您也不能像以前那样在命令中使用事件,只需使用如下所示的检查:

    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}')
    
    

    只需将其更改为您想要的方式,希望这会有所帮助!

    @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?\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)
    
    
          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}')
    
    

    【讨论】:

    • 我输入了 [prefix] 搜索,但机器人没有回复
    • 是的,您需要更正系统设置的缩进,我在网站内输入了这个,因此它与您的缩进不一致,您只需对齐它们
    • 你我修好了
    • 希望对您有所帮助!
    • 另外,你如何让机器人在超时时说些什么?
    【解决方案2】:
    random_place = [random.choice(list(search_list.keys())) , random.choice(list(search_list.keys())) , random.choice(list(search_list.keys()))]
    await message.channel.send(f'Where do you want to search? {random_place[0]},{random_place[1]},{random_place[2]}')
    
    def check(m):
        return m.content in random_place and m.channel == message.channel and m.author == message.author
    
    msg = await client.wait_for('message', check=check)
    await message.channel.send(f'{search_list[f"{msg}"]}')
    

    random_place 变量将保存搜索字典中的 3 个随机位置。
    下一行将发送'你想在哪里搜索? 1 , 2 , 3'
    现在我使用wait_for 函数等待用户输入。如果用户发送了一条消息,它将开始检查正确的place(1,2,3)、频道和用户,
    如果一切正常,它将发送所选地点的search dict 值!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2018-02-27
      • 2020-11-14
      • 2021-08-10
      • 1970-01-01
      相关资源
      最近更新 更多