Why isn't on_message working with my discordpy code?

Question:

My on_message code isn’t working can someone tell me what’s wrong or tell me what I did wrong?
There are no errors in the console saying I did something wrong also the bot has permissions to send messages.

@client.listen('on_message')
async def on_message(message):
  if message.content.lower == "good morning":
    await message.channel.send(f"Good Morning!")
  elif message.content.lower == "goodmorning":
    await message.channel.send(f"Good Morning!")
  elif message.content.lower == "gm":
    await message.channel.send(f"Good Morning!")
  elif message.content.lower == "good night":
    await message.channel.send(f"Good Night!")
  elif message.content.lower == "goodnight":
    await message.channel.send(f"Good Night!")
  elif message.content.lower == "gn":
    await message.channel.send(f"Good Night!")
Asked By: CodingSap

||

Answers:

message.content is a str, making message.content.lower a function which will never be equal to any of your string conditions.

Additionally, your code will be more efficient if you compute message.content.lower() once at the start of the function instead of once per conditional and you can also reduce the number of conditionals for readability.

Try this…



My on_message code isn't working can someone tell me what's wrong or tell me what I did wrong? There are no errors in the console saying I did something wrong also the bot has permissions to send messages.

@client.listen('on_message')
async def on_message(message):
  msg = message.content.lower()
  r = None
  if msg in ["good morning", "goodmorning", "gm"]:
    r = "Good Morning!"
  elif msg in ["good night", "goodnight", "gn"]:
    r ="Good Night!"

  if r is not None:
      await message.channel.send(r)
Answered By: Modularizer
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.