2
0
mirror of https://github.com/Nick80835/microbot synced 2025-09-03 07:55:52 +00:00

implement update command and improved stop_client

This commit is contained in:
Nick80835
2023-05-07 17:08:49 -04:00
parent 7a2cb4f204
commit c1b0b10017
4 changed files with 32 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ asyncpraw
cchardet cchardet
cryptg cryptg
cython cython
gitpython
hachoir hachoir
peewee peewee
pillow pillow

View File

@@ -29,11 +29,11 @@ class MicroBot():
self.start_client() self.start_client()
self.loader = Loader(self.client, self.logger, self.settings) self.loader = Loader(self.client, self.logger, self.settings)
def run_until_done(self): async def run_until_done(self):
self.loader.load_all_modules() self.loader.load_all_modules()
self.logger.info("Client successfully started.") self.logger.info("Client successfully started.")
self.client.run_until_disconnected() await self.client.run_until_disconnected()
self.client.loop.run_until_complete(self.loader.aioclient.close()) await self.stop_client(reason="Client disconnected.")
def _check_config(self): def _check_config(self):
api_key = self.settings.get_config("api_key") api_key = self.settings.get_config("api_key")
@@ -64,16 +64,21 @@ class MicroBot():
self.client = telethon.TelegramClient(self.settings.get_config("session_name", "bot0"), api_key, api_hash, connection=CTA).start(bot_token=bot_token) self.client = telethon.TelegramClient(self.settings.get_config("session_name", "bot0"), api_key, api_hash, connection=CTA).start(bot_token=bot_token)
except (TokenInvalidError, AccessTokenExpiredError, AccessTokenInvalidError): except (TokenInvalidError, AccessTokenExpiredError, AccessTokenInvalidError):
self.logger.error("The bot token provided is invalid, exiting.") self.logger.error("The bot token provided is invalid, exiting.")
sys.exit(2) sys.exit(1)
async def stop_client(self, reason=None): async def stop_client(self, reason=None, exit_code=None):
if reason: if reason:
self.logger.info("Stopping client for reason: %s", reason) self.logger.info("Stopping bot for reason: %s", reason)
else: else:
self.logger.info("Stopping client.") self.logger.info("Stopping bot.")
await self.loader.aioclient.close() await self.loader.aioclient.close()
await self.client.disconnect()
if exit_code is not None:
self.logger.info("Exiting with exit code: %i", exit_code)
sys.exit(exit_code)
else:
sys.exit(0)
telethon.events.NewMessage.Event = ExtendedEvent telethon.events.NewMessage.Event = ExtendedEvent
@@ -83,7 +88,4 @@ ldr = micro_bot.loader
client = micro_bot.client client = micro_bot.client
logger = micro_bot.logger logger = micro_bot.logger
try: client.loop.run_until_complete(micro_bot.run_until_done())
micro_bot.run_until_done()
except:
micro_bot.client.loop.run_until_complete(micro_bot.stop_client())

View File

@@ -5,7 +5,8 @@ from re import DOTALL, IGNORECASE, escape, search
from traceback import format_exc, print_exc from traceback import format_exc, print_exc
from telethon import events from telethon import events
from telethon.errors.rpcerrorlist import ChatWriteForbiddenError, ChatAdminRequiredError from telethon.errors.rpcerrorlist import (ChatAdminRequiredError,
ChatWriteForbiddenError)
from .database import ChatWrapper from .database import ChatWrapper
from .fixes import inline_photos from .fixes import inline_photos

View File

@@ -4,6 +4,7 @@ import io
import os import os
from platform import python_version from platform import python_version
import git
import psutil import psutil
from telethon import version from telethon import version
from telethon.tl.types import Channel, Chat from telethon.tl.types import Channel, Chat
@@ -89,6 +90,14 @@ async def reload_modules(event):
pass pass
@ldr.add("update", owner=True, hide_help=True)
async def update_bot(event):
update_msg = await event.reply("Pulling changes…")
repo = git.Repo(os.getcwd())
repo.remotes.origin.pull()
await update_msg.edit("Changes pulled successfully!")
@ldr.add("sysd", sudo=True, hide_help=True) @ldr.add("sysd", sudo=True, hide_help=True)
async def sysd(event): async def sysd(event):
try: try:
@@ -118,10 +127,14 @@ async def alive(event):
await event.reply(alive_format.format(version.__version__, python_version(), mem_usage)) await event.reply(alive_format.format(version.__version__, python_version(), mem_usage))
@ldr.add("shutdown", owner=True, hide_help=True) @ldr.add("shutdown", pattern_extra="(f|)", owner=True, hide_help=True)
async def shutdown(event): async def shutdown(event):
await event.reply("Goodbye…") await event.reply("Goodbye…")
await micro_bot.stop_client()
if event.other_args[0]:
await micro_bot.stop_client(reason="Shutdown command issued.", exit_code=1)
else:
await micro_bot.stop_client(reason="Shutdown command issued.")
@ldr.add("blacklist", sudo=True, hide_help=True) @ldr.add("blacklist", sudo=True, hide_help=True)