1

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)

2 Answers 2

1

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)

9
  • Replace it with ` await bot.send_message(ctx.message.channel, "Line {} >{}\n".format(str(n),", ".join(cs)))`
    – Tristo
    Aug 26, 2018 at 9:33
  • So instead of "Line 1" you add a custom message? Depends on what you want to add
    – Tristo
    Aug 26, 2018 at 12:09
  • There's couple of ways you could do this, you can have an array of 3 people and every time it prints the colours use pop() to remove the last one from the array
    – Tristo
    Aug 26, 2018 at 12:56
  • 1
    await client.send_message(ctx.message.channel, "{}\n".format(f" Line {n} > " + ", ".join(cs)))
    – Tristo
    Aug 26, 2018 at 14:56
  • 1
    Above for n in [1, 2, 3]: put counter =0 and below it put counter += 1 and instead of {n} in the last thing I wrote, put {counter}
    – Tristo
    Aug 26, 2018 at 15:21
0
@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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.