mirror of
https://github.com/Noettore/lagomareGateKeeperBot.git
synced 2025-10-14 19:16:40 +02:00
48 lines
2.4 KiB
Python
48 lines
2.4 KiB
Python
from telegram import Update
|
|
from telegram.ext import ContextTypes
|
|
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
|
|
|
|
user_id = str(update.effective_user.id)
|
|
role = users.get_role(user_id)
|
|
if role != Role.GUEST:
|
|
if update.callback_query:
|
|
await update.callback_query.answer("Only guests can request access.")
|
|
elif update.message:
|
|
return await update.message.reply_text("Only guests can request access.")
|
|
requester = users.get_fullname(user_id) or users.get_username(user_id)
|
|
text = (f"Access request: {requester} ({user_id}) requests access.\nUse `/grantaccess {user_id} <gate_id|all> YYYY-MM-DDTHH:MM:SSZ` to grant access.")
|
|
if update.callback_query:
|
|
await update.callback_query.answer("Your request has been submitted.")
|
|
elif update.message:
|
|
return await update.message.reply_text("Your request has been submitted.")
|
|
admins = users.get_admins()
|
|
for admin_id in admins:
|
|
try:
|
|
await context.bot.send_message(chat_id=admin_id, text=text, parse_mode="Markdown")
|
|
except Exception as e:
|
|
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.")
|
|
try:
|
|
user_id = context.args[0]
|
|
gate = context.args[1]
|
|
expires_at = context.args[2]
|
|
users.grant_access(user_id, gate, datetime.fromisoformat(expires_at.replace("Z", "+00:00")), grantor_id=grantor_id)
|
|
await update.message.reply_text(f"Access to {gate} granted to user {user_id} until {expires_at}")
|
|
try:
|
|
await context.bot.send_message(chat_id=user_id, text=f"Access granted to gate {gate} up to {expires_at}")
|
|
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: `/grantaccess <user_id> <gate_id|all> <expires_at:YYYY-MM-DDTHH:MM:SSZ>`") |