当我on_message()在我的代码中时,它会停止所有其他@bot.command命令的工作。我试过了await bot.process_commands(message),但这也不起作用。这是我的代码:
@bot.event
@commands.has_role("Owner")
async def on_message(message):
if message.content.startswith('/lockdown'):
await bot.process_commands(message)
embed = discord.Embed(title=":warning: Do you want to activate Lock Down?", description="Type 'confirm' to activate Lock Down mode", color=0xFFFF00)
embed.add_field(name="\u200b", value="Lock Down mode is still in early development, expect some issues")
channel = message.channel
await bot.send_message(message.channel, embed=embed)
msg = await bot.wait_for_message(author=message.author, content='confirm')
embed = discord.Embed(title=":white_check_mark: Lock Down mode successfully activated", description="To deactivate type '/lockdownstop'", color=0x00ff00)
embed.add_field(name="\u200b", value="Lock Down mode is still in early development, expect some issues")
await bot.send_message(message.channel, embed=embed)
Run Code Online (Sandbox Code Playgroud)
您必须放置await bot.process_commands(message)在if语句范围之外,process_command无论消息是否以“/lockdown”开头,都应该运行。
@bot.event
async def on_message(message):
if message.content.startswith('/lockdown'):
...
await bot.process_commands(message)
Run Code Online (Sandbox Code Playgroud)
顺便说一下,@commands.has_role(...)不能应用于on_message. 尽管没有任何错误(因为有适当的检查),has_role但实际上不会像您预期的那样工作。
@has_role装饰器的替代方案是:
@bot.event
async def on_message(message):
if message.channel.is_private or discord.utils.get(message.author.roles, name="Admin") is None:
return False
if message.content.startswith('/lockdown'):
...
await bot.process_commands(message)
Run Code Online (Sandbox Code Playgroud)