Demotry
Demotry

Reputation: 909

Python Random choice result in discord.py

how to make below code work in discord channel. the output should be in discord channel when used command.

colours = {'red', 'blue', 'green', 'yellow', 'black', 'purple',
           'Brown', 'Orange', 'violet', 'gray'}
for n in [5]:
    cs = random.sample(colours, k=n)
    colours -= set(cs)
    print(cs)

Upvotes: 1

Views: 3668

Answers (2)

Tristo
Tristo

Reputation: 2408

Here is the working code

@bot.command(pass_context=True)  
async def pick(ctx):
  colours_copy = colours.copy()
  for n in [1, 2, 3]:
      cs = random.sample(colours_copy , k=n)
      colours_copy -= set(cs)
      await bot.send_message(ctx.message.channel, "{}\n".format(", ".join(cs)))

If you were to subtract the set from colours every time someone ran !pick you would run out of colours pretty quickly

Instead, you can make a copy of your set inside the function so that even when you subtract from it during its execution, the original set will always be there as a reference

"the output should be in discord channel when used command !choose"
You can change what the command name is (it's currently pick)

Upvotes: 1

Mehvix
Mehvix

Reputation: 308

@client.event
    async def on_message(message):
        if message.content.upper() == ".CHOOSE":

            # Your code here

            await client.send_message(message.channel, "> {}\n".format(", ".join(cs)))

Should have the bot send the output just how it looks in the console to the respective channel the command is used in.

Upvotes: 0

Related Questions