如何从on_message事件获取Discord上下文对象?

时间:2020-09-09 21:39:21

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

我目前正在使用机器人,实际上要花更长的时间,并且总是使用命令。命令的参数始终是上下文对象:

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

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

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

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

2 个答案:

答案 0 :(得分:1)

您应该使用get_context

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

答案 1 :(得分:1)

通过使用异步函数,您是否在函数中使用了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