是否有方法使用discord.py列出discord服务器的所有成员

2024-04-20 03:00:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想检索一个包含discord服务器中所有人员的列表

import discord
from discord.ext import commands
TOKEN = 'my_token'
bot = commands.Bot(command_prefix = '!')

@bot.command()
async def members(ctx):
    for member in ctx.guild.members:
        await ctx.send(member)

bot.run(TOKEN)

无论何时使用!成员我只得到一个列表,其中列出了当前在语音频道中的所有成员,或者在服务器上以某种方式处于活动状态的所有成员,但我希望这些成员显示为在线或离线。 有人知道吗


Tags: fromimport服务器token列表人员bot成员
2条回答

Guild.members将返回公会成员列表

自>=1.5.0要求提升成员/状态事件的意图,您必须在开发人员门户和代码中启用成员意图

您还需要检查调用该命令的成员数,因此需要使用^{}装饰器并删除带有bot.get_guild()的行

使用guild_only()装饰器的原因是,如果在DM中调用!members,guild将为None,并将引发一个错误,表示'NoneType' object has no attribute called 'members'

自上次更新以来,您必须启用意图。从dev门户启用它们并自行编写代码

enter image description here

intents = discord.Intents().all()
bot = commands.Bot(command_prefix="$", intents=intents)

server = bot.get_guild(SERVER_ID)删除该行

相关问题 更多 >