Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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!")
@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 questionAsk a Question
731 491 924 answers to any question