massping command - discord.py

cap

I am trying to make a command that gets 5 random people from a server and mentions them. What I have so far is this:

@bot.command()
async def massping(ctx):
    await ctx.message.delete()
    user = choice(ctx.message.channel.guild.members)
    user2 = choice(ctx.message.channel.guild.members)
    user3 = choice(ctx.message.channel.guild.members)
    user4 = choice(ctx.message.channel.guild.members)
    user5 = choice(ctx.message.channel.guild.members)

    message = await ctx.send(f'{user.mention}{user2.mention}{user3.mention}{user4.mention}{user5.mention}')
    await message.delete()

The problem with this is that sometime it pings the same person twice. Is there any way around this so I can ensure it always pings 5 different people?

Any help appreciated.

Jawad

Guild.members returns a list of Member objects, you can use random.sample to return unique elements from the list.

async def ping(ctx, num: int):
    members = [m.mention for m in ctx.guild.members]
    rand = random.sample(members, num)
    await ctx.send("\n".join(rand))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related