简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM