async def get_player_role_for_bracket(self, guild, tournament, user, user_name, player_role): """Gives the player role of the bracket to the user, if he is a player of this bracket.""" bracket = tournament.current_bracket try: is_player, team_info = await self.is_a_player(bracket, user_name) except HttpError as e: raise tosurnament.SpreadsheetHttpError(e.code, e.operation, bracket.name, "players") if not is_player: return False roles_to_give = [player_role] bracket_role = tosurnament.get_role(guild.roles, bracket.role_id, bracket.name) if bracket_role: roles_to_give.append(bracket_role) if team_info: team_name = team_info.team_name.value team_role = tosurnament.get_role(guild.roles, None, team_name) if not team_role: try: team_role = await guild.create_role(name=team_name, mentionable=False) except discord.InvalidArgument: raise tosurnament.InvalidRoleName(team_name) if team_role: roles_to_give.append(team_role) if team_name == team_info.players[0].value: team_captain_role = tosurnament.get_role( guild.roles, tournament.team_captain_role_id, "Team Captain") if team_captain_role: roles_to_give.append(team_captain_role) await user.add_roles(*roles_to_give) return True
async def change_name_in_schedules_spreadsheet(self, ctx, bracket, previous_name, new_name, user_roles): schedules_spreadsheet = bracket.schedules_spreadsheet if not schedules_spreadsheet: return changed_name = False worksheet = schedules_spreadsheet.worksheet if user_roles.player: changed_name |= worksheet.change_value_in_range( schedules_spreadsheet.range_team1, previous_name, new_name) changed_name |= worksheet.change_value_in_range( schedules_spreadsheet.range_team2, previous_name, new_name) for role_name, role_store in user_roles.get_staff_roles_as_dict( ).items(): if role_store: changed_name |= worksheet.change_value_in_range( getattr(schedules_spreadsheet, "range_" + role_name.lower()), previous_name, new_name, ) if changed_name: try: schedules_spreadsheet.spreadsheet.update() except HttpError as e: raise tosurnament.SpreadsheetHttpError(e.code, e.operation, bracket.name, "schedules")
async def change_name_in_player_spreadsheet(self, ctx, bracket, previous_name, new_name): players_spreadsheet = bracket.players_spreadsheet if not bracket.players_spreadsheet: return changed_name = players_spreadsheet.worksheet.change_value_in_range( players_spreadsheet.range_team, previous_name, new_name) if changed_name: try: players_spreadsheet.spreadsheet.update() except HttpError as e: raise tosurnament.SpreadsheetHttpError(e.code, e.operation, bracket.name, "players")
async def step8_write_in_spreadsheet(self, bracket, match_info, prbuilder): match_info.score_team1.value = prbuilder.get_score_team1() match_info.score_team2.value = prbuilder.get_score_team2() mp_links = osu.build_mp_links(prbuilder.mp_links.split("\n")) i = 0 while i < len(match_info.mp_links) and i < len(mp_links): if i + 1 == len(match_info.mp_links): match_info.mp_links[i].value = "/".join(mp_links[i:]) else: match_info.mp_links[i].value = mp_links[i] i += 1 try: bracket.schedules_spreadsheet.spreadsheet.update() except HttpError as e: raise tosurnament.SpreadsheetHttpError(e.code, e.operation, bracket.name, "schedules")
async def take_or_drop_match(self, guild_id, author, channel, match_ids, take, user_roles): if not match_ids: raise commands.UserInputError() tournament = self.get_tournament(guild_id) try: tosurnament_user = self.get_verified_user(author.id) staff_name = tosurnament_user.osu_name except (tosurnament.UserNotLinked, tosurnament.UserNotVerified): # ! Temporary for nik's tournament staff_name = author.display_name invalid_match_ids = [] for bracket in tournament.brackets: if self.take_matches(bracket, match_ids, staff_name, user_roles, take, invalid_match_ids): try: bracket.schedules_spreadsheet.spreadsheet.update() except HttpError as e: raise tosurnament.SpreadsheetHttpError(e.code, e.operation, bracket.name, "schedules") await channel.send( self.build_take_match_reply(escape_markdown(staff_name), user_roles, take, invalid_match_ids) )
async def reaction_on_staff_reschedule_message(self, message_id, emoji, guild, channel, user): """Removes the referee from the schedule spreadsheet""" staff_reschedule_message = ( self.bot.session.query(StaffRescheduleMessage).where( StaffRescheduleMessage.message_id == message_id).first()) if not staff_reschedule_message or staff_reschedule_message.in_use: return if user.id != staff_reschedule_message.staff_id: return if emoji.name != "❌": return try: tosurnament_user = self.get_verified_user(user.id) osu_name = tosurnament_user.osu_name except (tosurnament.UserNotLinked, tosurnament.UserNotVerified ): # ! Temporary for nik's tournament osu_name = user.display_name staff_reschedule_message.in_use = True self.bot.session.update(staff_reschedule_message) try: tournament = self.get_tournament(guild.id) bracket = self.bot.session.query(Bracket).where( Bracket.id == staff_reschedule_message.bracket_id).first() if not bracket: raise tosurnament.UnknownError("Bracket not found") schedules_spreadsheet = bracket.schedules_spreadsheet if not schedules_spreadsheet: raise tosurnament.NoSpreadsheet() match_id = staff_reschedule_message.match_id match_info = MatchInfo.from_id(schedules_spreadsheet, match_id) staff_cells = match_info.referees + match_info.streamers + match_info.commentators for staff_cell in staff_cells: if staff_cell.has_value(osu_name): if schedules_spreadsheet.use_range: staff_cell.value = "" else: staffs = staff_cell.value.split("/") if len(staffs) == 2 and staffs[0].strip() == osu_name: staff_cell.value = staffs[1].strip() elif len(staffs) == 2 and staffs[1].strip == osu_name: staff_cell.value = staffs[0].strip() elif len( staffs) == 1 and staffs[0].strip() == osu_name: staff_cell.value = "" try: schedules_spreadsheet.spreadsheet.update() except HttpError as e: raise tosurnament.SpreadsheetHttpError(e.code, e.operation, bracket.name, "schedules") to_channel = channel staff_channel = self.bot.get_channel(tournament.staff_channel_id) if staff_channel: to_channel = staff_channel await self.send_reply( to_channel, "reschedule", "removed_from_match", user.mention, match_id, ) self.bot.session.delete(staff_reschedule_message) except Exception as e: staff_reschedule_message.in_use = False self.bot.session.update(staff_reschedule_message) await self.reaction_on_staff_reschedule_message_handler(channel, e)
async def agree_to_reschedule(self, reschedule_message, guild, channel, user, tournament): """Updates the schedules spreadsheet with reschedule time.""" schedules_spreadsheet = tournament.current_bracket.schedules_spreadsheet if not schedules_spreadsheet: raise tosurnament.NoSpreadsheet(tournament.current_bracket.name, "schedules") match_id = reschedule_message.match_id match_info = MatchInfo.from_id(schedules_spreadsheet, match_id) previous_date = datetime.datetime.strptime( reschedule_message.previous_date, tosurnament.DATABASE_DATE_FORMAT) new_date = datetime.datetime.strptime(reschedule_message.new_date, tosurnament.DATABASE_DATE_FORMAT) date_format = "%d %B" if schedules_spreadsheet.date_format: date_format = schedules_spreadsheet.date_format if schedules_spreadsheet.range_date and schedules_spreadsheet.range_time: match_info.date.value = new_date.strftime(date_format) match_info.time.value = new_date.strftime("%H:%M") elif schedules_spreadsheet.range_date: match_info.date.value = new_date.strftime(date_format + " %H:%M") elif schedules_spreadsheet.range_time: match_info.time.value = new_date.strftime(date_format + " %H:%M") else: raise tosurnament.UnknownError("No date range") staff_name_to_ping = set() staff_cells = match_info.referees + match_info.streamers + match_info.commentators for staff_cell in staff_cells: if schedules_spreadsheet.use_range: staff_name_to_ping.add(staff_cell.value) else: staffs = staff_cell.value.split("/") for staff in staffs: staff_name_to_ping.add(staff.strip()) staff_to_ping = list( filter( None, [guild.get_member_named(name) for name in staff_name_to_ping])) try: schedules_spreadsheet.spreadsheet.update() except HttpError as e: raise tosurnament.SpreadsheetHttpError( e.code, e.operation, tournament.current_bracket.name, "schedules") self.bot.session.delete(reschedule_message) ally_to_mention = None if reschedule_message.ally_team_role_id: ally_to_mention = tosurnament.get_role( guild.roles, reschedule_message.ally_team_role_id) if not ally_to_mention: ally_to_mention = guild.get_member(reschedule_message.ally_user_id) if ally_to_mention: await self.send_reply( channel, "reschedule", "accepted", ally_to_mention.mention, match_id, ) else: raise tosurnament.OpponentNotFound(user.mention) previous_date_string = previous_date.strftime( tosurnament.PRETTY_DATE_FORMAT) new_date_string = new_date.strftime(tosurnament.PRETTY_DATE_FORMAT) staff_channel = None if tournament.staff_channel_id: staff_channel = self.bot.get_channel(tournament.staff_channel_id) if staff_to_ping: if staff_channel: to_channel = staff_channel else: to_channel = channel for staff in staff_to_ping: sent_message = await self.send_reply( to_channel, "reschedule", "staff_notification", staff.mention, match_id, match_info.team1.value, match_info.team2.value, previous_date_string, new_date_string, ) staff_reschedule_message = StaffRescheduleMessage( tournament_id=reschedule_message.tournament_id, bracket_id=tournament.current_bracket.id, message_id=sent_message.id, match_id=match_id, new_date=reschedule_message.new_date, staff_id=staff.id, ) self.bot.session.add(staff_reschedule_message) elif staff_channel: await self.send_reply( staff_channel, "reschedule", "no_staff_notification", match_id, match_info.team1.value, match_info.team2.value, previous_date_string, new_date_string, ) allowed_reschedules = (self.bot.session.query(AllowedReschedule).where( AllowedReschedule.tournament_id == tournament.id).all()) for allowed_reschedule in allowed_reschedules: if allowed_reschedule.match_id.upper() == match_id.upper(): self.bot.session.delete(allowed_reschedule)