Reputation: 1
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)
Upvotes: 0
Views: 469
Reputation: 642
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
Upvotes: 0
Reputation: 3592
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.
Upvotes: 2