简体   繁体   English

Discord 列出所有成员 python

[英]Discord list all members python

I´m trying to make a bot to make a list of members that owns a role I ask (Command: $role Admin Answer: 3 people have role Admin) and when I will ask (Command: $role list Admin Answer: @Justyn, @JustBoy, @JustBoss).我正在尝试制作一个机器人来列出拥有我要求的角色的成员列表(命令:$role 管理员答案:3 人具有管理员角色)以及何时询问(命令:$role list 管理员答案:@Justyn ,@JustBoy,@JustBoss)。

I tried this code:我试过这段代码:

@bot.command(pass_context=True)
@commands.has_permissions(manage_messages=True)
async def members(ctx,*args):
    server = ctx.message.guild
    role_name = (' '.join(args))
    role_id = server.roles[0]
    for role in server.roles:
        if role_name == role.name:
            role_id = role
            break
    else:
        await ctx.send("Role doesn't exist")
        return
    for member in server.members:
        if role_id in member.roles:
            await ctx.send(f"{member.display_name} - {member.id}")

but when I ask $members Botz, the answer is only "Justyn Bot - 799779320906121236" (On the server I have 8 bots with role Botz but it only lists own bot (itself) ).但是当我问 $members Botz 时,答案只是“Justyn Bot - 799779320906121236”(在服务器上,我有 8 个角色为 Botz 的机器人,但它只列出了自己的机器人(本身))。 So I am confused.所以我很困惑。

If anyone knows how to do it please tell me.如果有人知道该怎么做,请告诉我。 Thank you.谢谢你。

Listing members列出成员

Guild.members requires intents.members enabled, to enable them: Guild.members需要启用intents.members才能启用它们:

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

bot = commands.Bot(..., intents=intents)

Also make sure to enable them in the developer portal , ( here's how )还要确保在开发者门户中启用它们,( 方法如下)

EDIT编辑

Listing members with a particular role列出具有特定角色的成员

async def whatever(ctx, role: discord.Role): # The `role` arg will be converted to a `discord.Role` instance
    await ctx.send(f"There's {len(role.members)} users with the role {role.name}")

Reference:参考:

You have to enable intents from Discord Developer Portal and also you have to define it in your code in order to use some events and methods like discord.Member.roles .您必须从Discord 开发人员门户启用意图,并且您必须在代码中定义它才能使用discord.Member.roles等一些事件和方法。

In Developer Portal, you have to do:在 Developer Portal 中,您必须执行以下操作:

Select Application -> Bot -> Privileged Gateway Intents -> Enable Presence Intent & Server Members Intent Select 应用程序 -> 机器人 -> 特权网关意图 -> 启用存在意图和服务器成员意图

In your code:在您的代码中:

import discord
from discord.ext import commands

intents = discord.Intents().all()
bot = commands.Bot(command_prefix="<prefix>", intents=intents)

For more information, you can check API References about intents .有关更多信息,您可以查看有关意图的 API 参考

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

相关问题 有没有办法用 discord.py 列出不和谐服务器的所有成员 - Is there a way to list all members of a discord server with discord.py 遍历成员列表以识别同时创建的所有帐户(Python、discord.py) - Iterating through members list to identify all accounts that were created at the same time (Python, discord.py) (discord.py) 获取特定语音频道中所有成员的列表 - (discord.py) Getting a list of all of the members in a specific voice channel Discord PY 如何获取所有服务器成员的列表? - Discord PY how to get list of all server members? 获取具有特定角色 discord bot 的所有成员的列表 - get list of all members with specific role discord bot 如何在discord.py中获取服务器中所有成员的列表? - How to obtain a list of all members in a server in discord.py? Discord.py 获取公会成员的所有名称和歧视(python) - Discord.py get all name and discrims for members in guild (python) 如何使用 discord.py 列出不和谐服务器中的所有成员? - How do I make a list of all members in a discord server using discord.py? 如何使用 discord.py 获取 discord 服务器中所有成员的列表 - How do I get a list of all of the members in a discord server using discord.py 如何使用新的discord.py版本获取discord服务器中所有成员的列表? - How do I get a list of all members in a discord server using the new discord.py version?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM