简体   繁体   中英

get list of all members with specific role discord bot

i want the bot send all member with role named weed but intead it send only bot name

my code:

@commands.command(pass_context=True)
    @commands.has_permissions(manage_messages=True)
    async def mom(self,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}")

You can simply use role.members

@commands.command()            # ↓ This will make sure that the argument passed is a discord.Role object
async def mom(self, ctx, role: discord.Role):
    members = role.members
    for member in members:
        await ctx.send(f'{member.display_name} - {member.id}')

Reference:

I believe there is something called role.members which returns all the members with the role "role". Here is an example of the code:

@commands.command(pass_context=True)
@commands.has_permissions(manage_messages=True)
async def mom(self,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 role.members:
        await ctx.send(f"{member.display_name} - {member.id}")

Try to replace your command with this one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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