Discord.py Bot的权限系统
我正在使用discord.py和asyncio制作一个discord bot . 机器人有像 kick
和 ban
这样的命令,显然不应该对普通用户可用 .
我想创建一个简单的系统,它将使用 ctx.message.author
来检测用户角色拥有的权限,以获取发送命令的用户 .
我不希望机器人检测到特定的角色名称,因为这些角色因服务器而异 . 我也不希望机器人有多个文件来保持简单 .
我已经看过discord.py文档和其他各种来源但没有包含如何实现他们所讨论的各种方法的示例 .
举个例子,这是我的机器人的一个命令:
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)
if else
声明是我自己尝试这样做的地方 . #ctx.message.author.Permissions.administrator
已注释掉,因为它不起作用并替换为 True
用于测试目的 .
感谢您提前提供任何帮助和建议 .
回答(2)
发现接受的答案的提示可能不起作用:
-
discord.py库的重写版本和预重写版本可能存在兼容性问题,这些版本仍然是非过时的,不推荐使用且仍在使用中 .
-
机器人还应该检查它自己的权限,以排除错误的一个原因 .
-
如果有错误,或者机器人本身的权限无效,机器人应该说些什么,对吗?
-
需要实现某些东西以防止机器人尝试在DM或组上下文中使用此命令 . 它几乎总是错误的 .
我提出以下解决方案(假设您使用命令扩展名):
import discord
from discord.ext import commands
import time
@bot.command(pass_context=True,description="Kicks the given member. Please ensure both the bot and the command invoker have the permission 'Kick Members' before running this command.")
async def kick(ctx, target:discord.Member):
"""(GUILD ONLY) Boot someone outta the server. See 's!kick' for more."""
if not str(ctx.message.channel).startswith("Direct Message with "):
msg=await bot.say("Checking perms...")
time.sleep(0.5)
if ctx.message.server.me.server_permissions.kick_members:
if ctx.message.author.server_permissions.kick_members:
await bot.edit_message(msg,new_content="All permissions valid, checking issues with target...")
time.sleep(0.5)
if target==ctx.message.server.owner:
await bot.edit_message(msg, new_content="All permissions are correct, but you're attempting to kick the server owner, whom you can't kick no matter how hard you try. Whoops!")
else:
if target==ctx.message.server.me:
await bot.edit_message(msg, new_content="Whoops! All permissions are corrent, but you just tried to make me kick myself, which is not possible. Perhaps you meant someone else, not poor me?")
else:
await bot.edit_message(msg, new_content="All permissions correct, and no issues with target being self or server owner, attempting to kick.")
time.sleep(2)
try:
await bot.kick(target)
await bot.edit_message(msg, ":boom: BAM! ***kicc'd***")
except Exception:
await bot.edit_message(msg, new_content="I was unable to kick the passed member. The member may have a higher role than me, I may have crashed into a rate-limit, or an unknown error may have occured. In that case, try again.")
else:
await bot.edit_message(msg, new_content="I've the correct permissions, {}, but you do not. Perhaps ask for them?").format(ctx.message.author.mention)
else:
await bot.edit_message(msg, new_content="I'm just a poor bot with no permissions. Could you kindly grant me the permission `Kick Members`? Thanks! :slight_smile:")
else:
await bot.say("'Tis a DM! This command is for servers only... try this again in a server maybe? :slight_smile:")
2 years ago
Permissions是该类的名称 . 要获取消息作者权限,您应该访问作者的server_permissions属性 .
更新:
验证调用命令的人员权限的更好方法是使用
commands
扩展名的check功能,特别是has_permissions检查 . 例如,如果您只想向拥有manage_roles
权限或ban_members
权限的人员打开命令,您可以像这样编写命令: