Show only granted gates in menu

This commit is contained in:
2025-05-24 00:08:08 +02:00
parent 5b8cbf8b9f
commit 9af398faef
5 changed files with 35 additions and 8 deletions

View File

@@ -14,7 +14,7 @@ async def requestaccess(update: Update, context: ContextTypes.DEFAULT_TYPE, user
if not context.args:
return await update.message.reply_text("Usage: `/requestaccess`", parse_mode="Markdown")
requester = users.get_fullname(user_id) or users.get_username(user_id)
text = (f"Access request: {requester} ({user_id}) requests access.\nUse `/approve {user_id} <gate|all> YYYY-MM-DDTHH:MM:SSZ` to grant access.")
text = (f"Access request: {requester} ({user_id}) requests access.\nUse `/grantaccess {user_id} <gate_id|all> YYYY-MM-DDTHH:MM:SSZ` to grant access.")
await update.message.reply_text("Your request has been submitted.")
admins = users.get_admins()
for admin_id in admins:
@@ -42,4 +42,4 @@ async def grantaccess(update: Update, context: ContextTypes.DEFAULT_TYPE, users:
except Exception as e:
print(f"Failed to notify {user_id} that admin {grantor_id} granted access for {gate} up to {expires_at}: {e}")
except Exception:
await update.message.reply_text("Usage: `/approve <user_id> <gate|all> <expires_at:YYYY-MM-DDTHH:MM:SSZ>`")
await update.message.reply_text("Usage: `/grantaccess <user_id> <gate_id|all> <expires_at:YYYY-MM-DDTHH:MM:SSZ>`")

View File

@@ -38,12 +38,26 @@ async def opengate(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Us
return await update.message.reply_text(f"Gate {gate_name} opened!")
await update.message.reply_text(f"ERROR: Cannot open gate {gate_name}")
async def open_gate_menu(update: Update, context: ContextTypes.DEFAULT_TYPE, gates: Gates):
async def open_gate_menu(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users, gates: Gates):
assert update.effective_user is not None
user_id = str(update.effective_user.id)
granted_gates = users.get_granted_gates(user_id)
if not granted_gates:
if update.callback_query:
await update.callback_query.answer("You have no gates to open.")
elif update.message:
return await update.message.reply_text("You have no gates to open.")
if 'all' in granted_gates:
granted_gates = gates.get_all_gates_id()
# Show a list of available gates as buttons
keyboard = [
[InlineKeyboardButton(gate.name, callback_data=f"opengate_{gate_id}")]
for gate_id, gate in gates._gates.items()
]
keyboard = []
for gate_id in granted_gates:
gate_name = gates.get_name(gate_id)
assert gate_name is not None
keyboard.append([InlineKeyboardButton(gate_name, callback_data=f"opengate_{gate_id}")])
reply_markup = InlineKeyboardMarkup(keyboard)
if update.callback_query:
await update.callback_query.answer()

View File

@@ -45,6 +45,6 @@ async def handle_main_menu_callback(update: Update, context: ContextTypes.DEFAUL
assert query is not None
data = query.data
if data == "open_gate_menu":
await open_gate_menu(update, context, gates=gates)
await open_gate_menu(update, context, users=users, gates=gates)
elif data == "request_access":
await requestaccess(update, context, users=users)