Python Discord bot works in DM but not in server

Question:

I am trying to set up a simple Discord bot for a server, but it only appears to be responding in DMs to commands and not in any server channels. The bot has admin permissions in the server I am trying to get it to respond in.

After doing some looking around I have found no fixes.

Here’s the code:

import discord

token_file = open("bot_token.txt", "r+")
TOKEN = str(token_file.readlines()).strip("[]'")
token_file.close()

command_prefix = ">"

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

@client.event
async def on_ready():
    print("Logged in as: {0.user}".format(client))

@client.event
async def on_message(message):

    if message.author == client.user:
        return
    
    if message.content.startswith(command_prefix):
        if message.content == ">help":
            await message.channel.send("What do you need help with?")
        elif message.content == ">hello":
            await message.channel.send("Hi there!")
        else:
            await message.channel.send("This command is not recognised by our bot, please use the help menu if required.")
    else:
        return

client.run(TOKEN)

Hope someone can help!

Asked By: TheEmeraldFishy

||

Answers:

It‘s pretty simple…
For receiving message events in guilds (servers) you need the message intent (discord.Intents.message_content = True), you also have to enable it in the discord developer dashboard.

Discord added a message content intent that has to be used since the first September 2022. if you‘re doing a discord.py tutorial, be aware that it should be a 2.0 tutorial as many things have been updated since then.

Also, think of using the commands extension of discord.py as it is handling some annoying stuff and provides more easier interfaces to handle commands.

I‘ll help you with further questions if there are any
😉

Answered By: Kejax

@kejax has given absolutely the right answer I just want to add how u
can make the use of 'intent' in your code:-


If you want to listen and have access to all available events and data

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

Full Code :-

import discord

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

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('................')

@client.event
async def on_message(message):
    if message.content.startswith('!greet'):
        await message.channel.send('Hello!')

client.run('YOUR_BOT_TOKEN')

You can also specify specific intents by creating a set of the desired discord.Intents constants.

intents = {discord.Intents.guilds, discord.Intents.messages}
client = discord.Client(intents=intents)

Full Code:-

import discord

intents = {discord.Intents.guilds, discord.Intents.messages}
client = discord.Client(intents=intents)


@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('....................')

@client.event
async def on_message(message):
    if message.content.startswith('!greet'):
        await message.channel.send('Hello!')

client.run('YOUR_BOT_TOKEN')
Answered By: Loop Assembly
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.