samedi 21 juillet 2018

Selecting a random online user in Discord.py

I'm writing a bot for a friend, and I'm looking to select a random user who is online with a command. My plan was to populate a list with the online users and then pick from that with this:

#Random Killing command
@client.command(name = "execute",
                 description = "Selects a random user for death.",
                 brief = "Randomly selects someone to die.",
                 aliases = ["RandDeath"])
async def Murderer():
    userList = []
    for member in server.members:
        if member.status == discord.Status.online:
            userList.append(member)
    picked = random.choice(userList)
    await client.say(picked + " was chosen!")

Unfortunately, my Python is pretty rusty, so this is proving to be more of a challenge than I was expecting. Executing this command causes this error: IndexError: Cannot choose from an empty sequence on this line: picked = random.choice(userList) I can't really figure out what went wrong, or what I can to to fix it, so any help is greatly appreciated. If there isn't enough code from the bot, just ask. I didn't want to just post everything since this is the bit I'm focused on.




Aucun commentaire:

Enregistrer un commentaire