How can I get a Discord Context Object from the on_message event?

Viewed 5579

Im currently working on a bot, actually for a bit longer now, always with commands. The argument for a command is always a Context Object:

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

So I've build my whole system around the Context Object. Now I wanted to use the on_message event, but instead of passing a Context Object, in this case, Discord passes a message Object:

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

How can I get the Context Objext using the message Object?

2 Answers

You should use get_context

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

By using asynchronous function, have you used await in functions,

@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
Related