Search code examples
randomdiscorddiscord.py

how do I write a random user who is on the server to the "user" variable?


I need to write the name of a random user on the server to a variable.

this is about how I present this code:

import discord
from discord.ext import commands
import random

@client.command()
async def hi(ctx):

    user = discord.utils.get(ctx.guild.members, name = random)

    await ctx.send(user + ' hi!')

Solution

  • You can get a list of all members with ctx.guild.members and then pick a random element of the list with random.choice(list):

    import discord
    from discord.ext import commands
    import random
    
    @client.command()
    async def hi(ctx):
        user = random.choice(ctx.guild.members)
    
        await ctx.send(user + ' hi!')