简体   繁体   中英

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)

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

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

The on_message event accepts message as a parameter.

Do this:

@bot.event
async def on_message(message: discord.Message) -> None:
   print(message.content)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM