python—在服务器中遍历所有用户

fdbelqdn  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(262)

这是我的密码:

import discord
from discord.ext import commands, task

client = commands.Bot(command_prefix = '$')

@client.command(aliases=['au'])
async def all_users(ctx):
    for guild in client.guilds:
        for member in guild.members:
            print(member)

(我做到了 client.run 在那之后,我只是没有在这里显示我的令牌)
运行命令时的输出:

它只输出两次bot的id(服务器中有3个人)
我该怎么做才能让它打印公会中的所有用户?

vh0rcniy

vh0rcniy1#

必须启用 Intents.members 以前是这样吗 client = commands.Bot(command_prefix = '$') :

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

并更新 client = commands.Bot(command_prefix = '$')client = commands.Bot(command_prefix = '$', intents=intents) ,所以您的最终代码是:

import discord
from discord.ext import commands, task
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '$', intents=intents)

@client.command(aliases=['au'])
async def all_users(ctx):
    for guild in client.guilds:
        for member in guild.members:
            print(member)

您还必须在discord中启用应用程序站点中的成员意图(类似discord.com/developers/applications/your\u bot\u id\u here/bot):

参考文献:
不和成员

相关问题