Exemplo n.º 1
0
    def set(self, discord_id: int, key: str, value):
        self._existence_check(discord_id)

        k = key if ":" not in key else key.split(":")[0]
        if not self.is_valid_attribute(k, value):
            raise DatabaseException(f"Invalid user attribute: {key} = {value}.")
        if k == "home_id":
            beam = key.split(":")[1]
            if not db.exists(f"beam:{beam}:active"):
                raise DatabaseException(f"Beam not found: {beam}.")

        db.set(f"user:{discord_id}:{key}", value)
Exemplo n.º 2
0
    def set(self, name: str, key: str, value):
        self._existence_check(name)

        if not self.is_valid_attribute(key, value):
            raise DatabaseException(f"Invalid beam attribute: {key} = {value}.")

        db.set(f"beam:{name}:{key}", value)
Exemplo n.º 3
0
 def get_attribute(self, name: str, attribute: str) -> Optional[Union[str, int]]:
     if attribute not in self.attributes:
         raise DatabaseException(f"Invalid beam attribute: {attribute}.")
     result = db.get(f"beam:{name}:{attribute}")
     if attribute in ("active", "admin_id", "replace", "timeout") and result:
         result = int(result)
     return result
Exemplo n.º 4
0
    def set(self, discord_id: int, key: str, value):
        self._check_existance(discord_id)

        if not self.is_valid_attribute(key, value):
            raise DatabaseException(f"Invalid wormhole attribute: {key} = {value}.")

        db.set(f"wormhole:{discord_id}:{key}", value)
Exemplo n.º 5
0
    def delete(self, name: str):
        self._existence_check(name)

        wormholes = [db.get(x) for x in db.scan(match="wormhole:*:beam")[1]]
        if name in wormholes:
            raise DatabaseException(f"Found {len(wormholes)} linked wormholes, halting.")

        for attribute in self.attributes:
            db.delete(f"beam:{name}:{attribute}")
Exemplo n.º 6
0
    def get_attribute(self, discord_id: int, attribute: str) -> Optional[Union[str, int]]:
        attr = attribute if ":" not in attribute else attribute.split(":")[0]
        if attr not in self.attributes:
            raise DatabaseException(f"Invalid user attribute: {attribute}.")

        result = db.get(f"user:{discord_id}:{attribute}")
        if attr in ("home_id", "mod", "readonly", "restricted") and result:
            result = int(result)
        return result
Exemplo n.º 7
0
    def get_attribute(self, discord_id: int, attribute: str) -> Optional[Union[str, int]]:
        if attribute not in self.attributes:
            raise DatabaseException(f"Invalid wormhole attribute: {attribute}.")
        result = db.get(f"wormhole:{discord_id}:{attribute}")

        # get data types
        if attribute in ("active", "admin_id", "messages", "readonly") and result:
            result = int(result)

        return result
Exemplo n.º 8
0
 def _existence_check(self, discord_id: int):
     result = db.get(f"user:{discord_id}:readonly")
     if result is None:
         raise DatabaseException(f"User ID `{discord_id}` unknown.")
Exemplo n.º 9
0
 def _availability_check(self, discord_id: int):
     result = db.get(f"user:{discord_id}:readonly")
     if result is not None:
         raise DatabaseException(f"User ID `{discord_id}` is already known.")
Exemplo n.º 10
0
 def _check_existance(self, discord_id: int):
     result = db.get(f"wormhole:{discord_id}:active")
     if result is None:
         raise DatabaseException(f"Channel `{discord_id}` is not a wormhole.")
Exemplo n.º 11
0
 def _check_availability(self, beam: str, discord_id: int):
     if not db.exists(f"beam:{beam}:active"):
         raise DatabaseException(f"Beam {beam} does not exist.")
     if db.exists(f"wormhole:{discord_id}:active"):
         raise DatabaseException(f"Channel `{discord_id}` is already a wormhole.")
Exemplo n.º 12
0
 def _existence_check(self, name: str):
     result = db.get(f"beam:{name}:active")
     if result is None:
         raise DatabaseException(f"Beam name `{name}` not found.")
Exemplo n.º 13
0
 def _availability_check(self, name: str):
     result = db.get(f"beam:{name}:active")
     if result is not None:
         raise DatabaseException(f"Beam name `{name}` already exists.")
Exemplo n.º 14
0
 def _name_check(self, name: str):
     if ":" in name:
         raise DatabaseException(f"Beam name `{name}` contains semicolon.")