Queer European MD passionate about IT

bot_tools.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """Administration tools for CicloPiBot."""
  2. # Standard library modules
  3. import asyncio
  4. import datetime
  5. async def _restart_command(bot, update, user_record):
  6. with bot.db as db:
  7. db['restart_messages'].insert(
  8. dict(
  9. text="<i>Restart was successful.</i>",
  10. chat_id=update['chat']['id'],
  11. parse_mode='HTML',
  12. reply_to_message_id=update['message_id'],
  13. sent=None
  14. )
  15. )
  16. await bot.reply(
  17. update=update,
  18. text="I bot verranno riavviati in pochi secondi, caricando prima le "
  19. "eventuali modifiche al codice."
  20. )
  21. bot.__class__.stop(message='=== RESTART ===', final_state=65)
  22. return
  23. def init(bot):
  24. """Assign commands to `bot`."""
  25. @bot.command(command='/restart', aliases=[], show_in_keyboard=False,
  26. description="Riavvia i bot",
  27. authorization_level='admin')
  28. async def restart_command(bot, update, user_record):
  29. return await _restart_command(bot, update, user_record)
  30. @bot.additional_task('BEFORE')
  31. async def load_handovers():
  32. """Perform handovers before running."""
  33. with bot.db as db:
  34. for restart_message in db['restart_messages'].find(sent=None):
  35. asyncio.ensure_future(
  36. bot.send_message(
  37. **{
  38. key: val
  39. for key, val in restart_message.items()
  40. if key in (
  41. 'chat_id',
  42. 'text',
  43. 'parse_mode',
  44. 'reply_to_message_id'
  45. )
  46. }
  47. )
  48. )
  49. db['restart_messages'].update(
  50. dict(
  51. sent=datetime.datetime.now(),
  52. id=restart_message['id']
  53. ),
  54. ['id'],
  55. ensure=True
  56. )
  57. return