diff --git a/cogs/players.py b/cogs/players.py index d3dea29..2e4c5f9 100644 --- a/cogs/players.py +++ b/cogs/players.py @@ -86,8 +86,10 @@ class Players(commands.Cog): self.bot = bot self.player_list = {} self.scorecards = {} + self.voice_channels = [] self.build_master_player_list.start() + self.live_scorecard_loop.start() # async def cog_command_error(self, ctx, error): # await ctx.send(f'{error}\n\nRun !help to see the command requirements') @@ -146,6 +148,41 @@ class Players(commands.Cog): logging.info(f'player list count: {len(self.player_list)}') logging.debug(f'player list: {self.player_list}') + @tasks.loop(minutes=3) + async def live_scorecard_loop(self): + guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) + if not guild: + logging.error(f'live_scorecard_loop - could not pull guild / retrying in 15 seconds') + await asyncio.sleep(15) + guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) + logging.info(f'live_scorecard_loop - guild: {guild}') + + score_channel = discord.utils.get(guild.text_channels, name='live-sba-scores') + player_role = get_role(score_channel, SBA_PLAYERS_ROLE_NAME, bot=self.bot) + + try: + if len(self.voice_channels) > 0: + game_strings = [] + for x in self.scorecards.values(): + game_strings.append(x.get_value('A1')) + + if len(game_strings) > 0: + # Clear old messages + async for message in score_channel.history(limit=25): + await message.delete() + + embed = get_team_embed('SBa Scoreboard') + embed.add_field(name='Live Games', value="\n\n".join(game_strings)) + await score_channel.set_permissions(player_role, read_messages=True) + await score_channel.send(content=None, embed=embed) + return + + await score_channel.set_permissions(player_role, read_messages=False) + except Exception as e: + await send_to_channel(self.bot, 'commissioners-office', f'Could not update live scorecard:\n\n{e}') + logging.error(f'Could not update live scorecard: {e}') + + @staticmethod async def update_injuries(ctx): current = await db_get('current') @@ -1667,14 +1704,18 @@ class Players(commands.Cog): category=discord.utils.get(ctx.guild.categories, name=f'Voice Channels') ) logging.info(f'Just created voice channel: {channel_name} for {ctx.author}') + self.voice_channels.append(this_vc) await ctx.send(f'Just created {this_vc} for you!') while True: await asyncio.sleep(900) if len(this_vc.members) == 0: - await this_vc.delete() - logging.info(f'Just deleted voice channel: {channel_name}') + self.voice_channels.remove(this_vc) + try: + await this_vc.delete() + except Exception as e: + logging.error(f'could not delete {this_vc}.') break @commands.command(name='private', help='Get private vc') @@ -1746,12 +1787,14 @@ class Players(commands.Cog): overwrites=overwrites, category=discord.utils.get(ctx.guild.categories, name=f'Voice Channels') ) + self.voice_channels.append(this_vc) await ctx.send(f'Just created {this_vc} for you!') while True: await asyncio.sleep(900) if len(this_vc.members) == 0: + self.voice_channels.remove(this_vc) try: await this_vc.delete() except Exception as e: