printing bot messages discord.py - python

Are there any changes in discord.py ?
Since yesterday my bots doesnt work.
They were supposed to mention someone when another bot writes someting
async def on_message(message):
if message.author.id == 123456789: #any bot ID
if message.content.startswith(
"Alright"): #message
await message.channel.send("<#123213213321231> look")
print("Done")
I tried to print messages from specific channel to see if there are any changes in bots messages but i cant even print them..
async def on_message(message):
if message.channel.id == 123456789:
print(message.content)
Carlbot for example - it prints my messages but carlbot messages are blank space...
abc
!lock
!unlock
?remindme 1min blahblah
I didnt change anything it was working for over a month and suddenly it doesnt and i have no idea why.

Do you have intents enabled?
(from d.py server!)
What are intents?
As of 2020-10-28, discord requires users declare what sort of information (i.e. events) they will require for their bot to operate.
This is done in the form of intents.
You can read more about intents here: https://discordpy.readthedocs.io/en/latest/intents.html
By default some intents such as members and presences are disabled as they are what is referred to as privileged. Meaning you'll need to take some extra steps to support them.
Read up on privileged intents here: https://discordpy.readthedocs.io/en/latest/intents.html#privileged-intents
If you use on_member_* events in your code, without changes your bot will not function anymore!
Steps on how to enable them:
1 - in code
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=...., intents=intents)
2 - on discord developer portal
applications > yourApplication > bot > scroll down then check these https://imgur.com/a/oE7LrIa (only ones you need!)

Related

How can I fix the problem with my discord bot that doesn't want to react to messages

I wanted to test some new things so I wanted to create a bot discord so I started to do something very basic :
import discord
client = discord.Client(intents=discord.Intents.default()
DISCORD_TOKEN = "my token"
#when the bot is online
#client.event
async def on_ready():
print("The bot is online")
#the bot reacts to messages
#client.event
async def on_message(message):
print(message.content)
client.run(DISCORD_TOKEN)
and when I run the code the bot goes online but the bot doesn't react to the message nothing happens so sure the code is basic for now but the bot was supposed to react to the message in the terminal right?
this is the first time I use the discord.py library... so Can someone help me to solve this problem please I would be very grateful
Pretty sure the issue is that you haven't enabled the message_content intent - you need to set that to be able to read message content.
intents = discord.Intents.default()
intents.message_content = True # explicitly enable the message content intents
client = discord.Client(intents=intents)
You will also need to enable this intent for your bot/application in the Discord Developer portal.
The message_content intent is on the first page of the docs.
Discord intents.

communication with my bot discord doesnt work

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")
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 ;-)
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.
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.

discord bot not reading commands

from discord.ext import commands
bot = commands.Bot(command_prefix='$')
#bot.command()
async def test(ctx):
print("aaaa")
await ctx.send('aaaa')
bot.run(TOKEN)
when I type in discord "$test" nothing happens, not event the print statement is called
As mentioned in this Discord post, you have to manually enable the message content intent through the developer portal. After doing so, you can use the following code:
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
To improve privacy bots now need a special intent in order to be able to read messages. The "new" commands (which i would recommend you to switch to) are slash commands as they are fully supported by Discord. You can find information on how to set up slash commands here.
If you want to stick with the old method, here is he to activate the intent: First go to your Developer Portal, select your application and activate the Message Content Intent. Next you need to activate the intent in your code like this:
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
Note that if your bot is on 100 or more servers it needs to be verified in order to access intents. More information on that can be found here.
The previous answers are correct, you have to manually enable the message content intent in discord developer portal and in your code, but "intents.message_content = True" is incorrect for discord.py.
The correct code would be:
intents = discord.Intents.default()
intents.messages = True
bot = commands.Bot(command_prefix='$', intents=intents)

Python discord bot doesn't answer to any commands

The bot starts just fine, but it doesn't answer to any commands I make with its prefix, it doesn't even raise the "command not found" exception. The bot was working just fine until some hours ago, I made some changes and it stopped working. I reverted the changes, but it still doesn't work.
I tried commenting the cogs startup and it still didn't work, so I think they're not relevant to the problem. I also left a lot of the code out because it's pretty big, and I assumed it didn't matter because the bot is running and if there were any errors in my code, the commands should at least be executed, but let me know if there's something else I should include.
Here's the code:
(imports)
(global variables)
bot = commands.Bot(command_prefix='!', help_command=None, case_insensitive=True,
intents=discord.Intents.default())
#bot.event
async def on_ready():
(cogs setup)
await bot.change_presence(activity=discord.Game(name='random status'))
print("My prefix is: " + bot.command_prefix)
print('We have logged in as {0.user}'.format(bot))
#bot.command()
async def random_command(ctx):
print("Command executed)
def random_method():
(does something)
bot.run(token)
The "on_ready" event is executed just fine, and the bot comes online in discord. I don't have any on_message events and the bot has admin perms in my server.
To improve privacy bots now need a special intent in order to be able to read messages. The "new" commands (which i would recommend you to switch to) are slash commands as they are fully supported by Discord. You can find more information on how to set up slash commands here.
If you want to stick with the old method, here is he to activate the intent: First go to your Developer Portal, select your application and activate the Message Content Intent. Next you need to activate the intent in your code like this:
intents = discord.Intents.default()
intents.messages = True
bot = commands.Bot(command_prefix='!', help_command=None, case_insensitive=True, intents=intents)
Note that if your bot is on 100 or more servers it needs to be verified in order to access intents. More information on that can be found here.

Discord.py on_member_join not working, no error message

I am trying to make a discord bot with the Discord.py library. The commands with the #client.command() decorator work fine for me, but none of the event ones that I tried work.
#client.event
async def on_member_join(member):
channel = client.get_channel(ChannelId) #I did define channel Id in my code
await channel.send("someone has joined")
#client.event
async def on_member_remove(member):
print("Someone has left")
I would expect this to output to the terminal or in the channel id I put in, but nothing appears, not even an error message.
*I used client. for all functions.
*I am doing this on mac.
Im not quite sure why its working, I do not get any error messages, and I cant seem to find anyone else with this problem.
Thanks in advance
With version >1.5.0 you can do something like this:
import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
#client.event
async def on_member_join(member):
await member.send("Welcome!")
You also need to enable intents in the developer portal in https://discord.com/developers/applications. Bot > Bot > Presence & Server Members Intents > Toggle On
import discord
intents = discord.Intents.all()
discord.member = True
bot = commands.Bot(command_prefix="[",intents = intents)
and you need to go to developer portal --> applications(select your bot)
and under the setting have a bot. Click it under the page has PRESENCE INTENT and SERVER MEMBERS INTENT you need to open it. That will be working.
If you are using discord.py v1.5.0, see the docs for Gateway Intents

Categories