mirror of
https://github.com/Noettore/lagomareGateKeeperBot.git
synced 2025-10-14 19:16:40 +02:00
Resolve type assertions
This commit is contained in:
@@ -4,6 +4,9 @@ from datetime import datetime
|
||||
from models import Users, Role
|
||||
|
||||
async def requestaccess(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users):
|
||||
assert update.effective_user is not None
|
||||
assert update.message is not None
|
||||
|
||||
user_id = str(update.effective_user.id)
|
||||
role = users.get_role(user_id)
|
||||
if role != Role.GUEST:
|
||||
@@ -21,6 +24,10 @@ async def requestaccess(update: Update, context: ContextTypes.DEFAULT_TYPE, user
|
||||
print(f"Failed to notify {admin_id} that guest {user_id} requested access: {e}")
|
||||
|
||||
async def grantaccess(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users):
|
||||
assert update.effective_user is not None
|
||||
assert update.message is not None
|
||||
assert context.args is not None
|
||||
|
||||
grantor_id = str(update.effective_user.id)
|
||||
if users.get_role(grantor_id) != Role.ADMIN:
|
||||
return await update.message.reply_text("Only admins can grant access.")
|
||||
|
@@ -3,6 +3,10 @@ from telegram.ext import ContextTypes
|
||||
from models import Users, Credential, Role
|
||||
|
||||
async def setcredentials(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users):
|
||||
assert update.effective_user is not None
|
||||
assert update.message is not None
|
||||
assert context.args is not None
|
||||
|
||||
user_id = str(update.effective_user.id)
|
||||
args = context.args
|
||||
if len(args) != 2:
|
||||
|
@@ -3,6 +3,9 @@ from telegram.ext import ContextTypes
|
||||
from models import Gates, Users, Role
|
||||
|
||||
async def opengate(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users, gates: Gates):
|
||||
assert update.effective_user is not None
|
||||
assert update.message is not None
|
||||
|
||||
user_id = str(update.effective_user.id)
|
||||
args = context.args
|
||||
if not args:
|
||||
@@ -16,6 +19,8 @@ async def opengate(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Us
|
||||
return await update.message.reply_text("Please set your credentials with `/setcredentials` first")
|
||||
elif role == Role.GUEST and users.can_open_gate(user_id, gate):
|
||||
grantor = users.get_grantor(user_id, gate)
|
||||
assert grantor is not None
|
||||
|
||||
creds = users.get_credentials(grantor)
|
||||
if not grantor:
|
||||
return await update.message.reply_text("No valid grantor available.")
|
||||
@@ -45,13 +50,16 @@ async def open_gate_menu(update: Update, context: ContextTypes.DEFAULT_TYPE, gat
|
||||
await update.callback_query.edit_message_text(
|
||||
"Select a gate to open:", reply_markup=reply_markup
|
||||
)
|
||||
else:
|
||||
elif update.message:
|
||||
await update.message.reply_text("Select a gate to open:", reply_markup=reply_markup)
|
||||
|
||||
async def handle_gate_open_callback(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users, gates: Gates):
|
||||
assert update.callback_query is not None
|
||||
|
||||
query = update.callback_query
|
||||
user_id = str(query.from_user.id)
|
||||
data = query.data
|
||||
assert data is not None
|
||||
if data.startswith("opengate_"):
|
||||
gate_id = data[len("opengate_") :]
|
||||
gate_name = gates.get_name(gate_id)
|
||||
@@ -63,6 +71,7 @@ async def handle_gate_open_callback(update: Update, context: ContextTypes.DEFAUL
|
||||
return
|
||||
elif role == Role.GUEST and users.can_open_gate(user_id, gate_id):
|
||||
grantor = users.get_grantor(user_id, gate_id)
|
||||
assert grantor is not None
|
||||
creds = users.get_credentials(grantor)
|
||||
if not grantor or not creds:
|
||||
await query.answer("No valid grantor credentials available.", show_alert=True)
|
||||
|
@@ -12,6 +12,9 @@ async def post_init(application: Application, bot_config: BotConfig) -> None:
|
||||
await application.bot.set_my_commands(bot_config.commands)
|
||||
|
||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users):
|
||||
assert update.effective_user is not None
|
||||
assert update.message is not None
|
||||
|
||||
user_id = str(update.effective_user.id)
|
||||
role = users.get_role(user_id)
|
||||
keyboard = []
|
||||
@@ -39,6 +42,7 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users
|
||||
|
||||
async def handle_main_menu_callback(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users, gates: Gates):
|
||||
query = update.callback_query
|
||||
assert query is not None
|
||||
data = query.data
|
||||
if data == "open_gate_menu":
|
||||
await open_gate_menu(update, context, gates=gates)
|
||||
|
@@ -3,6 +3,8 @@ from telegram.ext import ContextTypes
|
||||
from models import Users
|
||||
|
||||
async def updateuser(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users):
|
||||
assert update.effective_user is not None
|
||||
|
||||
user_id = str(update.effective_user.id)
|
||||
username = update.effective_user.username
|
||||
fullname = update.effective_user.full_name
|
||||
|
@@ -14,8 +14,8 @@ class Grant:
|
||||
self,
|
||||
grantor: str,
|
||||
expires_at: datetime,
|
||||
granted_at: datetime = None,
|
||||
last_used_at: datetime = None,
|
||||
granted_at: datetime | None = None,
|
||||
last_used_at: datetime | None = None,
|
||||
status: Status = Status.ENABLED
|
||||
):
|
||||
self.grantor = grantor
|
||||
@@ -50,8 +50,8 @@ class User:
|
||||
username: str,
|
||||
fullname: str,
|
||||
role: Role = Role.GUEST,
|
||||
credentials: Credential = None,
|
||||
grants: dict[str, Grant] = None,
|
||||
credentials: Credential | None = None,
|
||||
grants: dict[str, Grant] | None = None,
|
||||
status: Status = Status.ENABLED
|
||||
):
|
||||
self.id = id
|
||||
@@ -104,7 +104,7 @@ class Users:
|
||||
with open(self._json_path, "w") as f:
|
||||
json.dump({uid: user.to_dict() for uid, user in self._users.items()}, f, indent=2)
|
||||
|
||||
def update_user(self, id: str, username: str, fullname: str) -> bool:
|
||||
def update_user(self, id: str, username: str | None, fullname: str | None) -> bool:
|
||||
if not id or not username or not fullname:
|
||||
return False
|
||||
if id in self._users:
|
||||
|
Reference in New Issue
Block a user