如何从 on_message 事件中获取 Discord 上下文对象?
How can I get a Discord Context Object from the on_message event?
我目前正在开发一个机器人,实际上时间更长了,总是使用命令。命令的参数始终是上下文对象:
@client.command
async def test(context):
message = context.message
所以我围绕上下文对象构建了整个系统。现在我想使用 on_message 事件,但不是传递上下文对象,在这种情况下,Discord 传递消息对象:
@client.event
async def on_message(message):
...
如何使用消息对象获取上下文对象?
你应该使用get_context
@client.event
async def on_message(message):
ctx = await client.get_context(message)
通过使用异步函数,你在函数中使用过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
我目前正在开发一个机器人,实际上时间更长了,总是使用命令。命令的参数始终是上下文对象:
@client.command
async def test(context):
message = context.message
所以我围绕上下文对象构建了整个系统。现在我想使用 on_message 事件,但不是传递上下文对象,在这种情况下,Discord 传递消息对象:
@client.event
async def on_message(message):
...
如何使用消息对象获取上下文对象?
你应该使用get_context
@client.event
async def on_message(message):
ctx = await client.get_context(message)
通过使用异步函数,你在函数中使用过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