def error_handler(update: Update, context: CallbackContext): chat = update.effective_chat if chat.type == "private": if "lang" not in context.user_data: context.user_data["lang"] = database.get_language_player(update.effective_user.id) lang = context.user_data["lang"] else: if "lang" not in context.chat_data: context.chat_data["lang"] = database.get_language_chat(update.effective_chat.id) lang = context.chat_data["lang"] if update.callback_query: update.callback_query.answer(get_string(lang, "error"), show_alert=True) else: update.effective_message.reply_text(get_string(lang, "error")) payload = "" # normally, we always have an user. If not, its either a channel or a poll update. if update.effective_user: payload += f' with the user {mention_html(update.effective_user.id, update.effective_user.first_name)}' # there are more situations when you don't get a chat if update.effective_chat: payload += f' within the chat <i>{update.effective_chat.title}</i>' if update.effective_chat.username: payload += f' (@{update.effective_chat.username})' # but only one where you have an empty payload by now: A poll (buuuh) if update.poll: payload += f' with the poll id {update.poll.id}.' text = f"Oh no. The error <code>{context.error}</code> happened{payload}. The type of the chat is " \ f"<code>{chat.type}</code>. The current user data is <code>{context.user_data}</code>, the chat data " \ f"<code>{context.chat_data}</code>." context.bot.send_message(208589966, text, parse_mode=ParseMode.HTML) raise
def nextgame_start(update: Update, context: CallbackContext): user_id = update.effective_user.id database.insert_player_pm(user_id, True) context.args = context.args[0].split("_") if context.args else None if not context.args or context.args[0] != "nextgame": return try: chat_id = int(context.args[1]) new_id = database.get_new_id(chat_id) if new_id: chat_id = new_id lang = database.get_language_chat(chat_id) except ValueError: context.bot.send_message(user_id, get_string("en", "group_not_found")) return chat_details = database.get_group_title(chat_id) chat_link = helpers.chat_link(chat_details["title"], chat_details["link"]) new = database.insert_group_nextgame(chat_id, user_id) if new: context.bot.send_message(user_id, get_string( lang, "nextgame_added").format(chat_link), parse_mode=ParseMode.HTML, disable_web_page_preview=True) else: context.bot.send_message( user_id, get_string(lang, "nextgame_removed").format(chat_link), parse_mode=ParseMode.HTML, disable_web_page_preview=True)
def abort_game(update: Update, context: CallbackContext): chat_data = context.chat_data if "players" not in chat_data: chat_id = update.effective_chat.id lang = database.get_language_chat(chat_id) update.effective_message.reply_text(get_string(lang, "no_game_running")) return # sometimes a keyerror happens here. That can have different reasons, but this being an important command, I decided # to throw in an try expect try: lang = chat_data["lang"] except KeyError: lang = "en" chat_data["lang"] = "en" # little admin check if not is_admin(context.bot, update.effective_user.id, update.effective_chat): update.effective_message.reply_text(get_string(lang, "no_admin_abort")) return chat_id = update.effective_chat.id potential_job = context.job_queue.get_jobs_by_name(chat_id) if potential_job: potential_job[0].schedule_removal() chat_data.clear() update.effective_message.reply_text(get_string(lang, "abort_game")) chat_data["lang"] = lang
def greeting(update: Update, context: CallbackContext): new_chat_members = update.effective_message.new_chat_members if new_chat_members: if context.bot.id not in [user.id for user in new_chat_members]: return lang = database.get_language_chat(update.effective_chat.id) context.chat_data["lang"] = lang update.effective_message.reply_text(get_string(lang, "greeting"))
def real_shutdown(args): dp = args[0] bot = dp.bot for chat_id in dp.chat_data: if dp.chat_data[chat_id] and "players" in dp.chat_data[chat_id]: lang = database.get_language_chat(chat_id) bot.send_message(chat_id, get_string(lang, "run_shutdown")) change_handlers(dp) bot.send_message(args[1], "Shutdown done, upload activated")
def no_game(update, context, text): query = update.callback_query chat_data = context.chat_data chat_id = update.effective_chat.id if "lang" in chat_data: lang = chat_data["lang"] else: lang = database.get_language_chat(chat_id) query.answer(get_string(lang, text), show_alert=True) query.edit_message_reply_markup(None)
def real_shutdown(args): updater = args[0] dp = updater.dispatcher bot = dp.bot for chat_id in dp.chat_data: if dp.chat_data[chat_id]: lang = database.get_language_chat(chat_id) bot.send_message(chat_id, get_string(lang, "run_shutdown")) bot.send_message(args[1], "Shutdown done") updater.stop()
def real_shutdown(args): dp = args[0] bot = dp.bot for chat_id in dp.chat_data: if dp.chat_data[chat_id] and "players" in dp.chat_data[chat_id]: lang = database.get_language_chat(chat_id) try: bot.send_message(chat_id, get_string(lang, "run_shutdown")) except Exception as e: bot.send_message(args[1], f"{chat_id} still skipped because {e.__dict__}") change_handlers(dp) bot.send_message(args[1], "Shutdown done, upload activated")
def change_language(update: Update, context: CallbackContext): query = update.callback_query chat_id = int(query.data.split("_")[1]) current_lang = database.get_language_chat(chat_id) languages = get_languages() current_language = languages[current_lang] lang = context.user_data["lang"] buttons = group_settings_helpers.language_buttons(languages, chat_id) query.edit_message_text(get_string(lang, "group_setting_languages").format( current_language, TRANSLATION_CHAT_LINK), reply_markup=InlineKeyboardMarkup(buttons), parse_mode=ParseMode.HTML)
def game_rules(update: Update, context: CallbackContext): if update.effective_chat.type == "private": if "lang" not in context.user_data: context.user_data["lang"] = database.get_language_player( update.effective_user.id) lang = context.user_data["lang"] else: if "lang" not in context.chat_data: context.chat_data["lang"] = database.get_language_chat( update.effective_chat.id) lang = context.chat_data["lang"] update.effective_message.reply_html(get_string(lang, "rules"))
def group_stats(update: Update, context: CallbackContext): chat_data = context.chat_data if "lang" not in chat_data: chat_data["lang"] = database.get_language_chat( update.effective_chat.id) stats = database.get_group_stats(update.effective_chat.id) try: position = group_games.index(update.effective_chat.id) + 1 except ValueError: group_games.append(update.effective_chat.id) position = group_games.index(update.effective_chat.id) + 1 group_tournaments.append(update.effective_chat.id) amount = len(group_games) second_position = group_tournaments.index(update.effective_chat.id) + 1 text = get_string(chat_data["lang"], "group_stats").format(stats["games"], stats["tournaments"], position, amount, second_position) update.effective_message.reply_text(text)
def abort_game(update: Update, context: CallbackContext): chat_data = context.chat_data if "players" not in chat_data: chat_id = update.effective_chat.id lang = database.get_language_chat(chat_id) update.effective_message.reply_text(get_string(lang, "no_game_running")) return lang = chat_data["lang"] # little admin check if not is_admin(context.bot, update.effective_user.id, update.effective_chat): update.effective_message.reply_text(get_string(lang, "no_admin_abort")) return chat_id = update.effective_chat.id potential_job = context.job_queue.get_jobs_by_name(chat_id) if potential_job: potential_job[0].schedule_removal() chat_data.clear() update.effective_message.reply_text(get_string(lang, "abort_game")) chat_data["lang"] = lang
def error_handler(update: Update, context: CallbackContext): if not update: text = "Hey jo, error outside of update, The full traceback:\n\n < code > {trace} < / code > " context.bot.send_message(208589966, text, parse_mode=ParseMode.HTML) return # trying to hunt down a bug here if update.effective_message: logger.info( f"This is the message received: {update.effective_message.text}") chat = update.effective_chat if chat.type == "private": if "lang" not in context.user_data: context.user_data["lang"] = database.get_language_player( update.effective_user.id) lang = context.user_data["lang"] else: if "lang" not in context.chat_data: context.chat_data["lang"] = database.get_language_chat( update.effective_chat.id) lang = context.chat_data["lang"] if update.callback_query: update.callback_query.answer(get_string(lang, "error"), show_alert=True) else: update.effective_message.reply_text(get_string(lang, "error")) payload = "" # normally, we always have an user. If not, its either a channel or a poll update. if update.effective_user: payload += f' with the user {mention_html(update.effective_user.id, update.effective_user.first_name)}' # there are more situations when you don't get a chat if update.effective_chat: payload += f' within the chat <i>{update.effective_chat.title}</i>' if update.effective_chat.username: payload += f' (@{update.effective_chat.username})' # but only one where you have an empty payload by now: A poll (buuuh) if update.poll: payload += f' with the poll id {update.poll.id}.' trace = "".join(traceback.format_tb(sys.exc_info()[2])) text = f"Oh no. The error <code>{context.error}</code> happened{payload}. The type of the chat is " \ f"<code>{chat.type}</code>. The current user data is <code>{context.user_data}</code>, the chat data " \ f"<code>{context.chat_data}</code>.\nThe full traceback:\n\n<code>{trace}</code>" context.bot.send_message(208589966, text, parse_mode=ParseMode.HTML) raise
def group_setting(update: Update, context: CallbackContext): chat_id = update.effective_chat.id lang = database.get_language_chat(chat_id) if not helpers.is_admin(context.bot, update.effective_user.id, update.effective_chat): update.effective_message.reply_text( get_string(lang, "no_admin_settings")) return user_id = update.effective_user.id user_lang = database.get_language_player(user_id) pm = database.get_pm_player(user_id) context.user_data["lang"] = user_lang database.insert_group_title(chat_id, update.effective_chat.title, update.effective_chat.link) if pm: try: # yes, its not a string, I don't change the functions name for this you f****r chat_link = helpers.chat_link(update.effective_chat.title, update.effective_chat.link) buttons = group_settings_helpers.group_settings_buttons( get_string(user_lang, "group_setting_buttons"), chat_id) context.bot.send_message( user_id, get_string(user_lang, "group_setting_text").format(chat_link), reply_markup=InlineKeyboardMarkup(buttons), parse_mode=ParseMode.HTML, disable_web_page_preview=True) # this means the bot was blocked wtf except Unauthorized: update.effective_message.reply_text( get_string(lang, "admin_blocked_bot")) database.insert_player_pm(user_id, False) else: button = [[ InlineKeyboardButton( get_string(lang, "no_pm_settings_button"), url=f"https://t.me/thechameleonbot?start=settings_{chat_id}") ]] update.effective_message.reply_text( get_string(lang, "no_pm_settings"), reply_markup=InlineKeyboardMarkup(button))
def nextgame_command(update: Update, context: CallbackContext): user_id = update.effective_user.id chat_id = update.effective_chat.id pm = database.get_pm_player(user_id) new = database.insert_group_nextgame(chat_id, user_id) if "lang" not in context.chat_data: context.chat_data["lang"] = database.get_language_chat(chat_id) lang = context.chat_data["lang"] chat_link = helpers.chat_link(update.effective_chat.title, update.effective_chat.link) database.insert_group_title(chat_id, update.effective_chat.title, update.effective_chat.link) if pm: try: if new: context.bot.send_message( user_id, get_string(lang, "nextgame_added").format(chat_link), parse_mode=ParseMode.HTML, disable_web_page_preview=True) else: context.bot.send_message( user_id, get_string(lang, "nextgame_removed").format(chat_link), parse_mode=ParseMode.HTML, disable_web_page_preview=True) except Unauthorized: update.effective_message.reply_text( get_string(lang, "nextgame_block")) database.insert_player_pm(user_id, False) else: button = [[ InlineKeyboardButton( get_string(lang, "no_pm_settings_button"), url=f"https://t.me/thechameleonbot?start=nextgame_{chat_id}") ]] update.effective_message.reply_text( get_string(lang, "nextgame_pm"), reply_markup=InlineKeyboardMarkup(button))
def help_message(update: Update, context: CallbackContext): if "lang" not in context.chat_data: context.chat_data["lang"] = database.get_language_chat( update.effective_chat.id) update.effective_message.reply_text( get_string(context.chat_data["lang"], "help_group"))
def admins_reload(update: Update, context: CallbackContext): chat_id = update.effective_chat.id helpers.get_admin_ids(context.bot, chat_id, True) lang = database.get_language_chat(chat_id) update.effective_message.reply_html(get_string(lang, "admins_reload"))
def start(update: Update, context: CallbackContext, dp: Dispatcher): chat_id = update.effective_chat.id chat_data = context.chat_data if "lang" not in chat_data: lang = database.get_language_chat(chat_id) else: lang = chat_data["lang"] if database.shutdown: update.effective_message.reply_text( get_string(lang, "group_start_shutdown")) return elif "players" in chat_data: update.effective_message.reply_text(get_string(lang, "game_running")) return first_name = update.effective_user.first_name user_id = update.effective_user.id button = [[ InlineKeyboardButton(get_string(lang, "start_button"), callback_data="join") ]] mention = mention_html(user_id, first_name) group_settings = database.get_all_settings(chat_id) settings = [ "deck", "fewer", "more", "pin", "tournament", "restrict", "exclamation" ] wanted_settings = [] for setting in settings: if setting == "deck": wanted_settings.append(f"{group_settings[setting]}") elif group_settings[setting]: wanted_settings.append(get_string(lang, "activated")) else: wanted_settings.append(get_string(lang, "deactivated")) text = get_string(lang, "start_game").format(mention, mention, *wanted_settings) message = update.effective_message.reply_html( text, reply_markup=InlineKeyboardMarkup(button)) payload = { "dp": dp, "players": [{ "user_id": user_id, "first_name": first_name }], "message": message.message_id, "lang": lang, "chat_id": chat_id, "known_players": [], "tutorial": False, "starter": { "user_id": user_id, "first_name": first_name }, "group_settings": group_settings } context.job_queue.run_repeating(timer, START_TIME, context=payload, name=chat_id) payload = { "starter": { "user_id": user_id, "first_name": first_name }, "players": [{ "user_id": user_id, "first_name": first_name }], "lang": lang, "message": message.message_id, "left_players": {}, "settings": wanted_settings } chat_data.update(payload) chat_link = helpers.chat_link(update.effective_chat.title, update.effective_chat.link) for player_id in database.get_nextgame_ids(chat_id): if player_id == user_id: continue try: context.bot.send_message(player_id, get_string(lang, "nextgame").format(chat_link), parse_mode=ParseMode.HTML, disable_web_page_preview=True) except Unauthorized as e: database.remove_group_nextgame(chat_id, [player_id]) database.insert_player_pm(user_id, False) e.message += "handled in group no PM" raise e except BadRequest as e: if e.message == "Chat not found": database.remove_group_nextgame(chat_id, [player_id]) database.insert_player_pm(user_id, False) e.message += "handled in group no PM" raise e