【问题标题】:discord.py error in ban command role hierarchy禁止命令角色层次结构中的 discord.py 错误
【发布时间】:2021-08-22 16:19:58
【问题描述】:

我的代码:

    #Ban command
    @bot.command()
    async def ban(ctx, member : discord.Member, *, reason='nothing'):
        #to check if user has permissions
        if has_permissions(ban_members=True):
            #to not let admins ban each other
            check = False
            for i in member.roles:
                if i in ctx.author.roles[1:]:
                    check = True
    
            if(check):
                await ctx.send('❌ Cant ban admins/moderators.')
            else:
                
                await member.ban(reason=reason)
                await ctx.send(f'✓ {member} has been banned')
        else:
            await ctx.send(' you dont have that permission') 
    

问题是没有权限的用户如果使用该命令不会打印:you don't have that permission,我得到一个错误。

【问题讨论】:

  • 错误是什么?

标签: discord.py


【解决方案1】:

你可以试试ctx.author.guild_permissions。或者在@bot.command()之后使用discord.py的@bot.has_permissions(ban_members=True)方法

    @bot.command()
    async def ban(ctx, member : discord.Member, *, reason='nothing'):
        #to check if user has permissions
        if ctx.author.guild_permissions.ban_members:
            #to not let admins ban each other
            check = False
            for i in member.roles:
                if i in ctx.author.roles[1:]:
                    check = True
    
            if(check):
                await ctx.send('❌ Cant ban admins/moderators.')
            else:
                
                await member.ban(reason=reason)
                await ctx.send(f'✓ {member} has been banned')
        else:
            await ctx.send(' you dont have that permission') 

【讨论】:

    猜你喜欢
    • 2020-08-14
    • 2020-03-17
    • 2018-11-07
    • 2021-02-07
    • 2010-11-19
    • 2012-06-16
    • 2016-10-29
    • 2020-10-09
    • 2017-12-27
    相关资源
    最近更新 更多