diff --git a/bot.py b/bot.py index d21096e..f0e815c 100644 --- a/bot.py +++ b/bot.py @@ -1,5 +1,7 @@ import os as _os +from utils import logs as _log + from telegram.ext import Application from telegram.ext import CommandHandler from telegram.ext import MessageHandler @@ -26,7 +28,7 @@ async def bot_start(update, context): reply_markup= ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, resize_keyboard=True) chat_id = update.effective_chat.id - print(f'{chat_id}: I\'m building the world\'s game...') + _log.log_debug(f'{chat_id}: I\'m building the world\'s game...') Arena= _arena.BrSimArena() await update.message.reply_text('Ho creato il mondo di gioco', reply_markup=reply_markup) @@ -35,11 +37,17 @@ async def bot_start(update, context): async def update_bot(update, context): update.message.reply_text('Sto aggiornando il Bot...') chat_id = update.effective_chat.id - if update.message.chat.id not in _botsyms.SUPER_USERS: return print(f'update_bot: user {chat_id} not allowed') - print(f'update_bot: {chat_id} bot is updating...') - _os.system(f'cd {_botsyms.BOT_PATH}; git pull') - _os.system('systemctl --user start battle_royale.service') - print(f'update_bot: {chat_id} bot successfully updated') + if update.message.chat.id not in _botsyms.SUPER_USERS: return _log.log_warning(f'update_bot: user {chat_id} not allowed') + _log.log_debug(f'update_bot: {chat_id} bot is updating...') + err= _os.system(f'cd {_botsyms.BOT_PATH}; git pull') + if err: + _log.log_error(f'update_bot: {chat_id} error {err} while trying to update the app') + return await update.message.reply_text('Errore durante l\'aggiornamento del Bot') + err= _os.system('systemctl --user start battle_royale.service') + if err: + _log.log_error(f'update_bot: {chat_id} error {err} while trying to upstart the app') + return await update.message.reply_text('Errore durante il riavvio del Bot') + _log.log_debug(f'update_bot: {chat_id} bot successfully updated') await update.message.reply_text('Bot aggiornato e riavviato!') async def bot_commands(update, context): @@ -49,32 +57,32 @@ async def bot_commands(update, context): if text == 'upstart': return await update_bot(update, context) if text == 'Init/Restart': - print(f'bot_command: {chat_id} Init/Restart') + _log.log_info(f'bot_command: {chat_id} Init/Restart') return await bot_start(update, context) if text == 'Add Player': - print(f'bot_command: {chat_id} Add Player') + _log.log_info(f'bot_command: {chat_id} Add Player') context.application.bot_data['ask_name'] = 1 if 'ask_seconds' in context.application.bot_data: del(context.application.bot_data['ask_seconds']) return await update.message.reply_text('Inserisci il Nome del giocatore (o piu\' nomi separati da virgola)') if text == 'Get Players': - print(f'bot_command: {chat_id} Get Players') + _log.log_info(f'bot_command: {chat_id} Get Players') return await _bot_player.get_players(update, context) if text == 'Get Alive Players': - print(f'bot_command: {chat_id} Get Alive Players') + _log.log_info(f'bot_command: {chat_id} Get Alive Players') return await _bot_player.get_alive_players(update, context) if text == 'Get Death Players': - print(f'bot_command: {chat_id} Get Death Players') + _log.log_info(f'bot_command: {chat_id} Get Death Players') return await _bot_player.get_death_players(update, context) if text == 'Get Ranking Players': - print(f'bot_command: {chat_id} Get Ranking Players') + _log.log_info(f'bot_command: {chat_id} Get Ranking Players') return await _bot_player.get_ranking_players(update, context) if text == 'Simulate Day': - print(f'bot_command: {chat_id} Simulate Day') + _log.log_info(f'bot_command: {chat_id} Simulate Day') return await _bot_sim.simulate_day(context, chat_id) if text == 'Run Periodically': - print(f'bot_command: {chat_id} Run Periodically') + _log.log_info(f'bot_command: {chat_id} Run Periodically') context.application.bot_data['ask_seconds'] = 1 if 'ask_name' in context.application.bot_data: del(context.application.bot_data['ask_name']) @@ -82,7 +90,7 @@ async def bot_commands(update, context): waiting_for_name= context.application.bot_data.get('ask_name') if waiting_for_name or text in ['Add random Players', 'Add random color Players']: - print(f'bot_command: {chat_id} Collected Player Name {text}') + _log.log_debug(f'bot_command: {chat_id} Collected Player Name {text}') if 'ask_name' in context.application.bot_data: del(context.application.bot_data['ask_name']) players= text.split(',') @@ -100,13 +108,13 @@ async def bot_commands(update, context): waiting_for_seconds= context.application.bot_data.get('ask_seconds') if waiting_for_seconds: - print(f'bot_command: {chat_id} User Wants to auto-run the game every {text} seconds') + _log.log_debug(f'bot_command: {chat_id} User Wants to auto-run the game every {text} seconds') try: text= int(text) except: return seconds= max(1, text) return await _bot_repeat.start_loop_game(update, context, seconds) - print(f'bot_command: {chat_id} sent this text: {text}') + _log.log_debug(f'bot_command: {chat_id} sent this text: {text}') await update.message.reply_text(_botsyms.WIP_MSG) def main(): @@ -115,7 +123,7 @@ def main(): application.add_handler(CommandHandler('start', bot_start)) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, bot_commands)) - print('Bot is running...') + _log.log_debug('Bot is running...') application.run_polling() if __name__ == '__main__': diff --git a/utils/logs.py b/utils/logs.py new file mode 100644 index 0000000..4c9e507 --- /dev/null +++ b/utils/logs.py @@ -0,0 +1,19 @@ +import logging as _logging + +logger = _logging.getLogger(__name__) +_logging.basicConfig(filename= 'battle_royale.log', encoding= 'utf-8', level= _logging.DEBUG) + +def log_debug(txt): + logger.debug(txt) + +def log_info(txt): + logger.info(txt) + +def log_warning(txt): + logger.warning(txt) + +def log_error(txt): + logger.error(txt) + +def log_critical(txt): + logger.critical(txt)