python - 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)```
解决方案
它会触发冷却,因为当普通成员使用该功能时,它实际上会进入该功能并开始执行其中编码的操作。
为了避免触发冷却,您需要做的是添加检查以确保调用命令的用户具有正确的权限,但将其作为装饰器传递,在函数实际运行之前对其进行评估,因为它装饰它。
在这种情况下,您将需要添加@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>"
)
推荐阅读
- python - 我如何未能将项目添加到列表中?
- http - groovy 参数重复并用 '&' 分隔
- sql - PostgreSQL:唯一约束或唯一索引
- reactjs - 错误:ENOENT:没有这样的文件或目录,chmod 'C:\...\hello-world\node_modules\sshpk\bin\CredDB2.CEF'
- sql - Spring Data Jpa 调用存储过程
- c - 从 main 返回 errno
- ethereum - 映射类型的 Solidity 字段大小
- angular - AngularCLI 和 Angular 材质(原理图)错误:无法解析集合“@angular/material”
- c - 从 C 中的标准输入读取不可预测和不确定(即没有 EOF)大小的输入的最佳方法是什么?
- javascript - 为什么没有定义道具展示?