forked from Cryz/battle_royale_sim
move upstart code
This commit is contained in:
22
bot.py
22
bot.py
@@ -1,5 +1,3 @@
|
|||||||
import os as _os
|
|
||||||
|
|
||||||
from utils import logs as _log
|
from utils import logs as _log
|
||||||
|
|
||||||
from telegram.ext import Application
|
from telegram.ext import Application
|
||||||
@@ -13,6 +11,7 @@ from entities import arena as _arena
|
|||||||
from bot_libs import player_handling as _bot_player
|
from bot_libs import player_handling as _bot_player
|
||||||
from bot_libs import simulation as _bot_sim
|
from bot_libs import simulation as _bot_sim
|
||||||
from bot_libs import repeating as _bot_repeat
|
from bot_libs import repeating as _bot_repeat
|
||||||
|
from bot_libs import upstart as _bot_upstart
|
||||||
from bot_libs import syms as _botsyms
|
from bot_libs import syms as _botsyms
|
||||||
|
|
||||||
|
|
||||||
@@ -34,28 +33,10 @@ async def bot_start(update, context):
|
|||||||
await update.message.reply_text('Ho creato il mondo di gioco', reply_markup=reply_markup)
|
await update.message.reply_text('Ho creato il mondo di gioco', reply_markup=reply_markup)
|
||||||
context.application.bot_data['arena'] = Arena
|
context.application.bot_data['arena'] = Arena
|
||||||
|
|
||||||
async def update_bot(update, context):
|
|
||||||
await 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 _log.log_warning(f'update_bot: user {chat_id} not allowed')
|
|
||||||
_log.log_info(f'update_bot: {chat_id} bot is updating...')
|
|
||||||
err= _os.system(f'cd {_botsyms.BOT_PATH}; /usr/bin/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('/usr/bin/systemctl --user restart 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_info(f'update_bot: {chat_id} bot successfully updated')
|
|
||||||
await update.message.reply_text('Bot aggiornato e riavviato!')
|
|
||||||
|
|
||||||
async def bot_commands(update, context):
|
async def bot_commands(update, context):
|
||||||
text= update.message.text
|
text= update.message.text
|
||||||
chat_id = update.effective_chat.id
|
chat_id = update.effective_chat.id
|
||||||
|
|
||||||
if text == 'upstart': return await update_bot(update, context)
|
|
||||||
|
|
||||||
if text == 'Init/Restart':
|
if text == 'Init/Restart':
|
||||||
_log.log_info(f'bot_command: {chat_id} Init/Restart')
|
_log.log_info(f'bot_command: {chat_id} Init/Restart')
|
||||||
return await bot_start(update, context)
|
return await bot_start(update, context)
|
||||||
@@ -114,6 +95,7 @@ async def bot_commands(update, context):
|
|||||||
seconds= max(1, text)
|
seconds= max(1, text)
|
||||||
return await _bot_repeat.start_loop_game(update, context, seconds)
|
return await _bot_repeat.start_loop_game(update, context, seconds)
|
||||||
|
|
||||||
|
if text == 'upstart': return await _bot_upstart.update_bot(update, context)
|
||||||
_log.log_debug(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)
|
await update.message.reply_text(_botsyms.WIP_MSG)
|
||||||
|
|
||||||
|
|||||||
34
bot_libs/upstart.py
Normal file
34
bot_libs/upstart.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import os as _os
|
||||||
|
from utils import logs as _log
|
||||||
|
from bot_libs import syms as _botsyms
|
||||||
|
|
||||||
|
async def _pull_repo(update):
|
||||||
|
err= _os.system(f'cd {_botsyms.BOT_PATH}; /usr/bin/git pull')
|
||||||
|
if err:
|
||||||
|
_log.log_error(f'update_bot: {chat_id} error {err} while trying to update the app')
|
||||||
|
await update.message.reply_text('Errore durante l\'aggiornamento del Bot')
|
||||||
|
return err
|
||||||
|
|
||||||
|
async def _upstart_service(update):
|
||||||
|
err= _os.system('/usr/bin/systemctl --user restart battle_royale.service')
|
||||||
|
# this error is fake, probably due to systemd restart that make the bot istance broke
|
||||||
|
#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')
|
||||||
|
|
||||||
|
async def update_bot(update, context):
|
||||||
|
await 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 _log.log_warning(f'update_bot: user {chat_id} not allowed')
|
||||||
|
|
||||||
|
_log.log_info(f'update_bot: {chat_id} bot is updating...')
|
||||||
|
|
||||||
|
err= await _pull_repo(update)
|
||||||
|
if err: return
|
||||||
|
err= await _upstart_service(update)
|
||||||
|
if err: return
|
||||||
|
|
||||||
|
_log.log_info(f'update_bot: {chat_id} bot successfully updated')
|
||||||
|
await update.message.reply_text('Bot aggiornato e riavviato!')
|
||||||
Reference in New Issue
Block a user