on_message() and @bot.command issue(on_message() 和@bot.command 问题)
问题描述
当我的代码中有 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 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:on_message() 和@bot.command 问题
基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01