問題描述
當我的代碼中有 on_message()
時,它會停止所有其他 @bot.command
命令的工作.我嘗試過 await bot.process_commands(message)
,但這也不起作用.這是我的代碼:
When I have on_message()
in my code, it stops every other @bot.command
commands from working. I've tried to await bot.process_commands(message)
, but that doesn't work either. Here is my code that I have:
@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)
推薦答案
你必須將 await bot.process_commands(message)
放在 if
語句范圍之外,<無論消息是否以/lockdown"開頭,都應運行code>process_command.
You have to place await bot.process_commands(message)
outside of the if
statement scope, process_command
should be run regardless if the message startswith "/lockdown".
@bot.event
async def on_message(message):
if message.content.startswith('/lockdown'):
...
await bot.process_commands(message)
順便說一句,@commands.has_role(...)
不能應用于 on_message
.盡管沒有任何錯誤(因為檢查到位),但 has_role
實際上不會像您預期的那樣工作.
By the way, @commands.has_role(...)
cannot be applied to on_message
. Although there aren't any errors (because there’s checking in place), has_role
wouldn't actually work as you would've expected.
@has_role
裝飾器的替代方案是:
An alternative to the @has_role
decorator would be:
@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)
這篇關于on_message() 和@bot.command 問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!