Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

discord.py - Programming a Discord bot in Python- How do I make an embed have a random color?

I'm trying to make it so that whenever the bot sends an embed, the color for it is random. Here's my code:

colors = ['0xFFE4E1', '0x00FF7F', '0xD8BFD8', '0xDC143C', '0xFF4500', '0xDEB887', '0xADFF2F', '0x800000', '0x4682B4', '0x006400', '0x808080', '0xA0522D', '0xF08080', '0xC71585', '0xFFB6C1', '0x00CED1']

@client.command(help='Shares a meme')
async def meme(ctx):
    subreddit = reddit.subreddit("dankmemes")
    all_subs = []
    top = subreddit.top(limit = 75)

    for submission in top:
      all_subs.append(submission)
  
    random_sub = random.choice(all_subs)
    name = random_sub.title
    url = random_sub.url
    em = discord.Embed(title = name, color = random.choice(colors))

    em.set_image(url = url)
    await ctx.send(embed = em)

It gives me this error: TypeError: Expected discord.Colour, int, or Embed.Empty but received str instead.

Not sure how to fix this, any tips?

question from:https://stackoverflow.com/questions/65867118/programming-a-discord-bot-in-python-how-do-i-make-an-embed-have-a-random-color

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I guess, it must be

colors = [0xFFE4E1, 0x00FF7F, 0xD8BFD8, 0xDC143C, 0xFF4500, 0xDEB887, 0xADFF2F, 0x800000, 0x4682B4, 0x006400, 0x808080, 0xA0522D, 0xF08080, 0xC71585, 0xFFB6C1, 0x00CED1]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...