Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于@message()和@bot.command问题_Python_Discord.py - Fatal编程技术网

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
中处理命令?