1

How to make python bot pick a random name. For example if I provide a list of answers.

answers = ["apple", "ball", "cat", "dog", "elephant", "frog", "gun"]

@bot.command()  
async def choose(k : int):
    """Chooses between multiple choices."""
    if 0 <= k <= 50:
        await bot.say("This is your random {} pick".format(k))
        embed = discord.Embed(description='\n'.join(random.choices(answers, k=k)))
        await bot.say(embed=embed)
    else:
        await bot.say("Invalid number")
Demotry
  • 869
  • 4
  • 21
  • 40
  • https://stackoverflow.com/questions/6482889/get-random-sample-from-list-while-maintaining-ordering-of-items?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Sabri Karagönen Jun 06 '18 at 14:20
  • use random, use for loop to iterate x amount of times, inside loop print randint index of the list? something like that anyway. – L_Church Jun 06 '18 at 14:24
  • As for replying in the same channel, have you looked at the basic example bot from the github repo? https://github.com/Rapptz/discord.py/blob/async/examples/basic_bot.py – Patrick Haugh Jun 06 '18 at 14:26
  • That looks reasonable to me. What about it isn't working? – Patrick Haugh Jun 06 '18 at 14:38
  • @PatrickHaugh i updated my code it picks but only 1 answers. it needs to pick random xx amount mentioned from list. `!pick 10` also reply should be in embed. – Demotry Jun 06 '18 at 14:49

2 Answers2

2

You can use random.choices (not choice) to select n items with replacement, if you are on Python 3.6+

@bot.command()  
async def choose(k : int):
    """Chooses between multiple choices."""
    if 0 <= k <= 50:
        await bot.say("This is your random {} pick".format(k))
        embed = discord.Embed(description='\n'.join(random.choices(answers, k=k)))
        await bot.say(embed=embed)
    else:
        await bot.say("Invalid number")

@choose.error
def choose_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await bot.say("Please specify how many")
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • @Demotry See my edit. I'd double check that `Embed` code though, I'm not in a position to run it at the moment. – Patrick Haugh Jun 06 '18 at 15:28
  • You can provide a default value for `k` if you want. What do you expect to happen? – Patrick Haugh Jun 06 '18 at 20:53
  • [Here's an error handler example](https://stackoverflow.com/questions/49478189/discord-py-discord-notfound-exception/49478273#49478273). You will want to check for `commands.MissingRequiredArgument` instead of `commands.BadArgument` – Patrick Haugh Jun 06 '18 at 21:04
  • To get the name of the missing parameter, you can use the `param` attribute of the [`MissingRequiredArgument`](https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.MissingRequiredArgument) object `await bot.send_message(ctx.message.channel, "**Missing an argument:** " + error.param.name)` – Patrick Haugh Jun 06 '18 at 21:45
  • Yeah. `message.content` is `"?choose"`, so `message.content.split()` is `["?choose"]`. There is no element at index 1. – Patrick Haugh Jun 06 '18 at 22:01
  • so how to make it reply in same channel with custom message `"please mention how many"` after `?choose` – Demotry Jun 06 '18 at 22:04
  • Ok got it added a custom message. – Demotry Jun 06 '18 at 22:16
  • hi @patrick how to make it tag author. `await bot.say("This is your random {} pick".format(k))` for this line. `message.author.mention` – Demotry Jun 07 '18 at 10:34
-1
import random 

answers = ["apple", "ball", "cat", "dog", "elephant", "frog", "gun"]

def pick5(listOfAnswers):
    print("This is your random 5 pick:")

    for i in range(0, 5):
        myInt = random.randint(0,len(listOfAnswers)-1)
        print(listOfAnswers[myInt])
        listOfAnswers.remove(listOfAnswers[myInt])

pick5(answers)

This Python function picks 5 random elements out of a given list.

It iterates 5 times and uses a randomly selected integer to pull an element from the given list, then removes it to avoid duplicates. It uses the random module to accomplish this.

I don't know about getting it to discord, but I think this is what you were looking for.

Austin B
  • 184
  • 1
  • 1
  • 8