首页 > 解决方案 > 如何根据嵌入的数组更改嵌入页脚?

问题描述

我正在尝试制作一个角色扮演机器人命令,其中用户标记另一个人,然后机器人必须说些什么,然后它出现一个随机图像,页脚必须说哪个是图片中的角色,我什至想这样做更多的数组,每个字符一个,但我不知道我是否让自己复杂化了很多,即使我所做的没有工作,我也不知道为什么。

#Example for  StackOverflow
@bot.command()
async def roleplaying (ctx, user:discord.User):
    nyan_cat= ["https://1.bp.blogspot.com/-XBOvQhJ4e3E/YINVEJxvRyI/AAAAAAAAATo/S-PlEpwNLzs250tmpWEzkmiLt_Fbeu5UACLcBGAsYHQ/s476/Nyan%2Bcat.gif"]
    pikachu = ["https://c.tenor.com/-VYWaSmWx2QAAAAC/thunderbolt-pikachu.gif"]

    random_image= random.choice([nyan_cat,pikachu])
    avc = discord.Embed(
        title= "",
        description= f"**{ctx.message.author.name}** wants **{user.name}** to watch the picture ❤️",
        colour=0x0101df
        )
    if random_image == nyan_cat:
        avc.set_image(url=random_image)
        avc.set_footer(text= "Character: Nyan Cat.")
        await ctx.send(embed=avc)
    elif random_image == pikachu:
        avc.set_image(url=random_image)
        avc.set_footer(text= "Character: Pikachu.")
        await ctx.send(embed=avc)

标签: pythondiscorddiscord.py

解决方案


我稍微更改了您的代码,对我来说它工作正常,如下所示:

@bot.command()
async def roleplaying (ctx, user:discord.User):
    nyan_cat= "https://1.bp.blogspot.com/-XBOvQhJ4e3E/YINVEJxvRyI/AAAAAAAAATo/S-PlEpwNLzs250tmpWEzkmiLt_Fbeu5UACLcBGAsYHQ/s476/Nyan%2Bcat.gif"
    pikachu = "https://c.tenor.com/-VYWaSmWx2QAAAAC/thunderbolt-pikachu.gif"

    random_image= random.choice([nyan_cat, pikachu])
    avc = discord.Embed(title= "", 
                        description= f"**{ctx.message.author.name}** wants **{user.name}** to watch the picture ❤️", 
                        colour=0x0101df)
    if random_image == nyan_cat:
        avc.set_image(url=random_image)
        avc.set_footer(text= "Character: Nyan Cat.")
        await ctx.send(embed=avc)
        
    elif random_image == pikachu:
        avc.set_image(url=random_image)
        avc.set_footer(text= "Character: Pikachu.")
        await ctx.send(embed=avc)

编辑: 功能在数组中添加了多个 gif。很确定这不是最好的方法,但它确实有效。

@bot.command(name='roleplaying')
async def roleplaying(ctx, user:discord.User):
    
    nyan_cat= [ "https://1.bp.blogspot.com/-XBOvQhJ4e3E/YINVEJxvRyI/AAAAAAAAATo/S-PlEpwNLzs250tmpWEzkmiLt_Fbeu5UACLcBGAsYHQ/s476/Nyan%2Bcat.gif", "https://icegif.com/wp-content/uploads/nyan-cat-icegif-5.gif" ]
    pikachu = [ "https://c.tenor.com/-VYWaSmWx2QAAAAC/thunderbolt-pikachu.gif", "https://c.tenor.com/P9pPZ1prRCMAAAAd/pokemon-pikachu.gif" ]

    random_char = random.choice(['nyan_cat', 'pikachu'])
    if random_char == 'nyan_cat':
        random_image = random.choice(nyan_cat)
    elif random_char == 'pikachu':
        random_image = random.choice(pikachu)
    
    avc = discord.Embed(title= "", 
                        description= f"**{ctx.message.author.name}** wants **{user.name}** to watch the picture ❤️", 
                        colour=0x0101df)
    if random_char == 'nyan_cat':
        avc.set_image(url=random_image)
        avc.set_footer(text= "Character: Nyan Cat.")
        await ctx.send(embed=avc)
        
    elif random_char == 'pikachu':
        avc.set_image(url=random_image)
        avc.set_footer(text= "Character: Pikachu.")
        await ctx.send(embed=avc)

推荐阅读