python - discord.py 如何使命令具有权限
问题描述
我正在制作一个不和谐的机器人,并制作了一段代码,可以让语音频道中的每个人都静音。我想确保只有 Mod 或具有管理员权限的人才能使用此命令。
这是我的静音命令代码:
@client.command()
async def vcmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=True)
await ctx.send('Mics are closed!')
这是我的取消静音命令(这将使用仅管理员或 Mod 使用它的相同概念):
@client.command()
async def vcunmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=False)
await ctx.send('Mics are opened!')
解决方案
使用has_permissions
装饰器
@client.command()
@commands.has_permissions(administrator=True) # Or whatever perms you want
async def vcmute(ctx):
...
# If you wanna send some message if the user doesn't have the needed permissions
@vcmute.error
async def vcmute_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send(f"You need {error.missing_perms} permissions to use this command.")
推荐阅读
- sql - 无法更正 Oracle 错误 ORA-00905 SQL 中缺少关键字
- python-3.x - “没有名为 'twilio.rest' 的模块;twilio 不是一个包”
- node.js - node js await 似乎没有等待
- android - Android服务引起的替换通知
- ios - 屏幕截图大小错误
- javascript - getFiles 和 getFolders 总是只返回一个文件/文件夹
- apache-nifi - NiFi - 连接到另一个实例(S2S)
- mysql - 仅在插入新行时设置 current_timestamp
- docker - 在 docker 环境中通过 OWASP zap 扫描 Rest API
- asp.net - 如何将文件夹发布到 Azure 应用服务?