Home > Back-end >  My discord bot is responding only to DMs but not in channels
My discord bot is responding only to DMs but not in channels

Time:10-09

somehow my bot is not responding to commands in servers, but he is responding in DM. Someone can help me?

My code:

import discord
from dotenv import load_dotenv
token = "****"
load_dotenv()
client = discord.Client(intents=discord.Intents.default() )

@client.event
async def on_ready():
    print(f"Bot logged in as {client.user}")

@client.event
async def on_message(message):
    if message.author != client.user:
        if message.content =='$hi':
            d = "hi"
            await message.channel.send(d)

client.run(token)

CodePudding user response:

The default Intent value is 3243773 which doesn't have GUILD_MEMBERS, GUILD_PRESENCES and MESSAGE_CONTENT intents.
One of these (most likely MESSAGE_CONTENT) means that your bot isn't sent the content of messages in servers. You can test this by just printing message.content and you should just see empty messages.
To fix you can use discord.Intents.all() which has all intents

# add some/all of these to see what works
client = discord.Client(intents = discord.Intents.all())

quick note: you can make your code cleaner by removing an indent level

@client.event
async def on_message(message):
    if message.author == client.user: return;
    if message.content =='$hi':
        d = "hi"
        await message.channel.send(d)

CodePudding user response:

import discord
token = "BOT_TOKEN"

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

@client.event
async def on_ready():
    print(f"Bot logged in as {client.user}")

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

        if message.content == '$hi':
            d = "hi"
            await message.channel.send(d)

client.run(token)

And enable all Privileged Gateway Intents in Discord Developer Poral > App Name > Bot > Privileged Gateway Intents. If you need any help developing your discord bot or need some other help related to coding then message me on discord RobertK#6151!

https://i.stack.imgur.com/V2EI6.png

  • Related