def silph_rank(update, context): if len(context.args) != 1: logger.info("Invalid rank request by %s (%s)", update._effective_user.username, context.args) context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text="Ich brauche einen Spieler. Beispiel /rank ValorAsh") return if context.args[0] == 'enable' or context.args[0] == 'disable': logger.info("/rank %s by %s", context.args[0] , update._effective_user.username) database.toggle_groups(update, context, 'Rank') return #If we are in a group and dont want ranks to be searchable, return if update.message.chat_id < 0 and not database.group_enabled(update.message.chat_id, 'Rank'): logger.info("Disabled /rank request attempted by (%s)", update._effective_user.username) context.bot.delete_message(chat_id=update.message.chat_id,message_id=update.message.message_id) return name = context.args[0] logger.info('Silph rank request by %s for user %s', update._effective_user.username, name) user_id = database.get_silph_id(name) if user_id is None: url = 'https://sil.ph/'+name html = requests.get(url).content html = str(html) user = re.search('user_id: (\d+)', html) if user is None: logger.info('User %s does not have a silph card', name) context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text="Ich konnte den Nutzer " + name + " nicht finden") return else: logger.info('Adding userid to the database for user %s', name) user_id = user[0].split()[1] database.add_silph_id(name, user_id) url = 'https://sil.ph/card/cardData.json?user_id=' + str(user_id) f = urllib.request.urlopen(url) data = str(f.read()) data = data[data.index('{'):] p = re.compile(r'\\') data = p.sub('', data) p = re.compile(r'<.*?>') data= p.sub('', data) p = re.compile(r'[\']') data= p.sub('', data) user_data = json.loads(data) name = user_data['data']['in_game_username'] rank = user_data['data']['arenaGlobalRank'] region = user_data['data']['home_region'] response = "<b>Name: </b>" + name + "\n" response += "<b>Rank: </b>" + rank + "\n" response += "<b>Region: </b>" + region + "\n" logger.info('Return %s', response) context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response)
def language(update, context): logger.info('Language query by %s with query %s', update._effective_user.username, context.args) #Make sure that we only handle messages that we can speak language = database.get_language(update.message.chat_id) if update.message.chat_id < 0: admins = (admin.user.id for admin in context.bot.get_chat_administrators(update.message.chat.id)) if update._effective_user.id not in admins: response = responses[language]['only_for_admins'] bot_message = context.bot.send_message(parse_mode='Markdown', chat_id=update.message.chat_id, text=response) return if len(context.args) == 1 and context.args[0].lower() in supported_languages: database.toggle_groups(update, context, 'Language') #If we reject the input we try to delete the users message and let him know which languages we speak else: #Get the language that we are speaking in this group and tell the user which languages we can speak response = responses[language]['language_not_supported'] response = response.format(supported_languages) bot_message = context.bot.send_message(parse_mode='Markdown', chat_id=update.message.chat_id, text=response)
def language(update, context): #Make sure that we only handle messages that we can speak if len(context.args) == 1 and context.args[0].lower( ) in supported_languages: database.toggle_groups(update, context, 'Language') #If we reject the input we try to delete the users message and let him know which languages we speak else: try: context.bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id) except: logger.info("Cannot delete message Chat:%s MessageID:%s", update.message.chat_id, update.message.message_id) #Get the language that we are speaking in this group and tell the user which languages we can speak language = database.get_language(update.message.chat_id) response = responses[language]['language_not_supported'] response = response.format(supported_languages) bot_message = context.bot.send_message(parse_mode='Markdown', chat_id=update.message.chat_id, text=response)
def build_move_response(update, context, type): try: language = database.get_language(update.message.chat_id) responses = jsonresponse[language] logger.info('Move query %s with query %s', update._effective_user.username, context.args) #The user wants to edit the response customisation if (len(context.args) == 0): if update.message.chat_id < 0: admins = (admin.user.id for admin in context.bot.get_chat_administrators( update.message.chat.id)) if update._effective_user.id in admins: response = responses['moves_menu'] context.bot.send_message( parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=response_menu.custom_keyboard( update.message.chat_id, response_menu.types["moves"])) else: response = responses['only_for_admins'] bot_message = context.bot.send_message( parse_mode='Markdown', chat_id=update.message.chat_id, text=response) return else: logger.info("Invalid pokemon") response = responses['moves_menu'] context.bot.send_message( parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=response_menu.custom_keyboard( update.message.chat_id, response_menu.types["moves"])) return #Check, if move queries should be en-/disabled in this group if len(context.args) == 1 and (context.args[0] == 'enable' or context.args[0] == 'disable'): logger.info("/Move %s by %s", context.args[0], update._effective_user.username) #En-/disable IV queries for this group database.toggle_groups(update, context, 'Moves') return False #If we are in a group and dont want ivs queries are disabled we just delete the request and return if update.message.chat_id < 0 and not database.group_enabled( update.message.chat_id, 'Moves'): logger.info("Disabled /moves request attempted by (%s)", update._effective_user.username) return False #If the user did not specify a Pokemon if (len(context.args) != 1): response = responses['iv_no_argument'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response) return False #Look for the moves of that pokemon else: #Load the IV_Config for the current chat id (i.e. which attributes should be returned) move_config = database.get_iv_config(update.message.chat_id, "Moves") en_name, initial_language, different_language = iv_check.get_english_name( context.args[0], language) response = get_moves(en_name.lower(), language, responses, type, move_config) #Send the response to the user char_limit = 4096 for i in range(0, int(len(response) / 4096) + 1): if len(response) > 0: splitted_response = response[i * char_limit:(i + 1) * char_limit] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=splitted_response) return True #We got some weird input which we cannot perform except: logger.info("Could not perform /move request") response = responses['iv_error'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response) return False
def iv_rank(update, context): #Retrieve the current language language = database.get_language(update.message.chat_id) responses = jsonresponse[language] logger.info('IV request by %s with query %s', update._effective_user.username, context.args) #Check, if IV queries should be en-/disabled in this group if len(context.args) == 1 and (context.args[0] == 'enable' or context.args[0] == 'disable'): logger.info("/IV %s by %s", context.args[0], update._effective_user.username) #En-/disable IV queries for this group database.toggle_groups(update, context, 'IV') return #If we are in a group and dont want ivs queries are disabled we just delete the request and return if update.message.chat_id < 0 and not database.group_enabled( update.message.chat_id, 'IV'): logger.info("Disabled /iv request attempted by (%s)", update._effective_user.username) context.bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id) return #The user didn't specify a pokemon if (len(context.args) == 0): logger.info("Invalid pokemon") response = responses['iv_no_argument'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response) else: try: if context.args[0][0] is '+': evolutions, initial_language, different_language = get_pokemon_family( context.args[0][1:], language) else: evolutions, initial_language, different_language = get_english_name( context.args[0], language) evolutions = [evolutions] for evo in evolutions: #If the user just specified a Pokemon - Return the optimal distribution if (len(context.args) == 1): response = iv_given(evo.lower(), initial_language, responses) #If the user gave IVs with the pokemon - Return where this one ranks elif (len(context.args) == 4): att = normalize_iv(context.args[1]) de = normalize_iv(context.args[2]) sta = normalize_iv(context.args[3]) response = iv_given(evo.lower(), initial_language, responses, att, de, sta) logger.info('Return %s', response.encode("utf-8")) if different_language: language_hint = responses['language_hint'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=responses['language_hint']) #Send the response to the user context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response) #We got some weird input which we cannot perform except: logger.info("Could not perform /iv request") response = responses['iv_error'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response)
def get_iv_rank(update, context, xl): #Retrieve the current language language = database.get_language(update.message.chat_id) responses = jsonresponse[language] logger.info('IV request by %s with query %s', update._effective_user.username, context.args) #Check, if IV queries should be en-/disabled in this group if len(context.args) == 1 and (context.args[0] == 'enable' or context.args[0] == 'disable'): logger.info("/IV %s by %s", context.args[0] , update._effective_user.username) #En-/disable IV queries for this group database.toggle_groups(update, context, 'IV') return #If we are in a group and dont want ivs queries are disabled we just delete the request and return if update.message.chat_id < 0 and not database.group_enabled(update.message.chat_id, 'IV'): logger.info("Disabled /iv request attempted by (%s)", update._effective_user.username) context.bot.delete_message(chat_id=update.message.chat_id,message_id=update.message.message_id) return league = '1500' if len(context.args) > 0 and context.args[0] == '2500': context.args.pop(0) league = '2500' if len(context.args) > 0 and context.args[0] == '500': context.args.pop(0) league = '500' #The user didn't specify a pokemon if(len(context.args) == 0): if update.message.chat_id < 0: admins = (admin.user.id for admin in context.bot.get_chat_administrators(update.message.chat.id)) if update._effective_user.id in admins: response = responses['iv_menu'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=response_menu.custom_keyboard(update.message.chat_id, response_menu.types["iv"])) #context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=iv_keyboard(update.message.chat_id)) else: response = responses['only_for_admins'] bot_message = context.bot.send_message(parse_mode='Markdown', chat_id=update.message.chat_id, text=response) return else: logger.info("Invalid pokemon") response = responses['iv_menu'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=response_menu.custom_keyboard(update.message.chat_id, response_menu.types["iv"])) #context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=iv_keyboard(update.message.chat_id)) return else: try: #Load the IV_Config for the current chat id (i.e. which attributes should be returned) iv_config = database.get_iv_config(update.message.chat_id, "IV") if context.args[0][0] == '+': evolutions, initial_language, different_language = get_pokemon_family(context.args[0][1:], language) else: evolutions, initial_language, different_language = get_english_name(context.args[0], language) evolutions = [evolutions] for evo in evolutions: #If the user just specified a Pokemon - Return the optimal distribution if(len(context.args) == 1): response, ivs = iv_given(evo.lower(), initial_language, responses, iv_config, None, None, None, league, xl) #When a user requests a specific rank if(len(context.args) == 2): rank = context.args[1] response, ivs = iv_given_rank(evo.lower(), initial_language, responses, iv_config, rank, league, xl) #If the user gave IVs with the pokemon - Return where this one ranks elif(len(context.args) == 4): att = normalize_iv(context.args[1]) de = normalize_iv(context.args[2]) sta = normalize_iv(context.args[3]) response, ivs = iv_given(evo.lower(), initial_language, responses, iv_config, att, de, sta, league, xl) logger.info('Return %s', response.encode("utf-8")) if different_language: context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=responses['language_hint']) #Send the response to the user data = {"IVs": ivs, 'league': [str(league)], 'xl': [str(int(xl))]} forms = find_forms(evo) #forms = ["", "+alolan", "+purified"] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response, reply_markup=form_keyboard(evo.lower(), forms, data)) #We got some weird input which we cannot perform except: logger.info("Could not perform /iv request") response = responses['iv_error'] context.bot.send_message(parse_mode='HTML', chat_id=update.message.chat_id, text=response)