Compare commits

..

No commits in common. "3d5f2e3de433d100cdbf9993455ad036e76765dc" and "9e52347be386cc919b483ab14353773d9821bcd6" have entirely different histories.

3 changed files with 3 additions and 175 deletions

5
bot.py
View file

@ -21,7 +21,6 @@ class Bot(commands.Bot):
await cur.execute("DELETE from teamsubscriptions WHERE serverid = ?", (guild.id,)) await cur.execute("DELETE from teamsubscriptions WHERE serverid = ?", (guild.id,))
await cur.execute("DELETE from liveupdate WHERE serverid = ?", (guild.id,)) await cur.execute("DELETE from liveupdate WHERE serverid = ?", (guild.id,))
await cur.execute("DELETE from spotlightsubscriptions WHERE serverid = ?", (guild.id,)) await cur.execute("DELETE from spotlightsubscriptions WHERE serverid = ?", (guild.id,))
await cur.execute("DELETE from feedsubscriptions WHERE serverid = ?", (guild.id,))
await db.commit() await db.commit()
load_dotenv() load_dotenv()
@ -50,10 +49,6 @@ async def on_ready():
if await res.fetchone() is None: if await res.fetchone() is None:
await cur.execute("CREATE TABLE teamsubscriptions(serverid INTEGER, channelid INTEGER NOT NULL, teamid TEXT, classic INTEGER NOT NULL)") await cur.execute("CREATE TABLE teamsubscriptions(serverid INTEGER, channelid INTEGER NOT NULL, teamid TEXT, classic INTEGER NOT NULL)")
await db.commit() await db.commit()
res = await cur.execute("SELECT name FROM sqlite_master WHERE name='feedsubscriptions'")
if await res.fetchone() is None:
await cur.execute("CREATE TABLE feedsubscriptions(serverid INTEGER, channelid INTEGER NOT NULL, teamid TEXT, offset INTEGER)")
await db.commit()
#await cur.execute("ALTER TABLE liveupdate ADD classic INTEGER NOT NULL DEFAULT 0") #await cur.execute("ALTER TABLE liveupdate ADD classic INTEGER NOT NULL DEFAULT 0")
#await cur.execute("ALTER TABLE spotlightsubscriptions ADD classic INTEGER NOT NULL DEFAULT 0") #await cur.execute("ALTER TABLE spotlightsubscriptions ADD classic INTEGER NOT NULL DEFAULT 0")
#await cur.execute("ALTER TABLE teamsubscriptions ADD classic INTEGER NOT NULL DEFAULT 0") #await cur.execute("ALTER TABLE teamsubscriptions ADD classic INTEGER NOT NULL DEFAULT 0")

View file

