This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramPoster/plugins/commands/general.py

46 lines
1.7 KiB
Python
Raw Normal View History

2023-03-20 14:55:42 +02:00
from os import getpid, makedirs, path
from time import time
2023-02-14 12:38:54 +02:00
2023-01-10 13:52:44 +02:00
from pyrogram import filters
2023-03-20 14:55:42 +02:00
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
2023-02-14 12:38:54 +02:00
2023-03-20 14:55:42 +02:00
from classes.poster_client import PosterClient
from modules.app import app, users_with_context
2023-01-17 12:43:10 +02:00
from modules.logger import logWrite
2023-03-20 14:55:42 +02:00
from modules.scheduler import scheduler
from modules.utils import configGet, jsonSave, locale
2023-01-10 13:52:44 +02:00
2023-02-14 12:38:54 +02:00
2023-03-20 14:55:42 +02:00
@app.on_message(~filters.scheduled & filters.command(["shutdown"], prefixes=["", "/"]))
2023-02-17 17:46:33 +02:00
async def cmd_kill(app: PosterClient, msg: Message):
2023-02-17 22:55:38 +02:00
if msg.from_user.id in app.admins:
2023-03-20 14:55:42 +02:00
global users_with_context
if len(users_with_context) > 0:
await msg.reply_text(
f"There're {len(users_with_context)} unfinished users' contexts. If you turn off the bot, those will be lost. Please confirm shutdown using a button below.",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
"Confirm shutdown", callback_data="shutdown"
)
]
]
),
)
return
2023-01-10 13:52:44 +02:00
pid = getpid()
2023-03-20 14:55:42 +02:00
logWrite(f"Shutting down bot with pid {pid}")
2023-03-09 12:33:02 +02:00
await msg.reply_text(
2023-03-20 14:55:42 +02:00
locale("shutdown", "message", locale=msg.from_user.language_code).format(
pid
),
)
scheduler.shutdown()
makedirs(configGet("cache", "locations"), exist_ok=True)
jsonSave(
{"timestamp": time()},
path.join(configGet("cache", "locations"), "shutdown_time"),
2023-03-09 12:33:02 +02:00
)
2023-03-20 14:55:42 +02:00
exit()