【问题标题】:Discord.py @bot.eventDiscord.py @bot.event
【发布时间】:2020-09-16 10:48:39
【问题描述】:

所以我有一个同时使用@bot.event@bot.command() 的脚本。问题是当我有一个@bot.event 等待时,@bot.command() 将无法运行。

这是我的代码:

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

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    当将on_message 事件与命令混合使用时,您需要添加await bot.process_commands(message),如下所示:

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

    如文档中所述:

    此函数处理已注册到 bot 和其他组的命令。没有这个协程,任何命令都不会被触发。

    如果您选择覆盖 on_message() 事件,那么您也应该调用此协程。


    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-10
      • 2021-01-16
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 2021-01-07
      • 2021-03-24
      • 2020-09-18
      相关资源
      最近更新 更多