def GetAttempts(chatid: int) -> int: sql = "SELECT attempts FROM banned WHERE chatid=?" cursor.execute(sql, (chatid, )) result = cursor.fetchone() if result is not None: return int(result[0]) else: return 5
def UserIsBanned(chatid: int) -> tuple: sql = "SELECT state, reason FROM banned WHERE chatid=?" cursor.execute(sql, (chatid, )) result = cursor.fetchone() if result is not None: return bool(result[0]), result[1] else: return False, "User not found"
def VerifyToken(token: str) -> tuple: sql = "SELECT name FROM tokens WHERE token=?" hsh = TokenToHash(token) cursor.execute(sql, (hsh, )) result = cursor.fetchone() if result is not None: return True, result[0] else: return False, "Unknown"
def TokenContainsPermission(token_name: str, permission: str) -> bool: sql = "SELECT permissions FROM tokens WHERE name=?" cursor.execute(sql, (token_name, )) result = cursor.fetchone() # Check if token exists if not result: return False # Check root perms if result[0] == "*": return True # Check other perms else: return permission in loads(result[0])
def TokenDelete(name: str) -> bool: # Check if token exists sql = "SELECT id FROM tokens WHERE name=?" cursor.execute(sql, (name, )) result = cursor.fetchone() # Delete token if result is not None: lock.acquire(True) sql = "DELETE FROM tokens WHERE id=?" cursor.execute(sql, (result[0], )) connection.commit() lock.release() return True else: return False
def SetAttempts(chatid: int, username: str = "Unknown", attempts: int = 5) -> str: lock.acquire(True) sql = "SELECT id FROM banned WHERE chatid=?" cursor.execute(sql, (chatid, )) # Change attempts count to user result = cursor.fetchone() if result is not None: sql = "UPDATE banned SET attempts=?, username=? WHERE chatid=?" else: sql = "INSERT INTO banned (attempts, username, chatid) VALUES (?, ?, ?)" # Execute sql & commit changes cursor.execute(sql, (attempts, username, chatid)) connection.commit() lock.release() return sql[:6]
def BanUser(chatid: int, username: str = "Unknown", state: bool = True, reason: str = "") -> None: lock.acquire(True) sql = "SELECT id FROM banned WHERE chatid=?" cursor.execute(sql, (chatid, )) # Change ban state to user result = cursor.fetchone() if result is not None: sql = "UPDATE banned SET state=?, username=?, reason=? WHERE chatid=?" else: sql = "INSERT INTO banned (state, username, reason, chatid) VALUES (?, ?, ?, ?)" # Set user is banned cursor.execute(sql, (int(state), username, reason, chatid)) # Remove row from authorized users if state is True: sql = "DELETE FROM authorized WHERE chatid=?" cursor.execute(sql, (chatid, )) connection.commit() lock.release()