首页 > 解决方案 > Discord.py 命令冷却时间

问题描述

我正在制作一个 discord.py 机器人,该机器人将具有“复活”命令。该命令将发送 2 个 ping 以恢复聊天。我已将其设置为只有具有 MENTION EVERYONE 权限的人才能使用该命令,但当成员使用该命令时,冷却时间就会开始。那么有没有办法让成员不影响冷却时间?

@client.command()
@commands.cooldown(1.0, 3600.0, commands.BucketType.guild) 
async def revive(ctx):
    if ctx.author.guild_permissions.mention_everyone:
        await ctx.send(
            "I shall revive the CHAT, <@800171737832226827> <@&800168050077728808>"
        )
    else:
        embed = discord.Embed(title=None,
                              decription=None,
                              color=discord.Colour.blue())
        embed.add_field(
            name="Error!",
            value=
            "You do not have the permissions to do this! You need the permission: **Mention Everyone**"
        )
        await ctx.send(embed=embed)```


 

标签: pythondiscord.py

解决方案


它会触发冷却,因为当普通成员使用该功能时,它实际上会进入该功能并开始执行其中编码的操作。

为了避免触发冷却,您需要做的是添加检查以确保调用命令的用户具有正确的权限,但将其作为装饰器传递,在函数实际运行之前对其进行评估,因为它装饰它。

在这种情况下,您将需要添加@commands.has_permissions(mention_everyone=True)到您的代码中。

它应该如下所示:

@client.command()
@commands.has_permissions(mention_everyone=True)
@commands.cooldown(1.0, 3600.0, commands.BucketType.guild) 
async def revive(ctx):
    await ctx.send(
        "I shall revive the CHAT, <@800171737832226827> <@&800168050077728808>"
    )

推荐阅读