Remove liveupdates from user install, sort teams properly

This commit is contained in:
insert 2025-04-25 16:31:19 -04:00
parent dc18eb5b8c
commit bdfdf0e43f
Signed by: insert
GPG key ID: A70775C389ACF105
3 changed files with 89 additions and 47 deletions

4
bot.py
View file

@ -20,11 +20,11 @@ bot = commands.Bot()
async def on_ready():
global db
global cur
db = await sqlite3.connect("/data/mmolb.db")
db = await sqlite3.connect("/data/mmolb.sqlite")
cur = await db.cursor()
res = await cur.execute("SELECT name FROM sqlite_master WHERE name='liveupdate'")
if await res.fetchone() is None:
await cur.execute("CREATE TABLE liveupdate(serverid INTEGER, userid INTEGER, channelid INTEGER, messageid INTEGER, gameid TEXT, offset INTEGER)")
await cur.execute("CREATE TABLE liveupdate(serverid INTEGER, userid INTEGER, channelid INTEGER NOT NULL, messageid INTEGER, gameid TEXT, offset INTEGER)")
await db.commit()
bot.db = db
bot.cur = cur

View file

@ -14,26 +14,32 @@ class liveupdate(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.updatelivegames.start()
def cog_unload(self):
self.updatelivegames.cancel()
@nextcord.slash_command(
name="liveupdates",
description="Get live updates on a game",
name="spotlightgame",
description="Watch the spotlight game",
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 liveupdatecreate(self, interaction: nextcord.Interaction, gameid: str):
async def spotlightwatch(self, interaction: nextcord.Interaction):
await interaction.response.defer()
game = requests.get("https://mmolb.com/api/spotlight").json()
gameid = game["game_id"]
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
await interaction.response.send_message(f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
await interaction.edit_original_message(content=f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
message = await interaction.original_message()
if data["State"] == "Complete":
return
await self.bot.cur.execute(f"""
INSERT INTO liveupdate VALUES
({interaction.guild_id}, {interaction.user.id}, {interaction.channel_id}, {message.id}, "{gameid}", {len(data["EventLog"])})
@ -43,15 +49,39 @@ class liveupdate(commands.Cog):
@nextcord.slash_command(
name="liveupdatesdelete",
description="Delete a subscribed update",
name="liveupdates",
description="Get live updates on a game",
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 liveupdatecreate(self, interaction: nextcord.Interaction, gameid: str):
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
await interaction.response.send_message(f"{data["AwayTeamName"]} {data["AwayTeamEmoji"]} **{data["EventLog"][-1]["away_score"]}** vs {data["HomeTeamName"]} {data["HomeTeamEmoji"]} **{data["EventLog"][-1]["home_score"]}**")
message = await interaction.original_message()
if data["State"] == "Complete":
return
await self.bot.cur.execute(f"""
INSERT INTO liveupdate VALUES
({interaction.guild_id}, {interaction.user.id}, {interaction.channel_id}, {message.id}, "{gameid}", {len(data["EventLog"])})
""")
await self.bot.db.commit()
@nextcord.slash_command(
name="liveupdatesdelete",
description="Delete a subscribed update",
integration_types=[
IntegrationType.guild_install,
],
contexts=[
nextcord.InteractionContextType.guild,
nextcord.InteractionContextType.private_channel,
],
force_global=True,
@ -65,34 +95,46 @@ class liveupdate(commands.Cog):
@tasks.loop(seconds=10.0)
async def updatelivegames(self):
await self.bot.wait_until_ready()
res = await self.bot.cur.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate")
res = await res.fetchall()
for [serverid,userid,channelid,messageid,gameid,offset] in res:
channel = self.bot.get_channel(channelid)
message = await channel.fetch_message(messageid)
data = requests.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset}").json()
if len(data["entries"]) > 0:
splitstr = message.content.split("\n")
splitstr.pop(0)
while len(splitstr)>(5-len(data["entries"])):
splitstr.pop(0)
print(splitstr)
basedata = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
finalstr = f"{basedata["AwayTeamName"]} {basedata["AwayTeamEmoji"]} **{data["entries"][-1]["away_score"]}** vs {basedata["HomeTeamName"]} {basedata["HomeTeamEmoji"]} **{data["entries"][-1]["home_score"]}**"
for i in splitstr:
finalstr += f"\n{i}"
for i in data["entries"]:
finalstr += f"\n{i['message']}"
await self.bot.cur.execute(f"""
UPDATE liveupdate set offset = {offset+1} WHERE messageid = '{messageid}'
""") #Could do this for every meessage subscribed to the game but since the messages go one by one... maybe I should change that
await self.bot.db.commit()
if i["event"] == "Recordkeeping":
try:
channel = self.bot.get_channel(channelid)
message = await channel.fetch_message(messageid)
data = requests.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset}").json()
if len(data["entries"]) > 0:
splitstr = message.content.split("\n")
if len(splitstr) > 0:
splitstr.pop(0)
try:
while len(splitstr)>(5-len(data["entries"])):
splitstr.pop(0)
except IndexError:
print("Warning: index error, ignoring") #Not sure why this happens so far but ignorning the error doesn't break anything
print(splitstr)
basedata = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
finalstr = f"{basedata["AwayTeamName"]} {basedata["AwayTeamEmoji"]} **{data["entries"][-1]["away_score"]}** vs {basedata["HomeTeamName"]} {basedata["HomeTeamEmoji"]} **{data["entries"][-1]["home_score"]}**"
for i in splitstr:
finalstr += f"\n{i}"
for i in data["entries"]:
finalstr += f"\n{i['message']}"
await self.bot.cur.execute(f"""
DELETE from liveupdate WHERE messageid = {messageid}
""")
UPDATE liveupdate set offset = {offset+1} WHERE messageid = '{messageid}'
""") #Could do this for every meessage subscribed to the game but since the messages go one by one... maybe I should change that
await self.bot.db.commit()
await message.edit(finalstr)
if i["event"] == "Recordkeeping":
await self.bot.cur.execute(f"""
DELETE from liveupdate WHERE messageid = {messageid}
""")
await self.bot.db.commit()
await message.edit(finalstr)
except Exception as e:
await self.bot.cur.execute(f"""
DELETE from liveupdate WHERE messageid = {messageid}
""")
await self.bot.db.commit()
await message.edit(f"An error occured in this live update\n{e}")
def setup(bot: commands.Bot):
bot.add_cog(liveupdate(bot))

View file

@ -217,22 +217,22 @@ class team(commands.Cog):
name = "team",
description = "The greater leauge team",
choices = {
"Seattle Shine" : "6805db0cac48194de3cd40a2",
"Chicago Seers": "6805db0cac48194de3cd40b5",
"Atlanta Tree Frogs": "6805db0cac48194de3cd40ee",
"Baltimore Lady Beetles": "6805db0cac48194de3cd407c",
"Washington Baseball Team": "6805db0cac48194de3cd3ff7",
"St. Louis Archers": "6805db0cac48194de3cd400a",
"Anaheim Angles": "6805db0cac48194de3cd401d",
"Miami Merfolk": "6805db0cac48194de3cd4101",
"Washington Baseball Team": "6805db0cac48194de3cd3ff7",
"Dallas Instruments": "6805db0cac48194de3cd4114",
"Roswell Weather Balloons": "6805db0cac48194de3cd40c8",
"Brooklyn Scooter Dodgers": "6805db0cac48194de3cd4030",
"Toronto Northern Lights": "6805db0cac48194de3cd4043",
"Kansas City Stormchasers": "6805db0cac48194de3cd4069",
"Philadelphia Phantasms": "6805db0cac48194de3cd40db",
"Durhamshire Badgers": "6805db0cac48194de3cd4056",
"Kansas City Stormchasers": "6805db0cac48194de3cd4069",
"Baltimore Lady Beetles": "6805db0cac48194de3cd407c",
"Boston Street Sweepers": "6805db0cac48194de3cd408f",
"Brooklyn Scooter Dodgers": "6805db0cac48194de3cd4030"
"Seattle Shine" : "6805db0cac48194de3cd40a2",
"Chicago Seers": "6805db0cac48194de3cd40b5",
"Roswell Weather Balloons": "6805db0cac48194de3cd40c8",
"Philadelphia Phantasms": "6805db0cac48194de3cd40db",
"Atlanta Tree Frogs": "6805db0cac48194de3cd40ee",
"Miami Merfolk": "6805db0cac48194de3cd4101",
"Dallas Instruments": "6805db0cac48194de3cd4114",
}
)):
await interaction.response.defer()