mab update, !short command, last_access fix

- Updated mab to v0.5.0
- Added `!short` command to add named tokens
- Fixed `last_access_at` field not being updated
This commit is contained in:
2026-09-13 00:12:00 +03:00
parent 4a947cec3d
commit 4194f3da48
6 changed files with 136 additions and 79 deletions

View File

@@ -252,11 +252,17 @@ class Database:
return None
async def token_create(self) -> ObjectToken:
async def token_create(self, code: str | None = None) -> ObjectToken:
"""Create a token."""
if self._connection is None:
raise RuntimeError("Not connected to the database")
code = get_hash(str(time.time()).encode() + os.urandom(64))
if code is None:
code = get_hash(str(time.time()).encode() + os.urandom(64))
else:
if len(code) < 2 or len(code) > 16:
raise RuntimeError("Token must be 2-16 symbols long")
if not all(ord(c) < 128 and (c.islower() or c.isdigit()) for c in code):
raise RuntimeError("Only digits and ASCII lowercase letters are allowed")
create_time = time.time()
statement = """
INSERT INTO tokens (code, name, created_at, last_access_at)
@@ -329,6 +335,17 @@ class Database:
except:
self._logger.error(traceback.format_exc())
async def token_set_last_access(self, code: str, timestamp: float) -> None:
"""Set last_access_at for the token."""
if self._connection is None:
raise RuntimeError("Not connected to the database")
try:
statement = "UPDATE tokens SET last_access_at=? WHERE code=?"
await self._connection.execute(statement, (timestamp, code))
await self._connection.commit()
except:
self._logger.error(traceback.format_exc())
async def ban_create(self, ip: str, expires_at: float, reason: str) -> None:
"""Save information about banned IP address. Replaces existing IPs."""