Skip to content

Commit

Permalink
add guild to decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
pvyParts committed Oct 18, 2024
1 parent 069d0ba commit e748791
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions aadiscordbot/cogs/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os

from discord import Guild
from discord.ext import commands

from django.core.exceptions import ObjectDoesNotExist
Expand All @@ -17,11 +18,11 @@
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"


def has_perm(id, perm: str):
def has_perm(id, perm: str, guild: Guild = None):
if id in get_admins():
return True
try:
user = auth.get_auth_user(id)
user = auth.get_auth_user(id, guild=guild)
has_perm = user.has_perm(perm)

if has_perm:
Expand All @@ -38,8 +39,9 @@ def sender_has_perm(perm: str):
Permission Decorator: Does the user have x Django Permission
"""
def predicate(ctx):
guild = getattr(ctx, "guild", None)
if hasattr(ctx, "user"):
return has_perm(ctx.user.id, perm)
return has_perm(ctx.user.id, perm, guild=guild)
else:
return has_perm(ctx.author.id, perm) # !Commands

Expand Down Expand Up @@ -91,11 +93,11 @@ def predicate(ctx):
return commands.check(predicate)


def has_any_perm(id, perms: list):
def has_any_perm(id, perms: list, guild: Guild = None):
if id in get_admins():
return True
for perm in perms:
user = auth.get_auth_user(id)
user = auth.get_auth_user(id, guild=guild)
try:
has_perm = user.has_perm(perm)
if has_perm:
Expand All @@ -111,8 +113,9 @@ def sender_has_any_perm(perms: list):
Permission Decorator: Does the user have x or y Django Permission
"""
def predicate(ctx):
guild = getattr(ctx, "guild", None)
if hasattr(ctx, "user"):
return has_any_perm(ctx.user.id, perms)
return has_any_perm(ctx.user.id, perms, guild=guild)
else:
return has_any_perm(ctx.author.id, perms) # !Commands

Expand Down

0 comments on commit e748791

Please sign in to comment.