communication with my bot discord doesnt work

Question:

Hello im trying to communicate with my bot discord but its doesnt answer
the bot is online but no answer here the following code :

import discord


client = discord.Client(intents=discord.Intents.default())
client.run("token")

@client.event
async def on_message(message):
    if message.content == "ping":
        await message.channel.send("pong")

Asked By: Loran

||

Answers:

You need to enable the message content intent.
add this in your code under your intents definitions

intents.message_content = True

then head to the developer dashboard
and enable the Message Content at the Privileged Intents after that your code should work 😉

Answered By: Kejax

Having your bot to answer to sent messages requires the message_content intent.

With intents=discord.Intents.default() following intents are DISABLED:

  • self.presences (= having your bot to see rich presence, e.g. what game’s being played)
  • self.members (= having your bot to see the user of a guild)
  • self.message_content (= having your bot to see the content of messages)

You can now enable all mentioned intents or only specific intents. If you want to send a message, in response to a sent message, you need the intent self.message_content.

You can also add all intents to avoid any problems with them in the future (Note that after a certain amount of Discord servers you need to apply to use all privileged intents.)

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

You should consider activating the intents:
Visit the developer portal > choose your app > Privileged Gateway Intents.

For further programming in Discord.py, consider reading the docs, as there is a new version of Discord.py.

Answered By: ARealWant

The message_content intent mentioned above is necessary, but it’s not the only thing wrong here.

When you call client.run(), nothing below it will execute until the client goes down. This means that your on_message event is never created. client.run() should be the very last line in your file.

Answered By: stijndcl
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.