Python-为什么on_message会停止命令的工作?
基本上,一切似乎都可以正常运行并启动,但是由于某些原因,我无法调用任何命令。我已经很轻松地环顾了一个小时,然后看了一些示例/观看视频,但我终生无法找出问题所在。代码如下:
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都可以运行,没有任何异常,但是它只是不会调用命令。
-
覆盖提供的默认值将
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
,你需要自己调用它。 -
采用:
>>> 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
有关更多信息,请参见文档。
要保存输入,可以
datetime
从datetime
模块中导入对象:>>> from datetime import datetime
然后
datetime
.从以上所有位置移除引线。