feat: autocomplete

This commit is contained in:
insert 2025-04-28 15:59:15 -04:00
parent 6870f7ccd3
commit 6b244f2fab
Signed by: insert
GPG key ID: A70775C389ACF105
3 changed files with 89 additions and 57 deletions

2
bot.py
View file

@ -36,9 +36,9 @@ async def on_ready():
await db.commit()
bot.db = db
bot.cur = cur
bot.load_extension('cogs.team') #Must load first as it contains autofill code
bot.load_extension('cogs.liveupdate')
bot.load_extension('cogs.error')
bot.load_extension('cogs.team')
bot.add_all_application_commands()
await bot.sync_all_application_commands()

View file

@ -92,8 +92,11 @@ class liveupdate(commands.Cog):
name="team",
description="Subscribe to every game for the specified team, sending them in this channel",
)
async def teamsubscribe(self, interaction: nextcord.Interaction, teamid: str):
async def teamsubscribe(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]
game = requests.get(f"https://mmolb.com/api/game-by-team/{teamid}").json()
gameid = game["game_id"]
data = requests.get(f"https://mmolb.com/api/game/{gameid}").json()
@ -111,6 +114,16 @@ class liveupdate(commands.Cog):
await self.bot.db.commit()
await interaction.followup.send(content="From now on every game for the specified team will be sent in this channel", ephemeral=True)
@teamsubscribe.on_autocomplete("team")
async def teamsubscribeac(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(
@ -168,12 +181,26 @@ class liveupdate(commands.Cog):
name="team",
description="Unsubscribe to team game updates in this channel",
)
async def unsubscribeteamgame(self, interaction: nextcord.Interaction, teamid: str):
async def unsubscribeteamgame(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 teamsubscriptions WHERE channelid = ? AND teamid = ?
""",(interaction.channel_id,teamid))
await self.bot.db.commit()
await interaction.response.send_message("If existent, it has been removed")
await interaction.edit_original_message(content="If existent, it has been removed")
@unsubscribeteamgame.on_autocomplete("team")
async def unsubscribeteamgameac(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(
name="liveupdatesdelete",

View file

@ -7,9 +7,21 @@ import re
import asyncio
import nextcord
import itertools
from nextcord.ext import commands, application_checks
from nextcord.ext import commands, application_checks, tasks
from nextcord import TextInputStyle, IntegrationType
teams_dict = {}
teams_list = []
def get_all_teams():
data = requests.get("https://freecashe.ws/api/allteams").json()
teams_dict.clear()
teams_list.clear()
for index in data:
teams_list.append(f"{data[index]["Location"]} {data[index]["Name"]}")
teams_dict.update({f"{data[index]["Location"]} {data[index]["Name"]}": index})
print(teams_dict)
print(teams_list)
#all of this code is by evilscientist3, full credit to them
HTTP_CACHE_DIR = Path("http_cache")
@ -224,11 +236,15 @@ class team(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.updateallteams.start()
def cog_unload(self):
self.updateallteams.cancel()
@nextcord.slash_command(
name="greaterteam",
description="Get information about a greater leauge team",
name="team",
description="Get information about a team",
integration_types=[
IntegrationType.user_install,
IntegrationType.guild_install,
@ -240,29 +256,14 @@ class team(commands.Cog):
],
force_global=True,
)
async def greaterteam(self, interaction: nextcord.Interaction, teamid: str = nextcord.SlashOption(
async def greaterteam(self, interaction: nextcord.Interaction, team: str = nextcord.SlashOption(
name = "team",
description = "The greater leauge team",
choices = {
"Washington Baseball Team": "6805db0cac48194de3cd3ff7",
"St. Louis Archers": "6805db0cac48194de3cd400a",
"Anaheim Angles": "6805db0cac48194de3cd401d",
"Brooklyn Scooter Dodgers": "6805db0cac48194de3cd4030",
"Toronto Northern Lights": "6805db0cac48194de3cd4043",
"Durhamshire Badgers": "6805db0cac48194de3cd4056",
"Kansas City Stormchasers": "6805db0cac48194de3cd4069",
"Baltimore Lady Beetles": "6805db0cac48194de3cd407c",
"Boston Street Sweepers": "6805db0cac48194de3cd408f",
"Seattle Shine" : "6805db0cac48194de3cd40a2",
"Chicago Seers": "6805db0cac48194de3cd40b5",
"Roswell Weather Balloons": "6805db0cac48194de3cd40c8",
"Philadelphia Phantasms": "6805db0cac48194de3cd40db",
"Atlanta Tree Frogs": "6805db0cac48194de3cd40ee",
"Miami Merfolk": "6805db0cac48194de3cd4101",
"Dallas Instruments": "6805db0cac48194de3cd4114",
}
description = "The team",
)):
if team not in teams_dict:
await interaction.response.send_message("Invalid Team!", ephemeral=True)
await interaction.response.defer()
teamid = teams_dict[team]
data = requests.get(f"https://mmolb.com/api/team/{teamid}").json()
color = tuple(int(data["Color"][i:i+2], 16) for i in (0, 2, 4))
embed = nextcord.Embed(title=f"{data["Location"]} {data["Name"]} {data["Emoji"]}", description=f"{data["Motto"]}", colour = nextcord.Color.from_rgb(color[0], color[1], color[2]))
@ -274,35 +275,15 @@ class team(commands.Cog):
embed.set_footer(text=teamid)
await interaction.edit_original_message(embed=embed,view=TeamView())
@nextcord.slash_command(
name="lesserteam",
description="Get information about a lesser leauge team",
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 lesserteam(self, interaction: nextcord.Interaction, teamid: str = nextcord.SlashOption(
name = "team",
description = "The lesser leauge team"
)):
await interaction.response.defer()
data = requests.get(f"https://mmolb.com/api/team/{teamid}").json()
color = tuple(int(data["Color"][i:i+2], 16) for i in (0, 2, 4))
embed = nextcord.Embed(title=f"{data["Location"]} {data["Name"]} {data["Emoji"]}", colour = nextcord.Color.from_rgb(color[0], color[1], color[2]))
embed.set_footer(text=teamid)
embed.add_field(name="Wins", value=f"{data["Record"]["Regular Season"]["Wins"]}", inline=True)
embed.add_field(name="Losses", value=f"{data["Record"]["Regular Season"]["Losses"]}", inline=True)
embed.add_field(name="Run Differential", value=f"{data["Record"]["Regular Season"]["RunDifferential"]}", inline=True)
embed.add_field(name="Augments", value=f"{data["Augments"]}", inline=True)
embed.add_field(name="Championships", value=f"{data["Championships"]}", inline=True)
await interaction.edit_original_message(embed=embed,view=TeamView())
@greaterteam.on_autocomplete("team")
async def greaterteamac(self, interaction: nextcord.Interaction, team: str):
if not team:
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)
@nextcord.slash_command(
name="gamehistory",
@ -318,11 +299,14 @@ class team(commands.Cog):
],
force_global=True,
)
async def gamehistory(self, interaction: nextcord.Interaction, teamid: str = nextcord.SlashOption(
async def gamehistory(self, interaction: nextcord.Interaction, team: str = nextcord.SlashOption(
name = "team",
description = "The team"
)):
if team not in teams_dict:
await interaction.response.send_message("Invalid Team!", ephemeral=True)
await interaction.response.defer()
teamid = teams_dict[team]
data = requests.get(f"https://mmolb.com/api/team/{teamid}").json()
history = requests.get(f"https://lunanova.space/mmolb/api/gamesbyteam/{teamid}").json()
color = tuple(int(data["Color"][i:i+2], 16) for i in (0, 2, 4))
@ -341,6 +325,17 @@ class team(commands.Cog):
embed.add_field(name=f"vs. {tempdata["Location"]} {tempdata["Name"]} {tempdata["Emoji"]} ({ourscore} - {otherscore})", value=f"[watch](<https://mmolb.com/watch/{index}>)", inline=False)
await interaction.edit_original_message(embed=embed)
@gamehistory.on_autocomplete("team")
async def gamehistoryac(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)
@nextcord.slash_command(
name="teamstats",
@ -365,5 +360,15 @@ class team(commands.Cog):
splistats.pop(0)
for i in splistats:
await interaction.followup.send(f"```ansi\n{i}```")
@tasks.loop(hours=1)
async def updateallteams(self):
print("Updating teams autocomplete")
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, get_all_teams)
self.bot.teams_list = teams_list
self.bot.teams_dict = teams_dict
def setup(bot: commands.Bot):
bot.add_cog(team(bot))