remove.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """Add player conversation"""
  2. import random
  3. import string
  4. from telegram import ParseMode
  5. from telegram.ext import MessageHandler, CommandHandler, Filters, ConversationHandler
  6. from app import api, functions, database, HEADERS, BASE_URL, LOGGER
  7. PLAYER_NUMBER, CHOOSE, CONFIRM, VERIFICATION = range(4)
  8. def conv_start(update, context):
  9. """Start conversion"""
  10. LOGGER.info('"@%s" start remove account conversation', update.message.from_user.username)
  11. update.message.reply_text('Starting removal conversation, use /cancel to stop.')
  12. return conv_ask_player(update, context)
  13. def conv_ask_player(update, context):
  14. """Ask player id"""
  15. message_list = ['Send the number of account you would like to remove:']
  16. telegram_account = database.get_telegram_account(update.message.from_user.id)
  17. if not telegram_account:
  18. update.message.reply_text('No accounts found')
  19. return ConversationHandler.END
  20. players = database.get_rr_players(telegram_account)
  21. if not players:
  22. update.message.reply_text('No accounts found')
  23. return ConversationHandler.END
  24. context.user_data['player_list'] = players
  25. if not players:
  26. message_list.append('• none')
  27. index = 1
  28. for player in players:
  29. desktop_link = '[desktop](https://rivalregions.com/#slide/profile/{})'.format(player.id)
  30. mobile_link = '[mobile](https://m.rivalregions.com/#slide/profile/{})'.format(player.id)
  31. message_list.append(
  32. '{}: {} {} - {}'.format(
  33. index,
  34. functions.escape_text(player.name),
  35. desktop_link,
  36. mobile_link,
  37. )
  38. )
  39. index += 1
  40. message = '\n'.join(message_list)
  41. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  42. return PLAYER_NUMBER
  43. def conv_choose_player(update, context):
  44. """Ask max resource"""
  45. index = int(update.message.text)
  46. try:
  47. player = context.user_data['player_list'][index - 1]
  48. except IndexError:
  49. update.message.reply_text(
  50. 'Number {} not found, give me another number.'.format(index)
  51. )
  52. return PLAYER_NUMBER
  53. update.message.reply_text(
  54. 'Send `confirm` to remove {} from verified accounts.'.format(
  55. functions.escape_text(player.name)
  56. ),
  57. parse_mode=ParseMode.MARKDOWN
  58. )
  59. context.user_data['player'] = player
  60. return CONFIRM
  61. def get_player(player_id):
  62. """Get player by ID"""
  63. try:
  64. return api.get_rr_player(player_id)
  65. except api.PlayerNotFoundException:
  66. return False
  67. def conv_player_confirm(update, context):
  68. """Sending announcement"""
  69. player = context.user_data['player']
  70. LOGGER.info('"@%s" remove verified account %s', update.message.from_user.username, player.name)
  71. update.message.reply_text(
  72. 'Removing verified account.',
  73. parse_mode=ParseMode.MARKDOWN
  74. )
  75. database.remove_verified_player(update.message.from_user.id, player.id)
  76. context.user_data.clear()
  77. return ConversationHandler.END
  78. def conv_cancel(update, context):
  79. """Cancel announcement"""
  80. update.message.reply_text('Canceled action.')
  81. context.user_data.clear()
  82. return ConversationHandler.END
  83. # announcement conversation
  84. REMOVE_CONV = ConversationHandler(
  85. entry_points=[CommandHandler('remove', conv_start)],
  86. states={
  87. PLAYER_NUMBER: [
  88. MessageHandler(Filters.regex(r'^\d*$'), conv_choose_player),
  89. MessageHandler(Filters.text, conv_ask_player),
  90. ],
  91. CONFIRM: [
  92. MessageHandler(Filters.regex('(?i)confirm'), conv_player_confirm),
  93. MessageHandler(Filters.text, conv_cancel),
  94. ],
  95. },
  96. fallbacks=[CommandHandler('cancel', conv_cancel)]
  97. )