Invokage
Invokage

Reputation: 31

discord.py on_message no content

This usually always works, but for some reason my Message object has an empty "content" attribute, even when a normal message is sent (no embeds). Keep in mind im running this with the py-cord beta release.

from discord.ext import commands
bot = commands.Bot(command_prefix="$")

@bot.event
async def on_message(ctx):
    print(ctx.content) # Prints empty string

bot.run(token)

(All intents are already enabled in the developer portal)

Upvotes: 2

Views: 9152

Answers (1)

Falshazar
Falshazar

Reputation: 61

I have solved it. Since discord.py 2.0, you must now activate privleged intents for specific actions. Messages are one of those actions. So when you go to set up your intents into your function you must set intents.message_content = True

There are other intents to consider, but regarding this post, this is the most applicable Consider this site: https://discordpy.readthedocs.io/en/stable/intents.html for further reading on intents

def run_discord_bot():
    intents = discord.Intents.default()  
    intents.message_content = True
    client = discord.Client(intents = intents)

    @client.event
    async def on_ready():
        print(f'{client.user} is now running!')
        
    

    @client.event
    async def on_message(message):
        if message.author == client.user:
            return 


        username = str(message.author)
        user_message = str(message.content)
        channel = str(message.channel)

Upvotes: 6

Related Questions