From 6870f7ccd367896ad59d2b86234291a85081e91a Mon Sep 17 00:00:00 2001 From: insert Date: Sun, 27 Apr 2025 18:30:36 -0400 Subject: [PATCH] feat: game history --- bot.py | 1 + cogs/liveupdate.py | 6 ++--- cogs/team.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index 0811c41..9a14ec8 100644 --- a/bot.py +++ b/bot.py @@ -37,6 +37,7 @@ async def on_ready(): bot.db = db bot.cur = cur bot.load_extension('cogs.liveupdate') + bot.load_extension('cogs.error') bot.load_extension('cogs.team') bot.add_all_application_commands() await bot.sync_all_application_commands() diff --git a/cogs/liveupdate.py b/cogs/liveupdate.py index b29015d..c0b35e9 100644 --- a/cogs/liveupdate.py +++ b/cogs/liveupdate.py @@ -241,10 +241,10 @@ class liveupdate(commands.Cog): DELETE from liveupdate WHERE messageid = {messageid} """) await self.bot.db.commit() - await message.edit(f"An error ocdbed in this live update\n{e}") + await message.edit(f"An error occoured in this live update\n{e}") - @tasks.loop(seconds=30.0) + @tasks.loop(seconds=120.0) async def checkspotlightsubscriptions(self): try: await self.bot.wait_until_ready() @@ -281,7 +281,7 @@ class liveupdate(commands.Cog): return - @tasks.loop(seconds=35.0) + @tasks.loop(seconds=120.0) async def checkteamsubscriptions(self): try: print("refreshing team subscriptions") diff --git a/cogs/team.py b/cogs/team.py index e4b86f2..31e3491 100644 --- a/cogs/team.py +++ b/cogs/team.py @@ -6,6 +6,7 @@ import requests import re import asyncio import nextcord +import itertools from nextcord.ext import commands, application_checks from nextcord import TextInputStyle, IntegrationType @@ -193,6 +194,32 @@ class TeamView(nextcord.ui.View): for i in splistats: await interaction.followup.send(f"```ansi\n{i}```",ephemeral=True) + @nextcord.ui.button( + label="Game History", style=nextcord.ButtonStyle.green, custom_id="team:gamehistory" + ) + async def gamehistorybutton(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): + await interaction.response.defer() + ogmsg = interaction.message.embeds + embed = ogmsg[0] + teamid = embed.footer.text + data = requests.get(f"https://mmolb.com/api/team/{teamid}").json() + history = requests.get(f"https://lunanova.space/mmolb/api/gamesbyteam/{teamid}").json() + color = tuple(int(data["Color"][i:i+2], 16) for i in (0, 2, 4)) + embed = nextcord.Embed(title=f"Last ten games for the {data["Location"]} {data["Name"]} {data["Emoji"]}", colour = nextcord.Color.from_rgb(color[0], color[1], color[2])) + embed.set_footer(text=teamid) + for index in itertools.islice(history, (len(history)-10 if len(history)-10 > 0 else 0) , len(history)): + if history[index]["away_team_id"] != teamid: + awayteamid = history[index]["away_team_id"] + otherscore = history[index]["away_score"] + ourscore = history[index]["home_score"] + else: + awayteamid = history[index]["home_team_id"] + otherscore = history[index]["home_score"] + ourscore = history[index]["away_score"] + tempdata = requests.get(f"https://mmolb.com/api/team/{awayteamid}").json() + embed.add_field(name=f"vs. {tempdata["Location"]} {tempdata["Name"]} {tempdata["Emoji"]} ({ourscore} - {otherscore})", value=f"[watch]()", inline=False) + await interaction.followup.send(embed=embed,ephemeral=True) + class team(commands.Cog): def __init__(self, bot: commands.Bot): @@ -277,6 +304,43 @@ class team(commands.Cog): embed.add_field(name="Championships", value=f"{data["Championships"]}", inline=True) await interaction.edit_original_message(embed=embed,view=TeamView()) + @nextcord.slash_command( + name="gamehistory", + description="Get the last 10 games played by a team", + integration_types=[ + IntegrationType.user_install, + IntegrationType.guild_install, + ], + contexts=[ + nextcord.InteractionContextType.guild, + nextcord.InteractionContextType.bot_dm, + nextcord.InteractionContextType.private_channel, + ], + force_global=True, + ) + async def gamehistory(self, interaction: nextcord.Interaction, teamid: str = nextcord.SlashOption( + name = "team", + description = "The team" + )): + await interaction.response.defer() + data = requests.get(f"https://mmolb.com/api/team/{teamid}").json() + history = requests.get(f"https://lunanova.space/mmolb/api/gamesbyteam/{teamid}").json() + color = tuple(int(data["Color"][i:i+2], 16) for i in (0, 2, 4)) + embed = nextcord.Embed(title=f"Last ten games for the {data["Location"]} {data["Name"]} {data["Emoji"]}", colour = nextcord.Color.from_rgb(color[0], color[1], color[2])) + embed.set_footer(text=teamid) + for index in itertools.islice(history, (len(history)-10 if len(history)-10 > 0 else 0) , len(history)): + if history[index]["away_team_id"] != teamid: + awayteamid = history[index]["away_team_id"] + otherscore = history[index]["away_score"] + ourscore = history[index]["home_score"] + else: + awayteamid = history[index]["home_team_id"] + otherscore = history[index]["home_score"] + ourscore = history[index]["away_score"] + tempdata = requests.get(f"https://mmolb.com/api/team/{awayteamid}").json() + embed.add_field(name=f"vs. {tempdata["Location"]} {tempdata["Name"]} {tempdata["Emoji"]} ({ourscore} - {otherscore})", value=f"[watch]()", inline=False) + await interaction.edit_original_message(embed=embed) + @nextcord.slash_command( name="teamstats",