'How to pass ctx argument to own function?
It isn't exactly my code but similar example:
Function body:
async def fun(fun_word)
ctx.send(fun_word)
Then main code:
@client.event
async def on_message(message):
msg = message.content
if msg=="pancake"
fun(word)
I have tried many ways to pass ctx argument to function, but none of them worked.
Is any solution for this?
Solution 1:[1]
Await the function
You need to await the function. "Calling" a coroutine actually does nothing.
async def fun(fun_word):
ctx.send(fun_word)
@client.event
async def on_message(message):
msg = message.content
if msg == "pancake":
await fun(word)
Making context on the fly (albeit incomplete)
See the docs: https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=context#discord.ext.commands.Context. You can make your own context off the message given.
async def fun(ctx, fun_word):
ctx.send(fun_word)
@client.event
async def on_message(message):
msg = message.content
if msg == "pancake":
await fun(commands.Context(message=message), word)
Send the message normally
This is the cleanest way of achieving what you appear to want to do. Rather than forcing ctx, just send the message normally through message.channel. In fact, ctx.send is actually just an alias to this.
@client.event
async def on_message(message):
msg = message.content
if msg == "pancake":
await message.channel.send(word)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
