Skip to content

Commit

Permalink
Merge pull request #170 from DMcP89/feature/trade-command-rework
Browse files Browse the repository at this point in the history
Feature/trade command rework
  • Loading branch information
DMcP89 authored Oct 1, 2024
2 parents 036451c + 0d88573 commit bfdb10b
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 208 deletions.
16 changes: 13 additions & 3 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
25 changes: 17 additions & 8 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
Loading

0 comments on commit bfdb10b

Please sign in to comment.