-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.py
95 lines (81 loc) · 3.08 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Auth - One-liner authorization checks for commands
Throws AuthError to quickly exit the command
From there, Error can tell the users why the command failed
Recommended cogs: Error
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import discord
if TYPE_CHECKING:
from main import MerelyBot
from babel import Resolvable
from configparser import SectionProxy
class Auth():
""" Backup permissions/auth verification when guild permissions aren't in effect """
SCOPE = 'auth'
@property
def config(self) -> SectionProxy:
""" Shorthand for self.bot.config[scope] """
return self.bot.config[self.SCOPE]
def babel(self, target:Resolvable, key:str, **values: dict[str, str | bool]) -> str:
""" Shorthand for self.bot.babel(scope, key, **values) """
return self.bot.babel(target, self.SCOPE, key, **values)
def __init__(self, bot:MerelyBot):
self.bot = bot
# ensure config file has required data
if not bot.config.has_section(self.SCOPE):
bot.config.add_section(self.SCOPE)
if 'botadmin_guilds' not in self.config:
self.config['botadmin_guilds'] = ''
if 'superusers' not in self.config:
self.config['superusers'] = ''
if 'authusers' not in self.config:
self.config['authusers'] = ''
class AuthError(Exception):
"""Errors to be sent to a user that failed an auth test"""
def superusers(self, inter:discord.Interaction, fail=True) -> bool:
""" Verify this user is a superuser """
if str(inter.user.id) in self.config['superusers']:
return True
if fail:
raise self.AuthError(self.babel(inter, 'not_superuser'))
return False
def owners(self, inter:discord.Interaction, fail=True) -> bool:
""" Verify this user owns this guild """
if self.superusers(inter, fail=False):
return True
if isinstance(inter, discord.Interaction) and inter.user == inter.guild.owner:
return True
if fail:
raise self.AuthError(self.babel(inter, 'unauthorized'))
return False
def admins(self, inter:discord.Interaction, fail=True) -> bool:
""" Verify this user is an admin """
if self.owners(inter, fail=False):
return True
if inter.permissions.administrator:
return True
if fail:
raise self.AuthError(self.babel(inter, 'not_admin'))
return False
def authusers(self, inter:discord.Interaction, fail=True) -> bool:
""" Verify this user is an authuser """
if self.superusers(inter, fail=False):
return True
if str(inter.user.id) in self.config['authusers']:
return True
if fail:
raise self.AuthError(self.babel(inter, 'not_authuser'))
return False
def mods(self, inter:discord.Interaction, fail=True) -> bool:
""" Verify this user is a moderator """
if self.authusers(inter, fail=False):
return True
if self.admins(inter, fail=False):
return True
if inter.permissions.ban_members:
return True
if fail:
raise self.AuthError(self.babel(inter, 'not_mod'))
return False