Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord.py recognizing on_message client event, but IF statements not responding

I am working on a Discord bot and in this case want the bot to roll a 6 sided die when any user gives the "roll" command. See code below. Oddly enough this was working just fine, but now the print(worked) triggers and the if p_message statements do nothing. I get no errors, it just doesn’t work.

import discord
import random

intents = discord.Intents.all()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_message(message):
  p_message = message
  print("worked")

  if p_message == "roll":
    return str(random.randint(1, 6))

client.run('***') #my token

I have tried ensuring that message is a str but it makes no difference. Again, this code worked previously (at 1am when I was probably too tired to be writing code) and throws no error. It just doesn’t respond to the command at all in discord.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

import discord
import random

# change .all to .default
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)


@client.event
async def on_message(message):
    # use message.content
    if message.content == "roll":
        # send message to the channel that someone types "roll"
        await message.channel.send(random.randint(1, 6))


client.run('***')  # my token
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading