[英]Check if user has permission in discord.py
I am making a kick command for my discord.py bot.我正在为我的 discord.py 机器人发出踢腿命令。 Here is some code that the kick command might be:
下面是一些踢命令可能是的代码:
async kick_command(self, ctx, userName: discord.User):
prefix_used = ctx.prefix
alias_used = ctx.invoked_with
text = msg[len(prefix_used) + len(alias_used):]
if discord.User permissions = "admin":
try:
kick(User)
except Exception as e:
ee = "Error: " + e
await ctx.send_message(content=ee)
I am pretty sure the if statement and kick(User)
are invalid syntax.我很确定 if 语句和
kick(User)
是无效的语法。 Can you help me?你能帮助我吗?
My code: click here我的代码:点击这里
Try this:尝试这个:
@bot.command()
@has_permissions(kick_members=True)
async def kick(self, ctx, Member: discord.Member):
await bot.kick(Member)
@kick.error
async def kick_error(error, ctx):
if isinstance(error, MissingPermissions):
await ctx.send("You don't have permission to do that!")
don't forget to import the has_permissions
: from discord.ext.commands import has_permissions
不要忘记导入
has_permissions
: from discord.ext.commands import has_permissions
There is actually a better way of doing this without importing.实际上有一种更好的方法可以在不导入的情况下做到这一点。 I would recommend not importing and just doing this the way I am showing below because it will save you some time if you plan to import all of those.
我建议不要导入,而是按照我在下面显示的方式执行此操作,因为如果您计划导入所有这些,它将为您节省一些时间。
This doesn't require importing anything for has_permissions.这不需要为 has_permissions 导入任何内容。
@commands.has_permissions(kick_members=True)
async def kick(self, ctx, Member: discord.Member):
await bot.kick(Member)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.