mirror of
https://github.com/Noettore/lagomareGateKeeperBot.git
synced 2025-10-15 03:26:40 +02:00
Created modules and organized code
This commit is contained in:
85
handlers/gates.py
Normal file
85
handlers/gates.py
Normal file
@@ -0,0 +1,85 @@
|
||||
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
||||
from telegram.ext import ContextTypes
|
||||
from models import Gates, Users, Role
|
||||
|
||||
async def opengate(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users, gates: Gates):
|
||||
user_id = str(update.effective_user.id)
|
||||
args = context.args
|
||||
if not args:
|
||||
return await update.message.reply_text("Usage: `/opengate <gate>`")
|
||||
gate = args[0]
|
||||
gate_name = gates.get_name(gate)
|
||||
role = users.get_role(user_id)
|
||||
if role in (Role.ADMIN, Role.MEMBER):
|
||||
creds = users.get_credentials(user_id)
|
||||
if not creds:
|
||||
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)
|
||||
creds = users.get_credentials(grantor)
|
||||
if not grantor:
|
||||
return await update.message.reply_text("No valid grantor available.")
|
||||
if not creds:
|
||||
return await update.message.reply_text("No valid grantor credentials available.")
|
||||
users.update_grant_last_used(user_id, gate)
|
||||
try:
|
||||
await context.bot.send_message(chat_id=grantor, text=f"Guest {user_id} opened {gate_name}")
|
||||
except Exception as e:
|
||||
print(f"Failed to notify {grantor} that guest {user_id} opened {gate_name}: {e}")
|
||||
else:
|
||||
return await update.message.reply_text("Access denied.")
|
||||
|
||||
if gates.open_gate(gate, creds):
|
||||
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):
|
||||
# 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()
|
||||
]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
if update.callback_query:
|
||||
await update.callback_query.answer()
|
||||
await update.callback_query.edit_message_text(
|
||||
"Select a gate to open:", reply_markup=reply_markup
|
||||
)
|
||||
else:
|
||||
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):
|
||||
query = update.callback_query
|
||||
user_id = str(query.from_user.id)
|
||||
data = query.data
|
||||
if data.startswith("opengate_"):
|
||||
gate_id = data[len("opengate_") :]
|
||||
gate_name = gates.get_name(gate_id)
|
||||
role = users.get_role(user_id)
|
||||
if role in (Role.ADMIN, Role.MEMBER):
|
||||
creds = users.get_credentials(user_id)
|
||||
if not creds:
|
||||
await query.answer("Please set your credentials with /setcredentials first", show_alert=True)
|
||||
return
|
||||
elif role == Role.GUEST and users.can_open_gate(user_id, gate_id):
|
||||
grantor = users.get_grantor(user_id, gate_id)
|
||||
creds = users.get_credentials(grantor)
|
||||
if not grantor or not creds:
|
||||
await query.answer("No valid grantor credentials available.", show_alert=True)
|
||||
return
|
||||
users.update_grant_last_used(user_id, gate_id)
|
||||
try:
|
||||
await context.bot.send_message(chat_id=grantor, text=f"Guest {user_id} opened {gate_name}")
|
||||
except Exception as e:
|
||||
print(f"Failed to notify {grantor} that guest {user_id} opened {gate_name}: {e}")
|
||||
else:
|
||||
# TODO: guest not working
|
||||
await query.answer("Access denied.", show_alert=True)
|
||||
return
|
||||
|
||||
if gates.open_gate(gate_id, creds):
|
||||
await query.answer(f"Gate {gate_name} opened!", show_alert=True)
|
||||
await query.edit_message_text(f"Gate {gate_name} opened!")
|
||||
else:
|
||||
await query.answer(f"ERROR: Cannot open gate {gate_name}", show_alert=True)
|
||||
await query.edit_message_text(f"ERROR: Cannot open gate {gate_name}")
|
Reference in New Issue
Block a user