Skip to content

Commit

Permalink
Fixed admin perm check on has_guild_permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnaf-zamil authored and tandemdude committed Apr 30, 2021
1 parent f6e3ae4 commit 16a6166
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lightbulb/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def _get_missing_perms(permissions: hikari.Permissions, roles: typing.Sequence[h
for perm in permissions:
if perm is hikari.Permissions.ADMINISTRATOR:
return hikari.Permissions.NONE
if not any(role.permissions & perm for role in roles):

# Now it check if the role has admin. If it has admin, it doesn't count it as missing perm
if not any(role.permissions & perm or role.permissions & hikari.Permissions.ADMINISTRATOR for role in roles):
missing_perms |= perm
return missing_perms

Expand All @@ -150,6 +152,11 @@ async def _has_guild_permissions(ctx: context.Context, *, permissions: hikari.Pe
await _guild_only(ctx)

roles = ctx.bot.cache.get_roles_view_for_guild(ctx.guild_id).values()

# Checks if the author is the server owner
if ctx.guild.owner_id == ctx.author.id:
return True

missing_perms = _get_missing_perms(permissions, [role for role in roles if role.id in ctx.member.role_ids])

if missing_perms:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import mock
import pytest
from hikari import messages
from hikari import Permissions
from hikari import Intents

from lightbulb import checks
from lightbulb import context
Expand Down Expand Up @@ -83,6 +85,14 @@ async def test_owner_only_fails(ctx):
assert exc_info.type is errors.NotOwner


@pytest.mark.asyncio
async def test_guild_owner_passes(ctx):
ctx.author.id = 12345
ctx.guild.owner_id = 12345
ctx.bot.intents = Intents.GUILDS
assert await checks._has_guild_permissions(ctx, permissions=[Permissions.ADMINISTRATOR])


def test_add_check_called_with_guild_only():
deco = checks.guild_only()
fake_command = deco(mock.Mock(spec_set=commands.Command))
Expand Down

0 comments on commit 16a6166

Please sign in to comment.