I'm playing around with the Discord API and noticed that I can't access the content of a message.
This is my code:
import discord
client = discord.Client()
@client.event
async def on_ready():
print(f'Logged in as {client.user}')
@client.event
async def on_message(message):
if 'My Name' in message.author.name:
print(f'Author: {message.author.name}')
print(f'Content: {message.content}')
print(f'Clean_Content: {message.clean_content}')
print(f'System_Content: {message.system_content}')
client.run(TOKEN, bot=False)
Note that the token and my username are kept private in this post for obvious reasons.
This is the output that I get, no matter the message:
Author: My Name
Content:
Clean_Content:
System_Content:
As you can see I have also tried the clean_content and system_content attributes. However, none of them show the actual message. I've also tried to use a bot account and that surprisingly worked, but I want this to work with my own account. Is the problem that Discord does not intent private clients to read messages or did I miss something fundamental?
As Matt mentioned, user bots are not supported. However, your problem may be related to intents instead. You might try using the following lines:
import discord
intents = discord.Intents.all()
client = discord.Client(intents=intents)
See also:
You can solve this by enabling the MESSAGE CONTENT INTENT for your bot in the Discord Developer Panel.
import os
TOKEN = os.environ['TOKEN']
import discord
# This example requires the 'message_content' privileged intent to function.
class MyClient(discord.Client):
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
async def on_message(self, message):
# we do not want the bot to reply to itself
if message.author.id == self.user.id:
return
if message.content.startswith('!hello'):
await message.reply('Hello!', mention_author=True)
intents = discord.Intents.default()
intents.message_content = True
def main():
client = MyClient(intents=intents)
client.run(TOKEN)
if __name__ == '__main__':
main()
This is an example with reply in the documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With