Python-为什么on_message会停止命令的工作?

发布于 2021-02-02 23:15:58

基本上,一切似乎都可以正常运行并启动,但是由于某些原因,我无法调用任何命令。我已经很轻松地环顾了一个小时,然后看了一些示例/观看视频,但我终生无法找出问题所在。代码如下:

import discord
import asyncio
from discord.ext import commands

bot = commands.Bot(command_prefix = '-')
@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.event
async def on_message(message):
    if message.content.startswith('-debug'):
        await message.channel.send('d')

@bot.command(pass_context=True)
async def ping(ctx):
    await ctx.channel.send('Pong!')

@bot.command(pass_context=True)
async def add(ctx, *, arg):

    await ctx.send(arg)

我在on_message中拥有的调试输出实际上可以正常工作并做出响应,并且整个bot都可以运行,没有任何异常,但是它只是不会调用命令。

关注者
0
被浏览
53
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    覆盖提供的默认值将on_message禁止运行任何其他命令。要解决此问题,请bot.process_commands(message)在的末尾添加一行on_message。例如:

    @bot.event
    async def on_message(message):
        # do some extra stuff here
    
        await bot.process_commands(message)
    

    默认值on_message包含对此协程的调用,但是当你用自己的协程覆盖它时on_message,你需要自己调用它。



  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    采用:

    >>> import datetime
    >>> datetime.datetime.now()
    datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)
    
    >>> print(datetime.datetime.now())
    2009-01-06 15:08:24.789150
    

    而只是时间:

    >>> datetime.datetime.now().time()
    datetime.time(15, 8, 24, 78915)
    
    >>> print(datetime.datetime.now().time())
    15:08:24.789150
    

    有关更多信息,请参见文档。

    要保存输入,可以datetimedatetime模块中导入对象:

    >>> from datetime import datetime
    

    然后datetime.从以上所有位置移除引线。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看