【问题标题】:How can i define ctx here?我如何在这里定义 ctx ?
【发布时间】:2021-05-17 04:10:27
【问题描述】:
@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?

【问题讨论】:

    标签: discord.py


    【解决方案1】:

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

    ctx = await client.get_context(message)
    

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

    【讨论】:

      【解决方案2】:

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

      ctx = await client.get_context(message)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        • 2021-06-09
        • 2022-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多