简体   繁体   English

Discord.py bot.get_all_members 不工作

[英]Discord.py bot.get_all_members not working

Got no clue whats wrong with this i just want it to list all the people in the discord in the terminal不知道这有什么问题,我只想在终端中列出 discord 中的所有人

import os
import discord
from discord.ext import commands
from dotenv import load_dotenv

intents = discord.Intents.default()
intents.members = True 

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
bot  = commands.Bot(command_prefix = '£', intents=intents) 


@bot.event
async def on_ready():
    print (f'{bot.user} has connected to Discord!')
    print (bot.get_all_members)

Building on top of HamsterRecords response, you should be able to use list comprehension:HamsterRecords响应之上构建,您应该能够使用列表理解:

@bot.event
async def on_ready():
    print (f'{bot.user} has connected to Discord!')
    print ([member for member in bot.get_all_members()])

That's not how bot.get_all_members works, if you check out the documentation you can see it needs to be used in an for loop.However you could just use the example below which functions in the similar way as get_all_members() (there is probably a faster and more efficient way but I can't think of anything better right now)这不是bot.get_all_members的工作方式,如果您查看文档,您会发现它需要在 for 循环中使用。但是您可以只使用下面的示例,其功能与 get_all_members() 类似(可能有更快更有效的方法,但我现在想不出更好的方法)

all_members = []
for guild in bot.guilds:
    for member in guild.members:
        all_members.append(member.name)


@bot.event
async def on_ready():
    print (f'{bot.user} has connected to Discord!')
    print (all_members)

Edit编辑
Like as @Dominik said: For guild.members you need to have member intent enabled, otherwise its going to output an empty list正如@Dominik 所说:对于guild.members ,您需要启用成员意图,否则它将转到 output 一个空列表

You can just use len(bot.users) to grab the total amount of users that your bot can see.您可以只使用len(bot.users)来获取您的机器人可以看到的用户总数。 Like that:像那样:

print(len(bot.users))

Sources来源

Try尝试

len(set(bot.get_all_members()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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