Reputation: 43
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!
Upvotes: 3
Views: 2137
Reputation: 416
Two things must be done before you can respond to messages within channels:
Within the developer portal, find your bot under applications. Selecting the "bot" tab from the left, you'll see the following option:
It needs to be enabled.
If you try the below without enabling it, you will get a permission error:
... from None discord.errors.PrivilegedIntentsRequired: Shard ID None is requesting privileged intents that have not been explicitly enabled in the developer portal. It is recommended to go to https://discord.com/developers/applications/ and explicitly enable the privileged intents within your application's page. If this is not possible, then consider disabling the privileged intents instead.
Specifying intent is done through the following lines. It is required in addition to the option being enabled in the developer portal.
intents = discord.Intents.default()
intents.message_content = True
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.
Consider using the commands extension of discord.py as it is handling some annoying stuff and provides more easier interfaces to handle commands.
Upvotes: 3
Reputation: 3883
@kejax has given absolutely the right answer I just want to add how u can make the use of
'intent'
in your code:-
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')
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')
Upvotes: 1