Refactor !standings and !wc for Pagination

This commit is contained in:
Cal Corum 2023-02-26 19:17:45 -06:00
parent a4002df310
commit 6efa5a0312

View File

@ -1,4 +1,6 @@
import math
import discord
import pydantic
import re
@ -288,16 +290,16 @@ class Players(commands.Cog):
# f'told to delete 1-character messages due to spam.'
# )
@staticmethod
def get_standings_embed(current, progress, al_standings, nl_standings):
embed = discord.Embed(title=f'Season {current["season"]} | Week {current["week"]} | '
f'{progress["games_played"]}/{progress["game_count"]} games played',
color=0xB70000)
embed.add_field(name=f'**Full Standings**', value=SBA_STANDINGS_URL, inline=False)
embed.add_field(name=f'**American League**', value=al_standings, inline=False)
embed.add_field(name=f'**National League**', value=nl_standings, inline=False)
return embed
# @staticmethod
# def get_standings_embed(current, progress, al_standings, nl_standings):
# embed = discord.Embed(title=f'Season {current["season"]} | Week {current["week"]} | '
# f'{progress["games_played"]}/{progress["game_count"]} games played',
# color=0xB70000)
# embed.add_field(name=f'**Full Standings**', value=SBA_STANDINGS_URL, inline=False)
# embed.add_field(name=f'**American League**', value=al_standings, inline=False)
# embed.add_field(name=f'**National League**', value=nl_standings, inline=False)
#
# return embed
@commands.command(name='team', aliases=['roster', 'myboys', 'mybois'], help='Get team overview')
async def team_command(self, ctx, *abbrev):
@ -690,9 +692,7 @@ class Players(commands.Cog):
await ctx.send(content=None, embed=embed)
@commands.command(name='standings', help='Current standings')
async def standings_command(self, ctx):
current = await get_current()
async def get_division_standings(self, current) -> discord.Embed:
div_one = await get_standings(current['season'], league_abbrev='al', division_abbrev='e')
div_two = await get_standings(current['season'], league_abbrev='al', division_abbrev='w')
div_three = await get_standings(current['season'], league_abbrev='nl', division_abbrev='e')
@ -728,14 +728,10 @@ class Players(commands.Cog):
embed.add_field(name=f'**AL West**', value=div_two_standings, inline=False)
embed.add_field(name=f'**NL East**', value=div_three_standings, inline=False)
embed.add_field(name=f'**NL West**', value=div_four_standings, inline=False)
embed.set_footer(text='Run !wildcard to see wildcard standings')
await ctx.send(content=None, embed=embed)
@commands.command(name='wildcard', aliases=['wc'], help='Current wildcard')
async def wildcard_command(self, ctx):
current = await get_current()
return embed
async def get_wildcard_standings(self, current) -> discord.Embed:
al_teams = await get_standings(current['season'], league_abbrev='al')
nl_teams = await get_standings(current['season'], league_abbrev='nl')
@ -757,9 +753,64 @@ class Players(commands.Cog):
embed.add_field(name=f'**Full Standings**', value=SBA_STANDINGS_URL, inline=False)
embed.add_field(name=f'**AL Wildcard**', value=al_wildcard, inline=False)
embed.add_field(name=f'**NL Wildcard**', value=nl_wildcard, inline=False)
embed.set_footer(text='Run !standings to see divisional standings')
await ctx.send(content=None, embed=embed)
return embed
async def standings_button_loop(self, ctx, start: Literal['division', 'wildcard']):
current = await get_current()
div_embed = await self.get_division_standings(current)
wc_embed = await self.get_wildcard_standings(current)
logging.info(f'div_embed: {div_embed}\nwc_embed: {wc_embed}')
view = Pagination(responders=[ctx.author], timeout=15)
view.left_button.label = 'Division'
view.right_button.label = 'Wildcard'
if start == 'division':
embed = div_embed
view.left_button.disabled = True
else:
embed = wc_embed
view.right_button.disabled = True
st_message = await ctx.send(content=None, embed=embed, view=view)
while True:
await view.wait()
if view.value:
logging.info(f'standings_button_loop - view.value: {view.value}')
if view.value == 'cancel':
await st_message.edit(view=None)
return
elif view.value == 'left':
start = 'division'
elif view.value == 'right':
start = 'wildcard'
else:
await st_message.edit(view=None)
return
view = Pagination(responders=[ctx.author], timeout=15)
view.left_button.label = 'Division'
view.right_button.label = 'Wildcard'
if start == 'division':
embed = div_embed
view.left_button.disabled = True
else:
embed = wc_embed
view.right_button.disabled = True
logging.info(f'standings_button_loop - start: {start} / embed == div_embed? {embed == div_embed} / '
f'embed == wc_embed {embed == wc_embed}')
await st_message.delete()
st_message = await ctx.send(content=None, embed=embed, view=view)
@commands.command(name='standings', help='Current standings')
async def standings_command(self, ctx):
await self.standings_button_loop(ctx, 'division')
@commands.command(name='wildcard', aliases=['wc'], help='Current wildcard')
async def wildcard_command(self, ctx):
await self.standings_button_loop(ctx, 'wildcard')
# @commands.command(name='setinjury', aliases=['clearinjury'], help='!SetInjury or !ClearInjury')
# @commands.has_any_role(SBA_PLAYERS_ROLE_NAME)