首页 > 解决方案 > 如何将 .random.choice() 函数与 url(特别是 gif)一起使用?

问题描述

我最近创建了一个非常简单的不和谐机器人,它会根据命令给你一个随机的选项引用。我也想做同样的事情,但使用 GIF。格式一样吗?

第一个代码是我的报价机器人编码,第二个是我尝试嵌入 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)

标签: pythonbotsdiscorddiscord.pydiscord.py-rewrite

解决方案


>>> 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))

推荐阅读