【问题标题】:Check Role Permission Discord.py检查角色权限 Discord.py
【发布时间】:2020-11-10 22:29:38
【问题描述】:

我有一个带有用于检查用户角色的 for 循环的函数:

for role in ctx.author.roles:

现在,我想检查“角色”是否有权限 Manage_Channels

感谢大家的帮助!!!

【问题讨论】:

  • 您是否需要阻止非管理员使用该命令?

标签: python discord discord.py


【解决方案1】:

如果你需要这个来检查作者是否有使用该命令的权限。有一个内置函数可以做到这一点Docs

from discord.ext.commands import has_permissions

@bot.command()
@commands.has_permissions(manage_channels=True)
async def test(ctx):
    await ctx.send('You can manage channels.')

对于错误信息

from discord.ext.commands import MissingPermissions

@test.error
async def test_error(error, ctx):
    if isinstance(error, MissingPermissions):
        await ctx.send(f'Sorry {ctx.author.mention}, you do not have permissions to do that!')

【讨论】:

  • 在哪里?没找到
  • 有一个标有Docs的链接 这是那里的例子,试试看它是否适合你。
  • 你写的是对的,但是如果作者没有权限,那么机器人不会发送任何消息,如果作者没有权限,我希望它发送消息
  • 它没有检测到我认为的错误,这是我所做的更改:
  • @clear.error async def clear_error(error, ctx): if isinstance(error, MissingPermissions): await ctx.send(f'Sorry {ctx.author.mention}, you do not have permissions to do that!')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
  • 2019-03-06
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
相关资源
最近更新 更多