如何将用户名插入页脚?

2024-09-29 02:24:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一条不和谐的嵌入消息的页脚插入一个用户名,上面说是谁请求了命令,我尝试了各种各样的方法,但我不能让它按我想要的方式工作。任何帮助都将不胜感激。我希望它进入price命令

@client.command(pass_context=True)
async def ping(ctx):
    username = ctx.message.author.display_name
    channel = ctx.message.channel
    t1 = time.perf_counter()
    await client.send_typing(channel)
    t2 = time.perf_counter()
    embed=discord.Embed(title="Pong at {username}".format(username=username), description='It took {}ms.'.format(round((t2-t1)*1000)), color=0xDA70D6)
    await client.say(embed=embed) 
@client.command(aliases= ['price', 'p'])
    async def calc(quantity: int, itemA: str, itemB: str):
        #itemAPrice = get_value(itemA)
        #itemBPrice = get_value(itemB)
        itemAPrice = items[aliases[itemA]]
        itemBPrice = items[aliases[itemB]]
        if itemAPrice and itemBPrice:
            itemQuotient = itemAPrice/itemBPrice
            itemBEquivalent = round(quantity * itemQuotient, 2)
            embed=discord.Embed(title="Exchange Rate", description='{quantity} {itemA} is equal to {itemBEquivalent} {itemB}'.format(quantity = quantity, itemA = itemA, itemBEquivalent = itemBEquivalent, itemB = itemB), color=0xDA70D6)
            await client.say(embed=embed)
        elif not itemAPrice:
            await client.say('No match found for ' + itemA)
        elif not itemBPrice:
            await client.say('No match found for ' + itemB)

Tags: clientformatchannelusernameembedawaitquantitysay
2条回答

将调用上下文传递到命令中,然后使用该上下文获取调用该命令的用户的名称。在那里,您可以使用ctx.message.author.name,并将其插入页脚:

@client.command(aliases= ['price', 'p'], pass_context=True)
async def calc(ctx, quantity: int, itemA: str, itemB: str):
    itemAPrice = items[aliases[itemA]]
    itemBPrice = items[aliases[itemB]]
    if itemAPrice and itemBPrice:
        itemQuotient = itemAPrice/itemBPrice
        itemBEquivalent = round(quantity * itemQuotient, 2)
        embed=discord.Embed(title="Exchange Rate", description='{quantity} {itemA} is equal to {itemBEquivalent} {itemB}'.format(quantity = quantity, itemA = itemA, itemBEquivalent = itemBEquivalent, itemB = itemB), color=0xDA70D6)
        embed.set_footer(text="Command invoked by {}".format(ctx.message.author.name))
        await client.say(embed=embed)
    elif not itemAPrice:
        await client.say('No match found for ' + itemA)
    elif not itemBPrice:
        await client.say('No match found for ' + itemB)

如果您想提到用户,可以使用ctx.message.author.mention

相关问题 更多 >