Ask coding questions

← Back to all posts
the discord bot doesnt respond when I type +counter
tggaming999 (0)

The code doesn't say any errors but the bot doesn't reply in the server.

Comments
hotnewtop
InvisibleOne (3226)

You can only have one on_message() so you need to put all your on_message events beneath that single event, your code should look like this:

@client.event
async def on_message(message):
  
  if message.content.startswith('BumB0i noob'):
    # do something
  elif message.content.startswith('counter'):
    await message.channel.send('bla bla bla')
tggaming999 (0)

@InvisibleOne I see, also, how do I get the counter to work? I want it to increase by one everytime that message is sent.

Thank you!

InvisibleOne (3226)

Well, just have a variable named counter near the top of your code after the inputs, initially set it to zero. Then we use the on_message() to see when I user makes a comment and add one to counter, like this:

counter = 0

@client.event
async def on_message(message):
  global counter
  if client.user == message.author:
    return # don't let yourself trigger the count
  counter += 1
  elif message.content.startswith('+counter'):
    await message.channel.send(f"Current Count: {str(counter)}")

That should do what you are looking for it to do, let me know if it doesn't.
@tggaming999