Minecow 12345
Minecow 12345

Reputation: 1

Messages repeating infinitely -- Discord.py

So this is 100% a wip and I'm super confused on how to stop the discord bot from reading the same user message over and over again.

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

Upvotes: 0

Views: 662

Answers (1)

Shunya
Shunya

Reputation: 2474

Your issue comes from the while(choice != '!end') loop. Once the bot enters that loop, the messages he is sending within that loop of course will be repeated because you are not changing the value of choice unless the message is !idle.

Notice in your code how if the command is for example !click, you are sending the following message:

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

But you are not changing the value of choice, so in the next iteration of the while loop it will repeat the same, once and again.

I'm not sure what you are trying to achieve, but having a while loop in the on_message event does not seem a good idea for me, as this is a blocking feature. In my opinion you should replace it with an if.

Upvotes: 1

Related Questions