【问题标题】:How can I get a Discord Context Object from the on_message event?如何从 on_message 事件中获取 Discord 上下文对象?
【发布时间】:2020-12-28 08:58:23
【问题描述】:

我目前正在开发一个机器人,实际上已经工作了一段时间,总是使用命令。命令的参数始终是上下文对象:

@client.command
async def test(context):
    message = context.message

所以我已经围绕上下文对象构建了我的整个系统。现在我想使用 on_message 事件,但不是传递上下文对象,在这种情况下,Discord 传递的是消息对象:

@client.event
async def on_message(message):
    ...

如何使用消息对象获取上下文对象?

【问题讨论】:

  • messagecontext 参数完全不同。您无法从消息中获取上下文。

标签: python discord discord.py discord.py-rewrite


【解决方案1】:

你应该使用get_context

@client.event
async def on_message(message):
    ctx = await client.get_context(message)

【讨论】:

    【解决方案2】:

    通过使用异步函数,你有没有在函数中使用await

    @client.command
    async def test(context):
        message = context.message
        #This will be stored in context which must be retrieved
    
    @client.event
    async def on_message(Message):
        ctx = await client.get_context(Message)
        #Do whatever you want
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      相关资源
      最近更新 更多