Discord.py @bot.event

筅森魡賤 提交于 2021-02-19 05:35:52

问题


So I have a script that uses both @bot.event and @bot.command(). The problem is that when I have a @bot.event waiting the @bot.command() will not run.

Here is my code:

@bot.event
async def on_ready():
    print("Bot Is Ready And Online!")
    
async def react(message): 
    if message.content == "Meeting":
        await message.add_reaction("👍")

@bot.command()
async def info(ctx):
    await ctx.send("Hello, thanks for testing out our bot. ~ techNOlogics")

@bot.command(pass_context=True)
async def meet(ctx,time):
    if ctx.message.author.name == "techNOlogics":
        await ctx.channel.purge(limit=1)
        await ctx.send("**Meeting at " + time + " today!** React if you read.")

@bot.event ##THIS ONE HOLDS UP THE WHOLE SCRIPT
async def on_message(message):
    await react(message)

回答1:


When using a mixture of the on_message event with commands, you'll want to add await bot.process_commands(message), like so:

@bot.event
async def on_message(message):
    await bot.process_commands(message)
    # rest of code

As said in the docs:

This function processes the commands that have been registered to the bot and other groups. Without this coroutine, none of the commands will be triggered.

If you choose to override the on_message() event, you then you should invoke this coroutine as well.


References:

  • Bot.process_commands()
  • on_message()


来源:https://stackoverflow.com/questions/62076257/discord-py-bot-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!