[英]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}")
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.