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(): async def on_ready():
global db global db
global cur global cur
db = await sqlite3.connect("/data/mmolb.db") db = await sqlite3.connect("/data/mmolb.sqlite")
cur = await db.cursor() cur = await db.cursor()
res = await cur.execute("SELECT name FROM sqlite_master WHERE name='liveupdate'") res = await cur.execute("SELECT name FROM sqlite_master WHERE name='liveupdate'")
if await res.fetchone() is None: 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() await db.commit()
bot.db = db bot.db = db
bot.cur = cur bot.cur = cur

View file

@ -14,26 +14,32 @@ class liveupdate(commands.Cog):
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
self.bot = bot self.bot = bot
self.updatelivegames.start()
def cog_unload(self):
self.updatelivegames.cancel()
@nextcord.slash_command( @nextcord.slash_command(
name="liveupdates", name="spotlightgame",
description="Get live updates on a game", description="Watch the spotlight game",
integration_types=[ integration_types=[
IntegrationType.user_install,
IntegrationType.guild_install, IntegrationType.guild_install,
], ],
contexts=[ contexts=[
nextcord.InteractionContextType.guild, nextcord.InteractionContextType.guild,
nextcord.InteractionContextType.bot_dm,
nextcord.InteractionContextType.private_channel, nextcord.InteractionContextType.private_channel,
], ],
force_global=True, 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() 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() message = await interaction.original_message()
if data["State"] == "Complete":
return
await self.bot.cur.execute(f""" await self.bot.cur.execute(f"""
INSERT INTO liveupdate VALUES INSERT INTO liveupdate VALUES
({interaction.guild_id}, {interaction.user.id}, {interaction.channel_id}, {message.id}, "{gameid}", {len(data["EventLog"])}) ({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( @nextcord.slash_command(
name="liveupdatesdelete", name="liveupdates",
description="Delete a subscribed update", description="Get live updates on a game",
integration_types=[ integration_types=[
IntegrationType.user_install,
IntegrationType.guild_install, IntegrationType.guild_install,
], ],
contexts=[ contexts=[
nextcord.InteractionContextType.guild, 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, nextcord.InteractionContextType.private_channel,
], ],
force_global=True, force_global=True,
@ -65,17 +95,23 @@ class liveupdate(commands.Cog):
@tasks.loop(seconds=10.0) @tasks.loop(seconds=10.0)
async def updatelivegames(self): 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 self.bot.cur.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate")
res = await res.fetchall() res = await res.fetchall()
for [serverid,userid,channelid,messageid,gameid,offset] in res: for [serverid,userid,channelid,messageid,gameid,offset] in res:
try:
channel = self.bot.get_channel(channelid) channel = self.bot.get_channel(channelid)
message = await channel.fetch_message(messageid) message = await channel.fetch_message(messageid)
data = requests.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset}").json() data = requests.get(f"https://mmolb.com/api/game/{gameid}/live?after={offset}").json()
if len(data["entries"]) > 0: if len(data["entries"]) > 0:
splitstr = message.content.split("\n") splitstr = message.content.split("\n")
if len(splitstr) > 0:
splitstr.pop(0) splitstr.pop(0)
try:
while len(splitstr)>(5-len(data["entries"])): while len(splitstr)>(5-len(data["entries"])):
splitstr.pop(0) 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) print(splitstr)
basedata = requests.get(f"https://mmolb.com/api/game/{gameid}").json() 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"]}**" finalstr = f"{basedata["AwayTeamName"]} {basedata["AwayTeamEmoji"]} **{data["entries"][-1]["away_score"]}** vs {basedata["HomeTeamName"]} {basedata["HomeTeamEmoji"]} **{data["entries"][-1]["home_score"]}**"
@ -93,6 +129,12 @@ class liveupdate(commands.Cog):
""") """)
await self.bot.db.commit() await self.bot.db.commit()
await message.edit(finalstr) 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): def setup(bot: commands.Bot):
bot.add_cog(liveupdate(bot)) bot.add_cog(liveupdate(bot))

View file

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