Search code examples
pythondiscorddiscord.py

@client.event and @client.command


So I've been writing a bot using discord.py, and I've found an error I can't seem to be able to fix. First, I have something like this:

@client.command()
async def ping(ctx):
  await ctx.send("Pong!")

Then I added an entire section here:

@client.event
async def on_message(message):
# print(message)
  if message.content.startswith('$random'):
      if len(message.content) == 7:
        await message.channel.send("**You need to provide a valid number range. Examples: *$random 7 69 | $random 69 (automatically sets the first number as 1)***")
      else:
        if message.content.split(" ",1)[1] == "help":
          await message.channel.send("**You need to provide a valid number range. Examples: *$random 7 69 | $random 69 (automatically sets the first number as 1)***")
        try:
          if message.content.split(" ",2)[2] != "":
            number1 = int(message.content.split(" ",2)[1])
            number2 = int(message.content.split(" ",2)[2])
            number2send = str(random.randint(number1,number2))

            await message.channel.send("Your random number is: **" + number2send + "**")
            return
        except:
          try:
            if message.content.split(" ",1)[1] != "":
              number = int(message.content.split(" ",1)[1])
              number2send = str(random.randint(1,number))

              await message.channel.send("Your random number is: **" + number2send + "**")
              return
          except:
            await message.channel.send("**You need to provide a valid number range. Examples: *$random 7 69 | $random 69 (automatically sets the first number as 1)***")
            return
    
  if message.content.startswith('$timein'):
    if len(message.content) == 7:
        await message.channel.send("**You need to provide a valid country***")
    else:
      try:
        lad = message.split(" ",1)[1]
        print("Location address:", lad)
        location = geolocator.geocode(lad)
  
        print("Latitude and Longitude of the said address:")
        print((location.latitude, location.longitude))
          
        # pass the Latitude and Longitude
        # into a timezone_at
        # and it return timezone
        obj = TimezoneFinder()
          
        # returns 'Europe/Berlin'
        result = obj.timezone_at(lng=location.longitude, lat=location.latitude)
        print("Time Zone : ", result)
      except:
        return  

And any command made like the first section and $timein command does not work anymore, while both worked on their own, and I have no idea why. I feel like the errors are caused by the differences in

@client.command

and

@client.event

So this is probably what's causing the issue. Anyways, to any kind people helping me with these, thanks in advance!


Solution

  • Your commands doesn't work because of on_message. What happens is you are overriding the default on_message and defaut on_message is the one that handles the commands. You can fix it by adding a bot.process_commands(message) line at the end of your on_message.

    Discord.py has this issue covered on it's F.A.Q, read more here.

    Also, a side note. Please don't handle commands by yourself, Discord.py's commands extension has a lot of features you probably didn't discover. Please learn more about commands, it will save tons of work from your shoulders and will be easier to develop your dream bot.