From 42411e1c947c7672104ffb232cbd08a43860aa9b Mon Sep 17 00:00:00 2001 From: Levi <57452819+l3v11@users.noreply.github.com> Date: Mon, 6 Jun 2022 19:16:32 +0600 Subject: [PATCH] Add restart module --- Dockerfile | 2 +- bot/__main__.py | 32 ++++++++++++++++++---- bot/helper/telegram_helper/bot_commands.py | 1 + bot/modules/list.py | 4 +-- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4e424848..56768005 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,4 +15,4 @@ RUN pip3 install --no-cache-dir -r requirements.txt COPY . . -CMD ["bash","start.sh"] +CMD ["bash", "start.sh"] diff --git a/bot/__main__.py b/bot/__main__.py index 8d06800c..00a0eef3 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -1,14 +1,16 @@ +import os import signal import time from psutil import cpu_percent, cpu_count, disk_usage, virtual_memory, net_io_counters +from sys import executable from telegram import InlineKeyboardMarkup from telegram.ext import CommandHandler -from bot import LOGGER, botStartTime, AUTHORIZED_CHATS, DEST_DRIVES, TELEGRAPH, dispatcher, updater +from bot import bot, LOGGER, botStartTime, AUTHORIZED_CHATS, DEST_DRIVES, TELEGRAPH, Interval, dispatcher, updater from bot.modules import auth, cancel, clone, compress, count, delete, eval, list, permission, shell, status from bot.helper.ext_utils.bot_utils import get_readable_file_size, get_readable_time -from bot.helper.ext_utils.fs_utils import start_cleanup, exit_clean_up +from bot.helper.ext_utils.fs_utils import start_cleanup, clean_all, exit_clean_up from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.telegram_helper.button_builder import ButtonMaker from bot.helper.telegram_helper.filters import CustomFilters @@ -61,6 +63,16 @@ def stats(update, context): def log(update, context): sendLogFile(context.bot, update.message) +def restart(update, context): + restart_message = sendMessage("Restart in progress...", context.bot, update.message) + if Interval: + Interval[0].cancel() + clean_all() + with open(".restartmsg", "w") as f: + f.truncate(0) + f.write(f"{restart_message.chat.id}\n{restart_message.message_id}\n") + os.execl(executable, executable, "-m", "bot") + help_string = ''' SearchX - The Ultimate Telegram Bot for Google Drive @@ -72,13 +84,13 @@ def log(update, context):

/{BotCommands.StartCommand}: Start the bot

-• /{BotCommands.ListCommand} <query>: Find data on Google Drive +• /{BotCommands.ListCommand} <query>: Search data on Google Drive

/{BotCommands.CloneCommand} <url> <key>: Copy data from Google Drive, AppDrive and GDToT (Key optional)

-• /{BotCommands.ArchiveCommand}: Archive data from Google Drive, AppDrive and GDToT +• /{BotCommands.ArchiveCommand} <url>: Archive data from Google Drive, AppDrive and GDToT

-• /{BotCommands.ExtractCommand}: Extract data from Google Drive, AppDrive and GDToT +• /{BotCommands.ExtractCommand} <url>: Extract data from Google Drive, AppDrive and GDToT

/{BotCommands.CountCommand} <drive_url>: Count data from Google Drive

@@ -119,6 +131,8 @@ def log(update, context): • /{BotCommands.ExecHelpCommand}: Get help about executor

/{BotCommands.LogCommand}: Get the log file +

+• /{BotCommands.RestartCommand}: Restart the bot ''' help_admin = TELEGRAPH[0].create_page( @@ -135,6 +149,11 @@ def bot_help(update, context): def main(): start_cleanup() + if os.path.isfile(".restartmsg"): + with open(".restartmsg") as f: + chat_id, msg_id = map(int, f) + bot.editMessageText("Restarted successfully", chat_id, msg_id, parse_mode='HTMl') + os.remove(".restartmsg") start_handler = CommandHandler(BotCommands.StartCommand, start, run_async=True) keys_handler = CommandHandler(BotCommands.ListKeysCommand, listkeys, @@ -145,6 +164,8 @@ def main(): filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) log_handler = CommandHandler(BotCommands.LogCommand, log, filters=CustomFilters.owner_filter, run_async=True) + restart_handler = CommandHandler(BotCommands.RestartCommand, restart, + filters=CustomFilters.owner_filter, run_async=True) help_handler = CommandHandler(BotCommands.HelpCommand, bot_help, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(start_handler) @@ -152,6 +173,7 @@ def main(): dispatcher.add_handler(ping_handler) dispatcher.add_handler(stats_handler) dispatcher.add_handler(log_handler) + dispatcher.add_handler(restart_handler) dispatcher.add_handler(help_handler) updater.start_polling() LOGGER.info("Bot started") diff --git a/bot/helper/telegram_helper/bot_commands.py b/bot/helper/telegram_helper/bot_commands.py index 5bc37866..4630481a 100644 --- a/bot/helper/telegram_helper/bot_commands.py +++ b/bot/helper/telegram_helper/bot_commands.py @@ -22,6 +22,7 @@ def __init__(self): self.PingCommand = 'ping' self.StatsCommand = 'stats' self.LogCommand = 'log' + self.RestartCommand = 'restart' self.HelpCommand = 'help' BotCommands = _BotCommands() diff --git a/bot/modules/list.py b/bot/modules/list.py index 957e3d78..6fc0e8b8 100644 --- a/bot/modules/list.py +++ b/bot/modules/list.py @@ -15,13 +15,13 @@ def list_drive(update, context): if reply_to is not None: query = reply_to.text if query != '': - reply = sendMessage(f"Finding: {query}", context.bot, update.message) + reply = sendMessage(f"Search in progress...", context.bot, update.message) LOGGER.info(f"Finding: {query}") gd = GoogleDriveHelper() try: msg, button = gd.drive_list(query) except Exception as e: - msg, button = "There was an error", None + msg, button = "Internal error", None LOGGER.exception(e) editMessage(msg, reply, button) else: