如何将.random.choice()函数与网址(特别是gif)一起使用?

时间:2020-07-08 11:16:40

标签: python bots discord discord.py discord.py-rewrite

我最近创建了一个非常简单的discord机器人,该机器人可在命令后为您提供选项的随机报价。我想做同样的事情,但是要用gifs。格式是否相同?

第一个代码是我的报价机器人代码,第二个代码是我尝试嵌入gif的尝试,但是我不确定如何将.random.choice()合并到此代码中。

@client.command(brief='-positivity', description='will give you a positive quote!')
async def positivity(ctx):
    quotes = [#choice1, #choice2, ...., choice#25]
    await ctx.send(f'**{random.choice(quotes)}**')

\\

@client.command(brief='-hug', desription="Will give you a warm hug")
async def hug(ctx):
    gif = discord.Embed(title = 'A hug has been sent!', description = 'warm, fuzzy and comforting <3', color = 0x83B5E3)
    gif.set_image(url = 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif')
    await ctx.channel.send(embed=gif)

1 个答案:

答案 0 :(得分:4)

>>> import random
>>> random.choice(['gif1', 'gif2', 'url3', 'hello world'])
'gif2'
>>> random.choice(['gif1', 'gif2', 'url3', 'hello world'])
'gif2'
>>> random.choice(['gif1', 'gif2', 'url3', 'hello world'])
'gif1'
>>> random.choice(['gif1', 'gif2', 'url3', 'hello world'])
'url3'
>>> random.choice(['gif1', 'gif2', 'url3', 'hello world'])
'gif1'
>>> random.choice(['gif1', 'gif2', 'url3', 'hello world'])
'url3'
>>> random.choice(['gif1', 'gif2', 'url3', 'hello world'])
'gif2'
>>> random.choice(['gif1', 'gif2', 'url3', 'hello world'])
'hello world'

我想您可能想用以下方法做这样的事情:

quotes = ['quote one', 'etc...']
await ctx.send(random.choice(quotes))