我如何在这里定义 ctx?

时间:2021-05-16 19:58:02

标签: discord.py

@client.event
async def on_message(message):
    if client.user.mentioned_in(message):
        embed = discord.Embed(title='Im here boss', description='Prefix: `n!help`', color=discord.Colour.from_rgb(249, 35, 142))
        embed.set_image(url='https://i.pinimg.com/originals/fe/1d/60/fe1d606ca0bcaf954a78cae3ed9c628b.gif')
        embed.set_footer(icon_url = ctx.author.avatar_url, text = f'Brought here by {ctx.author.name}')
        await message.channel.send(embed=embed)

如果我在 async def 中添加 ctx,它会显示一个错误并且它没有定义,而且在页脚中它也没有定义。我如何在这里定义这个东西中的ctx?

2 个答案:

答案 0 :(得分:2)

您可以使用 get_context 方法。
无论您需要在何处获取 ctx 变量,都可以添加如下内容:

ctx = await client.get_context(message)

您还可以输入自定义上下文作为第二个参数 (cls=MyCustomCtx)

答案 1 :(得分:1)

不要使用 ctx.author.* 使用 message.author.*,但如果您真的想获得 Context (ctx),请使用以下内容:

ctx = await client.get_context(message)