Discord机器人不会接收新消息

时间:2020-03-24 18:11:57

标签: python discord.py

所以我正在制造一个奇怪的不和谐机器人。它显示一组障碍,并将需要按的相应键添加到字符串。它为每个“ ...”添加一个“ w”,为每个“ ***”添加一个“ s”。在Discord中,玩家必须输入正确的字符串才能通过关卡。因此,所有这些都可以正常显示。呼叫功能为“!go”,并显示级别。但是在输入字符串之前,它使我失败了。我想知道为什么它会自动激活?我的message.content函数不在该循环中。我认为这是因为我键入“!go”并且它不会重置我的消息。这是代码:

@client.event
async def on_message(message):
  global correct_answerS
  correct_answerS = ""
  obstacleList = []

  for i in range (0, 24):
    randNum = random.randint(1,2)
    if randNum == 1:
      obstacleList.append('...')
      correct_answerS += 'w'
    if randNum == 2:
      obstacleList.append('***')
      correct_answerS += 's'


  if message.content == ('!practice'):
    sky = str("```css\n A     ...     ...      ***    ...```")
    skyembed = discord.Embed(title="!============================!")
    skyembed.add_field(name='When you see three dots, it is a w. When you see 3 asteriks, it is an s.', value = sky)
    await message.channel.send(embed=skyembed)
  if message.content == ('wwsw'):
    await message.channel.send("Success.")

  if message.content == ('!go'):
    randlev = (str("```css\n A     ")) 
    for i in range(0, 24): 
      randlev += (str(obstacleList[i]) + "   ")
    randlev += "```"
    randlevembed = discord.Embed(title="!===============================================!")
    randlevembed.add_field(name='Good luck!', value = randlev)
    await message.channel.send(embed=randlevembed)
    print(correct_answerS)

  if message.content == (correct_answerS):
    await message.channel.send("Success!")
  else:
    await message.channel.send("Fail.")


@client.event
async def on_ready():
  print("Ready")
  print(client.user.id)
  print('--------------------------')
  await client.change_presence(activity=discord.Game(name='up and away'))

client.run(Token)

没有错误消息,只要我键入“!go”,它就会显示级别,然后不断显示“失败”。直到我阻止它。 我不知道该怎么办,任何帮助就是有帮助的。谢谢。

1 个答案:

答案 0 :(得分:1)

该机器人被困在一个循环中,在该循环中,它发送一条消息,然后对该消息进行处理,从而导致它发送另一条消息,依此类推。您可以在on_message的开头添加一个检查,以强制其执行忽略漫游器生成的消息:

@client.event
async def on_message(message):
    if message.author.bot:
        return 
    ...