mirror of
https://github.com/insertapp/mmolbbot.git
synced 2025-07-01 06:17:03 +00:00
83 lines
3.4 KiB
Python
83 lines
3.4 KiB
Python
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
from datetime import datetime, timedelta, timezone
|
|
import requests
|
|
import asyncio
|
|
import nextcord
|
|
import aiosqlite as sqlite3
|
|
from nextcord.ext import commands, application_checks, tasks
|
|
from nextcord import TextInputStyle, IntegrationType
|
|
import itertools
|
|
|
|
leagues_dict = {}
|
|
leagues_list = []
|
|
def get_all_leagues():
|
|
data = requests.get("https://freecashe.ws/api/leagues").json()
|
|
leagues_dict.clear()
|
|
leagues_list.clear()
|
|
for index in data["items"]:
|
|
leagues_list.append(f"{index["name"]}")
|
|
leagues_dict.update({f"{index["name"]}": index["league_id"]})
|
|
print(leagues_dict)
|
|
print(leagues_list)
|
|
|
|
class league(commands.Cog):
|
|
def __init__(self, bot: commands.Bot):
|
|
self.bot = bot
|
|
self.updateleagues.start()
|
|
|
|
def cog_unload(self):
|
|
self.updateleagues.cancel()
|
|
|
|
@tasks.loop(hours=24) #Unless there is some sort of story event this most likely won't frequently change
|
|
async def updateleagues(self):
|
|
print("Updating leagues autocomplete")
|
|
loop = asyncio.get_event_loop()
|
|
await loop.run_in_executor(None, get_all_leagues)
|
|
self.bot.leagues_list = leagues_list
|
|
self.bot.leagues_dict = leagues_dict
|
|
|
|
|
|
@nextcord.slash_command(
|
|
name="ranking",
|
|
description="Get a leaguess overall rankings",
|
|
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 leaguesranking(self, interaction: nextcord.Interaction, league: str):
|
|
if league not in leagues_dict:
|
|
await interaction.response.send_message("Invalid league!", ephemeral=True)
|
|
return
|
|
await interaction.response.defer()
|
|
leagueid = leagues_dict[league]
|
|
basedata = requests.get(f"https://mmolb.com/api/league/{leagueid}").json()
|
|
rankdata = requests.get(f"https://mmolb.com/api/league-top-teams/{leagueid}").json()
|
|
color = tuple(int(basedata["Color"][i:i+2], 16) for i in (0, 2, 4))
|
|
embed = nextcord.Embed(title=f"Top teams for the {basedata["Name"]} League {basedata["Emoji"]}", colour = nextcord.Color.from_rgb(color[0], color[1], color[2]))
|
|
rankdata = rankdata["teams"]
|
|
for i in itertools.islice(rankdata, 0, 10):
|
|
embed.add_field(name=f"{i["Location"]} {i["Name"]} {i["Emoji"]} ", value=f"[{i["Record"]["Regular Season"]["Wins"]}-{i["Record"]["Regular Season"]["Losses"]} ({i["Record"]["Regular Season"]["RunDifferential"]})](<https://mmolb.com/team/{i["_id"]}>)",inline=False)
|
|
embed.set_footer(text=leagueid)
|
|
await interaction.edit_original_message(embed=embed)
|
|
|
|
@leaguesranking.on_autocomplete("league")
|
|
async def leaguesrankingac(self, interaction: nextcord.Interaction, league: str):
|
|
if not league:
|
|
thanksdiscord = leagues_list[:20]
|
|
await interaction.response.send_autocomplete(thanksdiscord)
|
|
return
|
|
closestteam = [name for name in leagues_list if name.lower().startswith(league.lower())]
|
|
thanksdiscord = closestteam[:20]
|
|
await interaction.response.send_autocomplete(thanksdiscord)
|
|
|
|
def setup(bot: commands.Bot):
|
|
bot.add_cog(league(bot)) |