简体   繁体   中英

run command in on_message (discord.py)

  1. Can I use ctx in on_message() ?
  2. If not, then can I make the command work in on_message ?

Example:

# func.py
class function(self):   
    async def a(self, ctx):
    # content here


# bot.py
class Bot(self, commands.Bot):
    async def on_message(self, message):
    # make a() run here
  1. You can use ctx in on_message , by getting it with get_context(message) .
  2. You probably could execute a command by running it with argument got by get_context , but just using the message in on_message should be suitable for most tasks as it's similar to ctx and you can get all info almost the same way ( discord.message in the documentation ). Message is just like ctx.message .

Using message parameter in on_message() :

class Bot(self, commands.Bot):
    async def on_message(self, message):
        print(message.author) #prints author of the message
        await message.channel.send("Hello") #sends Hello to the channel (same result as with ctx.send)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM