V
V
vlad2462020-10-28 21:52:22
Python
vlad246, 2020-10-28 21:52:22

Discord.py How to randomize with individual users with roles?

I have a code with randomness for roles, but I need it not to randomize the specified roles, for example, by id
The code itself:

@client.command()
@commands.has_permissions(administrator = True)
async def random(ctx, members, role_name):
    await ctx.message.delete()
    await ctx.send('Бот начал рандом...')
    memberlist = []
    for member in ctx.message.guild.members:
        memberlist.append(member)
    Rand_role = discord.utils.get(ctx.message.guild.roles, name = role_name)
    for i in range(int(members)):
        winner = choice(memberlist)
        await ctx.send(f'{winner} попал на рандоме')
        for i in ctx.message.guild.members:
            if i == winner:
                await i.add_roles(Rand_role)

If the user has the specified role in the code, then he will not be randomly selected, but if I randomize the specified role in the code, then he will randomize that specified role, and there is some minus that he can repeat in the random user who has already been randomized , it is not necessary for him to repeat the person who he already got at random.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2020-10-30
@vlad246

Two options that I see:
1. Recursive function

async def give_me_not_in_roles(members, roles): # members: List[discord.Member], roles: List[discord.Role]
    member = random.choice(members)
    if any(role in member.roles for role in roles):
        return await give_me_not_in_roles(members, roles)
    return member

@client.command(name="random")
@commands.has_permissions(administrator = True)
async def rnd(ctx, members: commands.Greedy[discord.Member], *roles: discord.Role):  # https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.Greedy
    m = await give_me_not_in_roles(members, roles)
    await ctx.send(f"{m.mention} won a whole nothing!")

2. Make a list of everyone who is not in the specified roles:
@client.command(name="random")
@commands.has_permissions(administrator = True)
async def rnd(ctx, members: commands.Greedy[discord.Member], *roles: discord.Role):  # https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.Greedy
    filtered = [m for m in members if not any(m in r.members for r in roles)]
    m = random.choice(filtered)
    await ctx.send(f"{m.mention} won a whole nothing!")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question