无限重复的消息——Discord.py

时间:2021-02-24 01:57:49

标签: discord.py

所以这是 100% 的一个 wip,我对如何阻止 Discord 机器人一遍又一遍地阅读相同的用户消息感到非常困惑。

import discord

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged in as')
        print(self.user.name)
        print(self.user.id)
        print('------')
       
    async def on_message(self, message):
        global choice
        global r
        # we do not want the bot to reply to itself
        if message.author.id == self.user.id:
            return
        if message.content.startswith('!hello'):
            await message.reply('Hello!', mention_author=True)
        r=0
        basic = True
        #upgrade1 = False
        choice = '!idle'
        while(choice != '!end'):
          if message.content.startswith('!idle'):
            choice= await message.reply('!click / !bank / !upgrade / !minigame / !end')
          if message.author.id == self.user.id:
              return
          if message.content.startswith('!click' or '!c'):
              r = r + 1
              await message.reply("You have " + str(r) + " Renown")
          elif message.content.startswith ('!bank'):
              await message.reply ("You have " + str(r) + " Renown")
          #elif message.content.startswith ('!upgrade' and r >= 10):
              #await message.reply('Upgrading..')
              #r = r - 10
              #basic = False
          #elif message.content.startswith  ('!upgrade' and r < 10):
            #await message.reply ('Not Enough Renown')
          elif message.content.startswith('!minigame' or '!mg'):
            #random number choosing game
            await message.reply ("Game")
          elif message.content.startswith ('!end'):
            basic = False
          #while time.time() < future:
              #r =r + .00001



client = MyClient()
client.run

1 个答案:

答案 0 :(得分:0)

您的问题来自 while(choice != '!end') 循环。一旦机器人进入该循环,他在该循环内发送的消息当然会重复,因为除非消息是 choice,否则您不会更改 !idle 的值。

请注意,如果命令是例如 !click,您将在代码中发送以下消息:

await message.reply("You have " + str(r) + " Renown")

但您并未更改 choice 的值,因此在 while 循环的下一次迭代中,它将一次又一次地重复相同的操作。

我不确定您要实现的目标,但在 on_message 事件中使用 while 循环对我来说似乎不是一个好主意,因为这是一个阻塞功能。我认为您应该将其替换为 if