discord bot not reading commands - python

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)

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

How do I get all members the discord client can see?

I was trying to use the common method of:
for m in message.guild.members:
print(m)
But it only gave out the bot himself no one different.
Any solution ideas?
To view members, you will need to activate members intents. You can find this in your Developer's portal located as in the image below.
To activate intents, you will need to include the following in your code:
import discord
intents = discord.Intents.default()
intents.members = True
# Somewhere else (depending on what you're using):
# client = discord.Client(intents=intents)
# or
# from discord.ext import commands
# bot = commands.Bot(command_prefix='!', intents=intents)
Other than that, your code should be working.
You can read more about privileged intents here.

discord py bot sees only one member

I'm trying to get the # of all users on server, the bot sees only itself although it responds to the messages of the others.
client = discord.Client()
#client.event
async def on_message(message):
msg=message.content
if message.author == client.user:
return
if msg.startswith("count"):
await message.channel.send(client.users)
The code outputs a list with one user (bot itself)
You need to enable the Intents, they are missing.
Make sure to turn them on in the Discord Developer Portal for your application (Settings -> Bot).
To implement them to your code you can use the following:
intents = discord.Intents.all() # Imports all the Intents
client = commands.Bot(command_prefix="YourPrefix", intents=intents)
Or in your case:
intents = discord.Intents.all() # Imports all the Intents
client = discord.Client(intents=intents)
You can read more in the Docs here.
Well the issue you're having is because since version 1.5 (not 1.6 as i initially tought, thanks Łukasz Kwieciński) discord.py needs you to specify the intents your bot needs in order to receive such information from discord's API.
Your code will work if you change this:
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
For it to work though you'll need to go to your bot's page and enable the members intent.
For what i can see in your code it appears you're trying to make a command, i strongly suggest you start using the commands extension. Your code would look like this:
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='.', intents=intents)
#bot.command()
async def count(ctx):
await ctx.send(bot.users)
bot.run(TOKEN)
The resulting code is more compact, readable and maintainable.
You can check other examples 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