async def add_hs(self,ctx, *members): try: first_msg = 'Adding ' for member in members: first_msg += f'{member} ' first_msg += "to the clan's hs." await ctx.send(first_msg) not_found_osrs = [] in_cc = [] all_names = get_players_in_hs(self.cur) for name in members: name = name.lower() stats = getStats(playerURL(name,'iron')) if stats == 404: not_found_osrs.append(name) elif name in all_names: in_cc.append(name) else: try: sql_add_player_hs(self.cur,name,stats) except Exception as e: in_cc.append(name) #self.conn.commit() found = [x for x in members if (x not in in_cc and x not in not_found_osrs)] response = f"{found} were added!\n" if not_found_osrs: response+= f"{not_found_osrs} were not found in the osrs' hiscores.\n" if in_cc: response+= f"{in_cc} these players are already in the clan's hiscores.\n" except Exception as e: response = str(e) finally: await ctx.send(response)
async def updateteam(self, ctx, team_num): try: team_num = int(team_num) members = get_team(self.cur, team_num) first_msg = 'Updating ' for member in members: first_msg += f'{member} ' await ctx.send(first_msg) not_found_osrs = [] not_in_cc = [] for name in members: name = name.lower() stats = getStats(playerURL(name, 'iron')) if stats == 404: not_found_osrs.append(name) else: sql_update_player_hs(self.cur, name, stats, stats_col_names) # sql_add_player_hs_historic(self.self.cur,name,stats) #self.conn.commit() found = [ x for x in members if (x not in not_in_cc and x not in not_found_osrs) ] response = f"{found} were updated!\n" if not_found_osrs: response += f"{not_found_osrs} were not found in the osrs' ironman hiscores.\n" if not_in_cc: response += f"{not_in_cc} were not found on the clan's hiscores.\n" except Exception as e: response = str(e) finally: await ctx.send(response)
async def rmoutdated(self,ctx): try: await ctx.send("Checking for outdated names in the hs.") players_in_hs = get_players_in_hs(self.cur) outdated = [] for name in players_in_hs: stats = getStats(playerURL(name,'iron')) if stats == 404: outdated.append(name) rm_from_hs(self.cur,name) #self.conn.commit() response = f"Players outdated and removed: {outdated}" except Exception as e: response = str(e) finally: await ctx.send(response)
async def superadd(ctx, member, *args): #check spreadsheet if member is already there. converter = MemberConverter() try: member = await converter.convert(ctx, member) if "Member" in [x.name for x in member.roles]: response = f"{member} already has Member rank." else: rsn = " ".join(args) stats = getStats(playerURL(rsn, 'iron')) if stats == 404: response = f"{rsn} not found in the highscores." else: try: names = members_sheet.col_values(11)[1:] except gspread.exceptions.APIError as e: client.login() names = members_sheet.col_values(11)[1:] col0 = members_sheet.col_values(1) if rsn.replace(" ", "_").lower() in names: response = f"{rsn} is already in the memberlist (spreadsheet)." else: role = discord.utils.get(ctx.guild.roles, name="Member") role2 = discord.utils.get(ctx.guild.roles, name="NotifyOn") await member.add_roles(role) await member.add_roles(role2) await member.edit(nick=rsn) index = len(col0) + 1 #members_cell_list = members_sheet.range(f'A{index}:B{index}') members_sheet.update_acell(f"A{index}", rsn) members_sheet.update_acell(f"J{index}", rsn) members_sheet.update_acell( f"B{index}", member.joined_at.strftime("%d %b, %Y")) members_sheet.update_acell(f"L{index}", member.name) response = f"{rsn} has been added to the memberlist, given nickname and role, and added in the clan's HS." except discord.ext.commands.errors.BadArgument: response = f"Member {member} not found." except Exception as e: response = str(e) finally: await ctx.send(response)
async def fullupdate(self,ctx): await ctx.send("Updating all players...") members = get_players_in_hs(self.cur) not_found_osrs = [] for name in members: stats = getStats(playerURL(name,'iron')) if stats == 404: not_found_osrs.append(name) else: try: sql_update_player_hs(self.cur,name,stats_col_names,stats) except Exception as e: print(str(e)) response = "Finished updating." if not_found_osrs: response+= f"{not_found_osrs} were not found in the osrs' hiscores.\n" await ctx.send(response) await ctx.send(response)
async def update_clan_tracker(self, ctx): try: await ctx.send("Updating players...") players = get_players_in_tracker(self.cur) not_found_osrs = [] for name in players: stats = getStats(playerURL(name, 'iron')) if stats == 404: not_found_osrs.append(name) else: sql_update_player_hs(self.cur, name, stats, stats_col_names) #self.conn.commit() response = 'Tracker updated.\n' if not_found_osrs: response += f'These names {not_found_osrs} are outdated.' except Exception as e: response = str(e) finally: await ctx.send(response)
async def reset_my_tracker(self, ctx): try: name = coded_string(ctx.message.author.display_name) personal_tracker_names = get_players_in_personal_tracker(self.cur) if name in personal_tracker_names: stats = getStats(playerURL(name, 'iron')) if stats == 404: response = f"{name} is not on the osrs hiscores." else: reset_personal_tracker(self.cur, name) add_personal_tracker(self.cur, name, stats) sql_update_player_hs(self.cur, name, stats, stats_col_names) #self.conn.commit() response = f"Your tracker has been reset!" else: response = "You are not being tracked, to start your tracker do !hs startmytracker" except Exception as e: response = str(e) finally: await ctx.send(response)
async def start_clan_tracker(self, ctx, *members): try: await ctx.send("Adding players to the tracker.") players = get_players_in_tracker(self.cur) players_in_hs = get_players_in_hs(self.cur) already_being_tracked = [] not_found_osrs = [] for name in members: name = name.lower() if name in players: already_being_tracked.append(name) else: stats = getStats(playerURL(name, 'iron')) if stats == 404: not_found_osrs.append(name) else: if name not in players_in_hs: sql_add_player_hs(self.cur, name, stats) else: sql_update_player_hs(self.cur, name, stats, stats_col_names) add_clan_tracker(self.cur, name, stats) #self.conn.commit() response = '' if already_being_tracked: response += f'{already_being_tracked} are already being tracked.\n' if not_found_osrs: response += f'{not_found_osrs} were not found in the osrs hiscores.\n' found = [ x for x in members if (x not in already_being_tracked and x not in not_found_osrs) ] response += f'{found} were added to the clan tracker.' except Exception as e: response = str(e) finally: await ctx.send(response)
async def start_my_tracker(self, ctx): try: name = coded_string(ctx.message.author.display_name) personal_tracker_names = get_players_in_personal_tracker(self.cur) players_in_hs = get_players_in_hs(self.cur) if name not in personal_tracker_names: stats = getStats(playerURL(name, 'iron')) if stats == 404: response = f"{name} is not on the osrs hiscores." else: if name not in players_in_hs: sql_add_player_hs(self.cur, name, stats) else: sql_update_player_hs(self.cur, name, stats, stats_col_names) add_personal_tracker(self.cur, name, stats) #self.conn.commit() response = f"You are now being tracked. use !hs update 'your_name' to update your stats and !hs mytracker 'skill' to check your progress." else: response = "You are already being tracked. do: !hs resetmytracker, to restart your tracker. " except Exception as e: response = str(e) finally: await ctx.send(response)
def tracker(tracker_sheet, start_sheet, client, start=0, starting_cell=2): try: names = start_sheet.col_values(2)[1:] except gspread.exceptions.APIError: client.login() names = start_sheet.col_values(2)[1:] tracker_values = tracker_sheet.get_all_values()[1:] tracker_list = [] if start: date_cell_list = tracker_sheet.range(f'AX2:AX{len(names)+1}') else: date_cell_list = tracker_sheet.range(f'AY2:AY{len(names)+1}') # start_list = [] tracker_cell_list = tracker_sheet.range( f'B{starting_cell}:AW{len(names)+1}') # start_cell_list = start_sheet.range(f'C{starting_cell}:F{len(names)+1}') not_found = [] for index, name in enumerate(names[starting_cell - 2:], start=starting_cell): stats = getStats(playerURL(name, 'iron')) if stats != 404: player_skills, _, _ = createDicts(parseStats(stats)) # start_list.append((player_skills["Overall"],player_skills["Overall"],player_skills["Overall_Xp"],player_skills["Overall_Xp"])) player_skills = [ value for key, value in player_skills.items() if "Xp" in key ] player_skills = [ x for pair in zip(player_skills, player_skills) for x in pair ] print( f"updating {index}. {name} total {player_skills[0]} xp {player_skills[1]}" ) tracker_list.append(player_skills) else: print(f"{name} not found in highscores.") tracker_list.append(tracker_values[index - 2][1:-2]) not_found.append(name) tracker_list = [ int(item) if item != "" else item for sublist in tracker_list for item in sublist ] for i, val in enumerate(tracker_list): if val: if not start: if i % 2 == 1: tracker_cell_list[i].value = int(val) else: tracker_cell_list[i].value = int(val) if tracker_cell_list[i].value: tracker_cell_list[i].value = int(tracker_cell_list[i].value) today = datetime.now() today = today.strftime("%Y/%m/%d") for cell in date_cell_list: cell.value = today try: names = start_sheet.col_values(2)[1:] except gspread.exceptions.APIError: client.login() names = start_sheet.col_values(2)[1:] tracker_sheet.update_cells(tracker_cell_list) tracker_sheet.update_cells(date_cell_list) print("Trackers updated.") return not_found
def update_all(bosses_sheet, skills_sheet, start_sheet, client, starting_cell=2, tracker_sheet=None): try: names = start_sheet.col_values(2)[1:] except gspread.exceptions.APIError: client.login() names = start_sheet.col_values(2)[1:] bosses_values = bosses_sheet.get_all_values()[1:] start_values = start_sheet.get_all_values()[1:] bosses_list = [] skills_list = [] start_list_lvl = [] start_list_xp = [] if tracker_sheet: print("There is tracker sheet!") tracker_values = tracker_sheet.get_all_values()[1:] tracker_values = [x[:-2] for x in tracker_values] tracker_list = [] date_cell_list2 = tracker_sheet.range( f'AX{starting_cell}:AX{len(names)+1}') tracker_cell_list = tracker_sheet.range( f'B{starting_cell}:AW{len(names)+1}') # start_list = [] bosses_cell_list = bosses_sheet.range(f'B{starting_cell}:BA{len(names)+1}') skills_cell_list = skills_sheet.range(f'B{starting_cell}:AW{len(names)+1}') start_cell_list = start_sheet.range(f'D{starting_cell}:D{len(names)+1}') start_cell_list.extend( start_sheet.range(f'F{starting_cell}:F{len(names)+1}')) # start_cell_list = start_sheet.range(f'C{starting_cell}:F{len(names)+1}') date_cell_list = start_sheet.range(f'I{starting_cell}:I{len(names)+1}') not_found = [] outdated_names = start_sheet.range(f'J{starting_cell}:J{len(names)+1}') for index, name in enumerate(names[starting_cell - 2:], start=starting_cell): stats = getStats(playerURL(name, 'iron')) if stats != 404: player_skills, player_clues, player_bosses = createDicts( parseStats(stats)) player_bosses = [player_skills["Overall"]] + list( player_clues.values()) + list(player_bosses.values()) # start_list.append((player_skills["Overall"],player_skills["Overall"],player_skills["Overall_Xp"],player_skills["Overall_Xp"])) if tracker_sheet: temp_list = [ value for key, value in player_skills.items() if "Xp" in key ] tracker_list.append(temp_list) player_skills = list(player_skills.values()) print( f"updating {index}. {name} total {player_skills[0]} xp {player_skills[1]}" ) bosses_list.append(player_bosses) skills_list.append(player_skills) start_list_lvl.append(player_skills[0]) start_list_xp.append(player_skills[1]) else: print(f"{name} not found in highscores.") bosses_list.append(bosses_values[index - 2][1:]) skills_list.append(["" for i in range(48)]) # start_list.append([int(x) if x else x for x in start_values[index-2][2:6]]) start_list_lvl.append(start_values[index - 2][3]) start_list_xp.append(start_values[index - 2][5]) if tracker_sheet: #temp_list = [x for x,i in enumerate(tracker_values[index-2][1:-2]) if i%2==0] tracker_list.append(["" for i in range(24)]) not_found.append(name) bosses_list = [item for sublist in bosses_list for item in sublist] skills_list = [item for sublist in skills_list for item in sublist] start_list = start_list_lvl + start_list_xp if tracker_sheet: tracker_cell_list = [ x for i, x in enumerate(tracker_cell_list) if i % 2 == 0 ] tracker_list = [ int(item) if item != "" else item for sublist in tracker_list for item in sublist ] for i, val in enumerate(tracker_list): #print(tracker_cell_list[i].value, val) tracker_cell_list[i].value = int(val) if val else val if tracker_cell_list[i].value: tvar = tracker_cell_list[i].value tracker_cell_list[i].value = int(tvar) today = datetime.now() today = today.strftime("%Y/%m/%d") for cell in date_cell_list2: cell.value = today tracker_sheet.update_cells(tracker_cell_list) tracker_sheet.update_cells(date_cell_list2) for i, val in enumerate(bosses_list): if val: bosses_cell_list[i].value = int(val) for i, val in enumerate(skills_list): skills_cell_list[i].value = int(val) if val != "" else "" for i, val in enumerate(start_list): if val: start_cell_list[i].value = int(val) today = datetime.now() today = today.strftime("%Y/%m/%d") for i, val in enumerate(date_cell_list): date_cell_list[i].value = today for i, _ in enumerate(outdated_names): if len(not_found) > i: val = not_found[i] else: val = "" outdated_names[i].value = val try: names = start_sheet.col_values(2)[1:] except gspread.exceptions.APIError: client.login() names = start_sheet.col_values(2)[1:] bosses_sheet.update_cells(bosses_cell_list) skills_sheet.update_cells(skills_cell_list) start_sheet.update_cells(start_cell_list) start_sheet.update_cells(date_cell_list) start_sheet.update_cells(outdated_names) print("Sheets updated.") return not_found