From 15c5c2c50a522b22d45f7479c48e142cf8d81d76 Mon Sep 17 00:00:00 2001 From: insert Date: Sat, 28 Jun 2025 12:38:52 -0400 Subject: [PATCH] fix: handle stale live updates --- bot.py | 20 ++++++++++++++++++++ cogs/liveupdate.py | 20 +++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/bot.py b/bot.py index d7bb530..5765105 100644 --- a/bot.py +++ b/bot.py @@ -102,4 +102,24 @@ cog: str = SlashOption(required=False) except Exception as e: await interaction.response.send_message(e, ephemeral=True) + +@bot.slash_command( + name="clearliveupdates", + description="Delete all live updates", + guild_ids=[int(os.getenv("OWNER_GUILD"))], +) +@application_checks.is_owner() +async def delliveupdates(interaction: nextcord.Interaction, +confirm: str = SlashOption(required=False) +): + if confirm == "yes, do as I say!": + await db.execute(f"""DELETE from liveupdate""") + await db.commit() + await interaction.response.send_message("all gone...") + else: + res = await db.execute("SELECT COUNT (*) from liveupdate") + res, = await res.fetchone() + await interaction.response.send_message(f"There are currently {res} live updates") + + bot.run(os.getenv("TOKEN")) \ No newline at end of file diff --git a/cogs/liveupdate.py b/cogs/liveupdate.py index 8446d2a..76b976d 100644 --- a/cogs/liveupdate.py +++ b/cogs/liveupdate.py @@ -21,11 +21,15 @@ async def livegameworker(self,serverid,userid,channelid,messageid,gameid,offset) message = await channel.fetch_message(messageid) data = await session.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset}") data = await data.json() + basedata = await session.get(f"https://mmolb.com/api/game/{gameid}") + basedata = await basedata.json() + if basedata["State"] == "Complete": + await self.bot.db.execute(f""" + DELETE from liveupdate WHERE messageid = {messageid} + """) if len(data["entries"]) > 0: data = await session.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset-((7-len(data["entries"]) if offset > 7 else offset))}") data = await data.json() - basedata = await session.get(f"https://mmolb.com/api/game/{gameid}") - basedata = await basedata.json() finalstr = "" offsetadd = 0 for i in data["entries"]: @@ -67,7 +71,7 @@ async def livegameworker(self,serverid,userid,channelid,messageid,gameid,offset) embed.add_field(name="Batting",value=data["entries"][-1]["batter"], inline=True) embed.add_field(name="Pitching",value=data["entries"][-1]["pitcher"], inline=True) embed.add_field(name="On Deck",value=data["entries"][-1]["on_deck"], inline=True) - embed.add_field(name=f"Last 7 events",value=finalstr,inline=False) + embed.add_field(name=f"Last 7 events",value=finalstr[-1024:],inline=False) embed.set_thumbnail(f"https://insertapp.net/mmolbbot/assets/diamond_{data["entries"][-1]["on_1b"]}_{data["entries"][-1]["on_2b"]}_{data["entries"][-1]["on_3b"]}.png") embed.set_footer(text=f"Embed too big? Need historical data? consider classic mode.") await message.edit(content="",embed=embed) @@ -100,9 +104,13 @@ async def classiclivegameworker(self,serverid,userid,channelid,gameid,offset): channel = self.bot.get_channel(channelid) data = await session.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset}") data = await data.json() + basedata = await session.get(f"https://mmolb.com/api/game/{gameid}") + basedata = await basedata.json() + if basedata["State"] == "Complete": + await self.bot.db.execute(f""" + DELETE from liveupdate WHERE channelid = {channelid} AND gameid = '{gameid}' + """) if len(data["entries"]) > 0: - basedata = await session.get(f"https://mmolb.com/api/game/{gameid}") - basedata = await basedata.json() finalstr = "\n>>> " offsetadd = 0 maysend = False @@ -502,6 +510,8 @@ class liveupdate(commands.Cog): worklist = worklist + [classiclivegameworker(self,serverid,userid,channelid,gameid,offset) for [serverid,userid,channelid,gameid,offset] in res] await asyncio.gather(*worklist,return_exceptions=True) await self.bot.db.commit() + game = nextcord.Game(f"Broadcasting {len(worklist)} games") + await self.bot.change_presence(status=nextcord.Status.online, activity=game) timings.append(timeit.default_timer()-begin) if len(timings) >= 12: finalstr = "Last 12 timings:"