gpt4 book ai didi

python - on_message() 和 @bot.command 问题

转载 作者:行者123 更新时间:2023-11-30 22:16:52 24 4
gpt4 key购买 nike

当我的代码中有 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)

最佳答案

您必须将 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)

顺便说一句,@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)

    

关于python - on_message() 和 @bot.command 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49837578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com