@ -6,7 +6,6 @@ import requests
import asyncio import asyncio
import nextcord import nextcord
import aiohttp import aiohttp
import itertools
import aiosqlite as sqlite3 import aiosqlite as sqlite3
from nextcord.ext import commands, application_checks, tasks from nextcord.ext import commands, application_checks, tasks
from nextcord import TextInputStyle, IntegrationType from nextcord import TextInputStyle, IntegrationType
@ -192,8 +191,6 @@ async def teamsubscriptionworker(self,serverid,channelid,teamid,classic):
data = await session.get(f"https://mmolb.com/api/game/{gameid}") data = await session.get(f"https://mmolb.com/api/game/{gameid}")
data = await data.json() data = await data.json()
channel = self.bot.get_channel(channelid) channel = self.bot.get_channel(channelid)
if channel is None:
return
if data["State"] == "Complete": if data["State"] == "Complete":
return return
check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ? AND classic = ?", (channelid,gameid,classic)) check = await self.bot.db.execute("SELECT serverid,userid,channelid,messageid,gameid,offset FROM liveupdate WHERE channelid = ? AND gameid = ? AND classic = ?", (channelid,gameid,classic))
@ -224,36 +221,6 @@ async def teamsubscriptionworker(self,serverid,channelid,teamid,classic):
print(e) print(e)
return return
async def feedsubscriptionworker(self,serverid,channelid,teamid,offset):
begin = timeit.default_timer()
lastserverid = serverid
try:
async with aiohttp.ClientSession() as session:
channel = self.bot.get_channel(channelid)
if channel is None:
return
data = await session.get(f"https://mmolb.com/api/team/{teamid}")
data = await data.json()
data = data['Feed']
if len(data) > offset:
finalstr = ""
for index in itertools.islice(data, offset, len(data)):
finalstr += f"\n{index['emoji']} Season {index['season']} Day {index['day']}: {index['text']}"
await channel.send(finalstr)
await self.bot.db.execute(f"""
UPDATE feedsubscriptions set offset = {len(data)} WHERE channelid = '{channelid}' AND teamid = '{teamid}'
""")
except Exception as e:
warning = self.bot.get_channel(1365478368555827270)
await warning.send(e)
except nextcord.Forbidden:
warning = self.bot.get_channel(1365478368555827270)
await self.bot.db.execute("DELETE from teamsubscriptions WHERE serverid = ?", (lastserverid,))
await self.bot.db.execute("DELETE from liveupdate WHERE serverid = ?", (lastserverid,))
await self.bot.db.execute("DELETE from spotlightsubscriptions WHERE serverid = ?", (lastserverid,))
await self.bot.db.execute("DELETE from feedsubscriptions WHERE serverid = ?", (lastserverid,))
await self.bot.db.commit()
await warning.send(f"Deleted {lastserverid} from the database due to 403 error")
class liveupdate(commands.Cog): class liveupdate(commands.Cog):
@ -261,7 +228,6 @@ 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() self.updatelivegames.start()
self.checkfeedsubscriptions.start()
self.checkspotlightsubscriptions.start() self.checkspotlightsubscriptions.start()
self.checkteamsubscriptions.start() self.checkteamsubscriptions.start()
@ -269,7 +235,6 @@ class liveupdate(commands.Cog):
self.updatelivegames.cancel() self.updatelivegames.cancel()
self.checkspotlightsubscriptions.cancel() self.checkspotlightsubscriptions.cancel()
self.checkteamsubscriptions.cancel() self.checkteamsubscriptions.cancel()
self.checkfeedsubscriptions.cancel()
@nextcord.slash_command( @nextcord.slash_command(
name="spotlightgame", name="spotlightgame",
@ -425,36 +390,6 @@ class liveupdate(commands.Cog):
thanksdiscord = closestteam[:20] thanksdiscord = closestteam[:20]
await interaction.response.send_autocomplete(thanksdiscord) await interaction.response.send_autocomplete(thanksdiscord)
@subscribe.subcommand(
name="feed",
description="Subscribe to a team's feed",
)
async def feedsubscribe(self, interaction: nextcord.Interaction, team: str):
if team not in self.bot.teams_dict:
await interaction.response.send_message("Invalid Team!", ephemeral=True)
await interaction.response.defer()
teamid = self.bot.teams_dict[team]
async with aiohttp.ClientSession() as session:
data = await session.get(f"https://mmolb.com/api/team/{teamid}")
data = await data.json()
offset = len(data['Feed'])
await self.bot.db.execute(f"""
INSERT INTO feedsubscriptions VALUES
({interaction.guild_id}, {interaction.channel_id}, "{teamid}", {offset})
""")
await self.bot.db.commit()
await interaction.edit_original_message(content="This channel is now subscribed to feed updates")
@feedsubscribe.on_autocomplete("team")
async def feedsubscribeac(self, interaction: nextcord.Interaction, team: str):
if not team:
thanksdiscord = self.bot.teams_list[:20]
await interaction.response.send_autocomplete(thanksdiscord)
return
closestteam = [name for name in self.bot.teams_list if name.lower().startswith(team.lower())]
thanksdiscord = closestteam[:20]
await interaction.response.send_autocomplete(thanksdiscord)
@nextcord.slash_command( @nextcord.slash_command(
name="watch", name="watch",
description="Get live updates on a game", description="Get live updates on a game",
@ -539,32 +474,6 @@ class liveupdate(commands.Cog):
thanksdiscord = closestteam[:20] thanksdiscord = closestteam[:20]
await interaction.response.send_autocomplete(thanksdiscord) await interaction.response.send_autocomplete(thanksdiscord)
@unsubscribe.subcommand(
name="feed",
description="Unsubscribe to team feed updates in this channel",
)
async def unsubscribefeed(self, interaction: nextcord.Interaction, team: str):
if team not in self.bot.teams_dict:
await interaction.response.send_message("Invalid Team!", ephemeral=True)
await interaction.response.defer()
teamid = self.bot.teams_dict[team]
await self.bot.db.execute(f"""
DELETE from feedsubscriptions WHERE channelid = ? AND teamid = ?
""",(interaction.channel_id,teamid))
await self.bot.db.commit()
await interaction.edit_original_message(content="If existent, it has been removed")
@unsubscribefeed.on_autocomplete("team")
async def unsubscribefeedac(self, interaction: nextcord.Interaction, team: str):
if not team:
thanksdiscord = self.bot.teams_list[:20]
await interaction.response.send_autocomplete(thanksdiscord)
return
closestteam = [name for name in self.bot.teams_list if name.lower().startswith(team.lower())]
thanksdiscord = closestteam[:20]
await interaction.response.send_autocomplete(thanksdiscord)
@nextcord.slash_command( @nextcord.slash_command(
name="liveupdatesdelete", name="liveupdatesdelete",
description="DEBUG: Delete a subscribed update", description="DEBUG: Delete a subscribed update",
@ -668,22 +577,6 @@ class liveupdate(commands.Cog):
await warning.send(e) await warning.send(e)
print(e) print(e)
return return
@tasks.loop(seconds=120.0)
async def checkfeedsubscriptions(self):
try:
await self.bot.wait_until_ready()
res = await self.bot.db.execute("SELECT serverid,channelid,teamid,offset FROM feedsubscriptions")
res = await res.fetchall()
worklist = [feedsubscriptionworker(self,serverid,channelid,teamid,offset) for [serverid,channelid,teamid,offset] in res]
await asyncio.gather(*worklist,return_exceptions=True)
await self.bot.db.commit()
except Exception as e:
#I know this is bad practice but these loops must be running
warning = self.bot.get_channel(1365478368555827270)
await warning.send(e)
print(e)
return
def setup(bot: commands.Bot): def setup(bot: commands.Bot):
bot.add_cog(liveupdate(bot)) bot.add_cog(liveupdate(bot))

View file

@ -7,7 +7,6 @@ import re
import asyncio import asyncio
import nextcord import nextcord
import itertools import itertools
import aiohttp
from nextcord.ext import commands, application_checks, tasks from nextcord.ext import commands, application_checks, tasks
from nextcord import TextInputStyle, IntegrationType from nextcord import TextInputStyle, IntegrationType
@ -212,7 +211,7 @@ class TeamView(nextcord.ui.View):
label="Game History", style=nextcord.ButtonStyle.green, custom_id="team:gamehistory" label="Game History", style=nextcord.ButtonStyle.green, custom_id="team:gamehistory"
) )
async def gamehistorybutton(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): async def gamehistorybutton(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
await interaction.response.defer(ephemeral=True) await interaction.response.defer()
ogmsg = interaction.message.embeds ogmsg = interaction.message.embeds
embed = ogmsg[0] embed = ogmsg[0]
teamid = embed.footer.text teamid = embed.footer.text
@ -234,25 +233,6 @@ class TeamView(nextcord.ui.View):
embed.add_field(name=f"vs. {tempdata["Location"]} {tempdata["Name"]} {tempdata["Emoji"]} ({ourscore} - {otherscore})", value=f"[watch](<https://mmolb.com/watch/{index['game_id']}>)", inline=False) embed.add_field(name=f"vs. {tempdata["Location"]} {tempdata["Name"]} {tempdata["Emoji"]} ({ourscore} - {otherscore})", value=f"[watch](<https://mmolb.com/watch/{index['game_id']}>)", inline=False)
await interaction.followup.send(embed=embed,ephemeral=True) await interaction.followup.send(embed=embed,ephemeral=True)
@nextcord.ui.button(
label="Feed", style=nextcord.ButtonStyle.green, custom_id="team:feed"
)
async def feedbutton(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
await interaction.response.defer(ephemeral=True)
ogmsg = interaction.message.embeds
embed = ogmsg[0]
teamid = embed.footer.text
async with aiohttp.ClientSession() as session:
data = await session.get(f"https://mmolb.com/api/team/{teamid}")
data = await data.json()
color = tuple(int(data["Color"][i:i+2], 16) for i in (0, 2, 4))
embed = nextcord.Embed(title=f"Last ten feed events for the {data["Location"]} {data["Name"]} {data["Emoji"]}", colour = nextcord.Color.from_rgb(color[0], color[1], color[2]))
embed.set_footer(text=teamid)
data = data['Feed']
for index in itertools.islice(data, (len(data)-10 if len(data)-10 > 0 else 0) , len(data)):
embed.add_field(name=f"{index['emoji']} Day {index['day']} Season {index['season']}", value=index['text'], inline=False)
await interaction.followup.send(embed=embed,ephemeral=True)
class team(commands.Cog): class team(commands.Cog):
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
@ -339,7 +319,7 @@ class team(commands.Cog):
await interaction.response.defer() await interaction.response.defer()
teamid = teams_dict[team] teamid = teams_dict[team]
data = requests.get(f"https://mmolb.com/api/team/{teamid}").json() data = requests.get(f"https://mmolb.com/api/team/{teamid}").json()
history = requests.get(f"https://freecashe.ws/api/games?team={teamid}&season=3").json()["items"] history = requests.get(f"https://freecashe.ws/api/games?team={teamid}&season=2").json()["items"]
color = tuple(int(data["Color"][i:i+2], 16) for i in (0, 2, 4)) 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 = 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) embed.set_footer(text=teamid)
@ -407,47 +387,7 @@ class team(commands.Cog):
thanksdiscord = closestteam[:20] thanksdiscord = closestteam[:20]
await interaction.response.send_autocomplete(thanksdiscord) await interaction.response.send_autocomplete(thanksdiscord)
@nextcord.slash_command(
name="feed",
description="Get the latest entry from the team's feed",
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 teamfeed(self, interaction: nextcord.Interaction, team: str):
if team not in teams_dict:
await interaction.response.send_message("Invalid Team!", ephemeral=True)
return
await interaction.response.defer()
teamid = teams_dict[team]
async with aiohttp.ClientSession() as session:
data = await session.get(f"https://mmolb.com/api/team/{teamid}")
data = await data.json()
color = tuple(int(data["Color"][i:i+2], 16) for i in (0, 2, 4))
embed = nextcord.Embed(title=f"Last ten feed events for the {data["Location"]} {data["Name"]} {data["Emoji"]}", colour = nextcord.Color.from_rgb(color[0], color[1], color[2]))
embed.set_footer(text=teamid)
data = data['Feed']
for index in itertools.islice(data, (len(data)-10 if len(data)-10 > 0 else 0) , len(data)):
embed.add_field(name=f"{index['emoji']} Day {index['day']} Season {index['season']}", value=index['text'], inline=False)
await interaction.edit_original_message(embed=embed)
@teamfeed.on_autocomplete("team")
async def feedac(self, interaction: nextcord.Interaction, team: str):
if not team:
print("we're here")
thanksdiscord = teams_list[:20]
await interaction.response.send_autocomplete(thanksdiscord)
return
closestteam = [name for name in teams_list if name.lower().startswith(team.lower())]
thanksdiscord = closestteam[:20]
await interaction.response.send_autocomplete(thanksdiscord)
@tasks.loop(hours=1) @tasks.loop(hours=1)
async def updateallteams(self): async def updateallteams(self):