Home > Software design >  Why isn't on_message working with my discordpy code?
Why isn't on_message working with my discordpy code?

Time:10-31

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!")

CodePudding user response:

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)
  • Related