Skip to content

Commit

Permalink
Merge pull request #171 from DMcP89/release/v0.4.3-Beta
Browse files Browse the repository at this point in the history
Release/v0.4.3 beta
  • Loading branch information
DMcP89 authored Oct 4, 2024
2 parents 070ebb6 + c58dceb commit e1ad9bd
Show file tree
Hide file tree
Showing 7 changed files with 437 additions and 337 deletions.
42 changes: 33 additions & 9 deletions harambot/cogs/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ async def roster(self, interaction: discord.Interaction, team_name: str):
async def trade(self, interaction: discord.Interaction):
logger.info("Command:Trade called in %i", interaction.guild_id)
await interaction.response.defer()
if (
self.yahoo_api.get_settings(guild_id=interaction.guild_id)[
"trade_ratify_type"
]
!= "vote"
):
await interaction.followup.send(
"Trade command only available for leagues with vote ratification"
)
return
latest_trade = self.yahoo_api.get_latest_trade(
guild_id=interaction.guild_id
)
Expand All @@ -116,7 +126,7 @@ async def trade(self, interaction: discord.Interaction):
)
return

teams = self.yahoo_api.get_teams()
teams = self.yahoo_api.get_teams(guild_id=interaction.guild_id)
if teams is None:
await interaction.followup.send(self.error_message)
return
Expand All @@ -131,7 +141,7 @@ async def trade(self, interaction: discord.Interaction):
player_set0.append(player["name"])
api_details = (
self.get_player_text(
self.yahoo_api.get_player_details(player["name"])
self.yahoo_api.get_player_details(player["name"], guild_id=interaction.guild_id)
)
+ "\n"
)
Expand All @@ -145,7 +155,7 @@ async def trade(self, interaction: discord.Interaction):
player_set1_details = ""
for player in latest_trade["tradee_players"]:
player_set1.append(player["name"])
player_details = self.yahoo_api.get_player_details(player["name"])
player_details = self.yahoo_api.get_player_details(player["name"], guild_id=interaction.guild_id)
if player_details is None:
await interaction.followup.send(self.error_message)
return
Expand Down Expand Up @@ -218,15 +228,20 @@ async def stats_autocomplete(
name="stats", description="Returns the details of the given player"
)
@app_commands.autocomplete(player_name=stats_autocomplete)
async def stats(self, interaction: discord.Interaction, player_name: str):
async def stats(
self,
interaction: discord.Interaction,
player_name: str,
week: Optional[int] = None,
):
logger.info(
"Command:Stats called in %i with player_name:%s",
interaction.guild_id,
player_name,
)
await interaction.response.defer()
player = self.yahoo_api.get_player_details(
player_name, guild_id=interaction.guild_id
player_name, guild_id=interaction.guild_id, week=week
)
if player:
embed = self.get_player_embed(player)
Expand All @@ -244,12 +259,21 @@ def get_player_embed(self, player):
embed.add_field(name="Team", value=player["editorial_team_abbr"])
if "bye_weeks" in player:
embed.add_field(name="Bye", value=player["bye_weeks"]["week"])
if "player_points" in player:
embed.add_field(
name="Total Points", value=player["player_points"]["total"]
)
embed.add_field(name="Owner", value=player["owner"])
embed.set_thumbnail(url=player["image_url"])
if "total_points" in player["stats"]:
embed.add_field(
name="Total Points",
value=player["stats"]["total_points"],
inline=False,
)
del player["stats"]["player_id"]
del player["stats"]["name"]
del player["stats"]["position_type"]
for key, value in player["stats"].items():
if key == "total_points":
continue
embed.add_field(name=key, value=value)
return embed

def get_player_text(self, player):
Expand Down
40 changes: 30 additions & 10 deletions harambot/yahoo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@


class Yahoo:

scoring_type = None
league_id = None
league_type = None
current_league = None
Expand All @@ -29,6 +27,9 @@ def handle_oauth(f):
@functools.wraps(f)
def wrapper(self, *args, **kwargs):
guild_id = kwargs.get("guild_id")
if guild_id is None:
logger.error("Guild id not provided to Yahoo API")
return None
guild = Guild.get_or_none(Guild.guild_id == str(guild_id))
if guild is None:
logger.error(
Expand Down Expand Up @@ -73,9 +74,6 @@ def league(self):
self.league_id = id
break
self.current_league = gm.to_league(self.league_id)
self.scoring_type = self.current_league.settings()[
"scoring_type"
]
return self.current_league
except (RuntimeError, AssertionError):
logger.error(
Expand All @@ -84,9 +82,6 @@ def league(self):
self.current_league = gm.to_league(
gm.game_id() + ".l." + self.league_id
)
self.scoring_type = self.current_league.settings()[
"scoring_type"
]
return self.current_league
except Exception:
logger.exception(
Expand All @@ -96,6 +91,20 @@ def league(self):
)
return None

@cached(cache, key=functools.partial(keys.hashkey, "get_settings"))
@handle_oauth
def get_settings(self, guild_id):
try:
return self.league().settings()
except Exception:
logger.exception(
"Error while fetching settings for league {} in guild {}".format(
self.league_id,
guild_id,
)
)
return None

@cached(cache, key=functools.partial(keys.hashkey, "get_teams"))
@handle_oauth
def get_teams(self, guild_id):
Expand Down Expand Up @@ -169,12 +178,23 @@ def get_roster(self, team_name, guild_id):
)
return None

@cached(cache)
# @cached(cache)
@handle_oauth
def get_player_details(self, player_name, guild_id):
def get_player_details(self, player_name, guild_id, week=None):
try:
player = self.league().player_details(player_name)[0]
player["owner"] = self.get_player_owner(player["player_id"])
if week:
player["stats"] = self.league().player_stats(
[player["player_id"]],
req_type="week",
week=week,
)[0]
else:
player["stats"] = self.league().player_stats(
[player["player_id"]],
req_type="season",
)[0]
return player
except Exception:
logger.exception(
Expand Down
Loading

0 comments on commit e1ad9bd

Please sign in to comment.