Python discord bot doesn't answer to any commands - python

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.

Related

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.

Py-cord bot goes online but doesnt respond

I have been trying to make a discord bot but when I put this piece of code tougether the bot turned online but if I use the command there is no response from the bot.
I used the import command from their github
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command()
async def test(ctx):
await ctx.send(content="Test")
bot.run('token')
Thanks.
With the latest Pycord versions (starting from 2.0.0b5 if I recall correctly), they use a Discord gateway version high enough that you have to enable the (privileged) message content intent both on your application's developer site (https://discord.com/developers/applications/YOUR_APPLICATION_ID/bot, but you can also go to https://discord.com/developers/applications and then navigate to your bot page from there)
and in your code:
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
With this setup, your bot will receive message events with the content and can actually process your commands
Note that once your bot reaches 100 servers, this intent will require you to undergo a whitelisting process by Discord. Until then, you can freely enable the intent to test and build your bot

Slash commands produce "interaction failed" when invoked in chat?

I am using the discord-py-slash-command library and have setup my commands. However when I type my command in the chat I get interaction failed each time. I have given my bot the correct scope and reinvited it to the server several times. Can someone take a look at my code an tell me what is going on? I have followed several tutorials with no luck.
TOKEN = open("./token.txt", "r").readline()
bot = commands.Bot(command_prefix='-', intents=discord.Intents.all())
slash = SlashCommand(bot, sync_commands=True)
#bot.event
async def on_ready():
print(f'{bot.user.name} has connected to the Discord!')
#slash.slash(name="test", description='Test slash command activated', guild_ids=
[928451150515150890])
async def test(ctx: SlashContext):
embed = discord.Embed(title="Embed Test")
await ctx.send(embed=embed)
Edit: I am editing my answer because at the time I was not sure what library you were using.
Your code works just fine, however, If you are going to use intents=discord.Intents.all()
you need to be sure to enable presence as well as server members intents in the developer console of the bot.
If you do not need to use intents for your application, disable them and set it to intents=discord.Intents.default()
i switched over to the nextcord library for slash commands and the behavior is working as expected... I will consider this closed

printing bot messages discord.py

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!)

Why is my discord bot code not running anything?

Okay so I wanted to make a discord bot for my first python project. However, despite my seemingly correct code, the bot doesn't run:
I used this basic code to simply get the bot online but nothing happens. I haven't seen anyone else have this problem either.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Bot is ready")
client.run("Token") # i removed the token for security
and this is the result of the program
Am I doing something wrong?
I am using this version of the discord botting API: https://github.com/Rapptz/discord.py
As #jwjhdev said, client.run is not supposed to be tabbed under on_ready. If you delete the tab, then everything should work perfectly.

Categories