Python 关于@message()和@bot.command问题
当我的代码中有Python 关于@message()和@bot.command问题,python,discord.py,Python,Discord.py,当我的代码中有on_message()时,每隔@bot.command命令就会停止工作。我试图等待bot.process\u命令(消息),但这也不起作用。以下是我的代码: @bot.event @commands.has_role("Owner") async def on_message(message): if message.content.startswith('/lockdown'): await bot.process_commands(message)
on_message()
时,每隔@bot.command
命令就会停止工作。我试图等待bot.process\u命令(消息)
,但这也不起作用。以下是我的代码:
@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)
您必须将
wait bot.process_命令(消息)
置于if
语句范围之外,无论消息是否以“/lockdown”开头,都应运行process_命令
@bot.event
async def on_message(message):
if message.content.startswith('/lockdown'):
...
await bot.process_commands(message)
顺便说一下,@commands.has\u role(…)
不能应用于消息上的。虽然没有任何错误(因为有检查),但是的角色实际上并不像您预期的那样工作
@has\u角色的替代方案是:
@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)
您必须将等待bot.process_命令(消息)
置于语句范围之外,如果语句范围内,则无论消息是否以“/lockdown”开头,都应运行process_命令
@bot.event
async def on_message(message):
if message.content.startswith('/lockdown'):
...
await bot.process_commands(message)
顺便说一下,@commands.has\u role(…)
不能应用于消息上的。虽然没有任何错误(因为有检查),但是的角色实际上并不像您预期的那样工作
@has\u角色的替代方案是:
@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)
可以理解的是,@commands.has\u role
不会与消息上的一起工作,因为消息上的事件不是命令。这是一个每次将消息发送到服务器时都会调用的事件。还有一件事,即@commands.has\u role(“…”)不适用于on\u message(),您知道如何仅使用It role吗?@sirchanion您可以从message
对象访问message.author.roles
。然后,您可以对message.author.roles中的role执行类似于的操作:如果role.name==“Owner”
@sirchannion,我可能会迭代这些角色,将结果保存到一个变量(类似于Owner\u role=True
)中,然后如果您已经存在if
条件,则将其作为一部分。如果您需要进一步了解代码示例,请提出一个新问题或编辑现有问题。@has\u角色装饰器的替代方案将在处理任何命令之前退出该功能,除非满足第一个条件如果条件,那么@commands.has\u role
将无法处理消息上的,因为on_消息
事件不是命令。这是一个每次将消息发送到服务器时都会调用的事件。还有一件事,即@commands.has\u role(“…”)不适用于on\u message(),您知道如何仅使用It role吗?@sirchanion您可以从message
对象访问message.author.roles
。然后,您可以对message.author.roles中的role执行类似于的操作:如果role.name==“Owner”
@sirchannion,我可能会迭代这些角色,将结果保存到一个变量(类似于Owner\u role=True
)中,然后如果您已经存在if
条件,则将其作为一部分。如果您需要进一步了解代码示例,请提出新问题或编辑现有问题。@has\u角色装饰器的替代方案将在处理任何命令之前退出该函数,除非满足第一个If
条件,如果您使用命令
扩展,为什么要在on_message
中处理命令?如果使用commands
扩展名,为什么要在on_message
中处理命令?