fix: handle stale live updates

This commit is contained in:
insert 2025-06-28 12:38:52 -04:00
parent a6e06e9e98
commit 15c5c2c50a
Signed by: insert
GPG key ID: A70775C389ACF105
2 changed files with 35 additions and 5 deletions

20
bot.py
View file

@ -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"))

View file

@ -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()
if len(data["entries"]) > 0:
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:
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:"