Boord的权限系统 [英] Permission System for Discord.py Bot
问题描述
我正在使用discord.py和asyncio制作discord机器人。该机器人具有 kick
和 ban
之类的命令,这些命令显然不适合普通用户使用。
I am in the process of making a discord bot using discord.py and asyncio. The bot has commands like kick
and ban
which obviously should not be available to normal users.
我想创建一个简单的系统,该系统使用 ctx.message.author
来检测用户角色具有的权限
I want to make a simple system which will detect what permissions the user's role has using ctx.message.author
to get the user who sent the command.
我不希望机器人程序检测到特定的角色名称,因为这些名称在服务器之间会有所不同。我也不想让机器人拥有多个文件来保持它的简单性。
I do not want the bot to detect a specific role name as these vary across servers. I also prefer not to have multiple files for the bot to keep it simple.
我已经看到了discord.py文档和其他各种资源,但都没有包含如何进行操作的示例。实现它们讨论的各种方法。
I have seen the discord.py documentation and various other sources but none contain examples of how to implement the various methods they talk about.
例如,这是我的机器人发出的单个命令:
As an example, here is a single command from my bot:
async def kick(ctx, userName: discord.User):
if True: #ctx.message.author.Permissions.administrator
await BSL.kick(userName)
else:
permission_error = str('Sorry ' + ctx.message.author + ' you do not have permissions to do that!')
await BSL.send_message(ctx.message.channel, permission_error)
的地方
声明是我自己做的尝试。 #ctx.message.author.Permissions.administrator
被注释掉,因为它不起作用,并替换为 True
测试目的。
Where the if else
statement is my attempt of doing this on my own. The #ctx.message.author.Permissions.administrator
is commented out as it does not work and replaced with True
for testing purposes.
谢谢您的任何帮助和建议。
Thank you for any help and suggestions in advance.
推荐答案
权限
是类的名称。要获得消息作者的权限,您应该访问 server_permissions
作者的财产。
Permissions
is the name of the class. To get the message authors permissions, you should access the server_permissions
property of the author.
if ctx.message.author.server_permissions.administrator:
# you could also use server_permissions.kick_members
更新:
验证用户调用命令权限的更好方法是使用命令扩展名的commands / commands.html#checks rel = noreferrer>检查功能,特别是 has_permissions
检查。例如,如果您只想向拥有 manage_roles
权限或 ban_members
权限的人打开命令,您可以这样编写命令:
A better way to validate the permissions of the person invoking the commands is by using the check feature of the commands
extension, specifically the has_permissions
check. For example, if you wanted to open your command only to people who had either the manage_roles
permission or the ban_members
permission, you could write your command like this:
from discord import Member
from discord.ext.commands import has_permissions, MissingPermissions
@bot.command(name="kick", pass_context=True)
@has_permissions(manage_roles=True, ban_members=True)
async def _kick(ctx, member: Member):
await bot.kick(member)
@_kick.error
async def kick_error(error, ctx):
if isinstance(error, MissingPermissions):
text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author)
await bot.send_message(ctx.message.channel, text)
这篇关于Boord的权限系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!