omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Discord.py 1.2.2 Bot doesn't respond to commands

    Pythonista
    5
    10
    18774
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Noxive
      Noxive last edited by

      Ok, so I'm trying to create Discord Bot and i can't figure out why my commands don't work. The on_message() events work properly if i type shrug the bot sends the emoji but when i try to call the $test command bot doesn't respond literraly no errors and no response on the channel. I've spent like 2 hours reading the documentation and everything is ok, but its not.

      Python 3.6.8
      discord-py 1.2.2

      Please somebody help.

      from discord.ext.commands import Bot
      
      TOKEN = 'Here goes my token'
      
      client = discord.Client()
      bot = Bot(command_prefix='$') """ Command prefix is marked red in PyCharm"""
      
      
      @bot.command()
      async def test(ctx, arg):
          await ctx.send(arg)
      
      """
      After the bot logging on to the server 
      on_ready event occurs and prints the name of the logged in Bot
      """
      
      
      @client.event
      async def on_ready():
          print("We have logged in as: {0.user}".format(client))
      
      """
      When the message is sent event on_message() occurs
      """
      
      
      @client.event
      async def on_message(message):
      
          """
          If the message is from the bot nothing happens.
          ...
          When the user inputs $hello bot sends Hello! message on the channel.
          ...
          For the message = 'shrug' bot sends shrug emoji in ASCII onto the channel.
          """
      
          if message.author == client.user:
              return
      
          if message.content.startswith('$hello'):
              await message.channel.send("Hello!")
      
          if message.content.startswith("shrug"):
              await message.channel.send('¯\_(ツ)_/¯')
      
      client.run(TOKEN)
      
      
      cvp 1 Reply Last reply Reply Quote 0
      • ellie_ff1493
        ellie_ff1493 last edited by

        Can you do some basic debugging and find out if the functions get called, so we know if it’s a problem with the return or the message isn’t making it to the function

        1 Reply Last reply Reply Quote 0
        • ellie_ff1493
          ellie_ff1493 last edited by

          Just put a few prints in it

          1 Reply Last reply Reply Quote 0
          • ccc
            ccc last edited by ccc

            This is probably unrelated but on-the-same-line comments like on line 6 should be done with the pound/hash (#) sign instead of with triple quotes.

            a = 'a'  # letter a
            b = 'b'  """ letter b"""
            assert len(a) == len(b), (a, b)
            
            1 Reply Last reply Reply Quote 0
            • cvp
              cvp @Noxive last edited by cvp

              @Noxive I really don't know discord but if you see the doc, you have to use your $test with a parameter,
              like "$test world". Did you do so? Else nothing happens.

              1 Reply Last reply Reply Quote 0
              • Noxive
                Noxive last edited by

                @ellie_ff1493 I did. Nothing happens. Like the command event never even occured.

                @ccc @cvp I deleted all the comments and PyDoc, there is no change.

                1 Reply Last reply Reply Quote 0
                • JonB
                  JonB last edited by

                  could you try:

                  @bot.command()
                  async def test(ctx):
                      await ctx.send('test')
                  

                  then type $test

                  1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB last edited by JonB

                    oh, i see.

                    client.run('token')

                    shuld be

                    bot.run('token')

                    also, client.on_message, etc all become bot.on_message
                    Bot is a subclass of Client -- your bot instance IS your client.

                    1 Reply Last reply Reply Quote 0
                    • Noxive
                      Noxive last edited by

                      @JonB Tried the function, nothing no response not on the server or the terminal

                      @bot.command()
                      async def test(ctx):
                          print("test Called")
                          await ctx.send('test')
                      

                      I changed the client.event to bot.event, the on_message event and on_ready work fine. The command still doesn't respond

                      import discord
                      from discord.ext.commands import Bot
                      
                      TOKEN = 'MyToken'
                      
                      bot = Bot(command_prefix='$')
                      
                      
                      @bot.command()
                      async def test(ctx):
                          print("test Called")
                          await ctx.send('test')
                      
                      
                      @bot.event
                      async def on_ready():
                          print("We have logged in as: {0.user}".format(bot))
                      
                      
                      @bot.event
                      async def on_message(message):
                      
                          if message.author == bot.user:
                              return
                      
                          if message.content.startswith("shrug"):
                              print("shrug")
                              await message.channel.send('¯\_(ツ)_/¯')
                      
                      bot.run(TOKEN)
                      1 Reply Last reply Reply Quote 0
                      • Noxive
                        Noxive last edited by

                        Ok finally solved it. I missed this fragment in the docs:

                        Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message
                        

                        Thanks everyone for help.

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors