def start(bot, update, log_update=False): """ Start a new ranking for the chat from which the message originates. Args: bot (Telegram Bot): Bot that received the message. update (Telegram Update): Full description of the Telegram message. Returns: Boolean: Was the Ranking properly started """ if log_update: update_log(update) if PRINT_UPDATES: print_upd(update) if update.message.chat.type == "private": logging.log(logging.INFO, "{} tried to create ranking for private chat".format(update.effective_user.username)) msg = bot.send_message(chat_id=update.message.chat_id, text=("Unfortunately, private-chats cannot have rankings.\n\n" + "To get started with a ranking:\n" + "* Create a new channel\n" + "* Invite @table_soccer_ranker_bot to the channel\n" + "* Send '/start' message to the channel")) if log_update: bot_log(msg) return False channel_admins = update.message.chat.get_administrators() if not update.effective_user.id in [u.user.id for u in channel_admins]: logging.log(logging.INFO, "{} tried to create ranking in a chat, where he's not an admin. Chat: {}".format(update.effective_user.username, update.message.chat.title)) msg = bot.send_message(chat_id=update.message.chat_id, text="Uh oh, you need to be an channel administrator to start a ranking.") if log_update: bot_log(msg) return False # Check if ranking exists chat_id = update.message.chat_id if not chat_id in RANKINGS: try: ranking = Ranking.load_ranking(chat_id) except FileNotFoundError: try: logging.log(logging.INFO, "Creating a new Ranking for chat {}.".format(update.message.chat.name)) except AttributeError: # Chat had no 'name' logging.log(logging.INFO, "Creating a new Ranking for chat-id {}.".format(chat_id)) ranking = Ranking(chat_id) RANKINGS[chat_id] = ranking for admin in channel_admins: if not admin.user.is_bot: ranking.add_user(admin.user.id, admin.user.username) starter_user = update.effective_user if not starter_user.is_bot: ranking.add_user(starter_user.id, starter_user.username) # TODO Ping administrator to add unknown users msg = bot.send_message(chat_id=update.message.chat_id, text="Succesfully created a ranking for this chat. Happy gaming!") if log_update: bot_log(msg) return True
def __lazy_load_ranking(chat_id): chat_id = int(chat_id) R = Ranking.load_ranking(chat_id) RANKINGS[chat_id] = R return