Add football slash commands, clean up some ugly stuff

pull/90/head
Stijn De Clercq 2021-09-03 21:18:29 +02:00
parent 0ec321a51b
commit 2e51af6f1c
4 changed files with 66 additions and 31 deletions

View File

@ -2,7 +2,7 @@ from decorators import help
from discord.ext import commands
from enums.help_categories import Category
from functions import checks, config
from functions.football import getMatches, getTable, get_jpl_code
from functions.football import get_matches, get_table, get_jpl_code
class Football(commands.Cog):
@ -20,21 +20,16 @@ class Football(commands.Cog):
pass
@jpl.command(name="Matches", aliases=["M"], usage="[Week]*")
async def matches(self, ctx, *args):
args = list(args)
async def matches(self, ctx, day: int = None):
# Default is current day
if not args:
args = [str(config.get("jpl_day"))]
if day is None:
day = int(config.get("jpl_day"))
if all(letter.isdigit() for letter in args[0]):
await ctx.send(getMatches(int(args[0])))
else:
return await ctx.send("Dit is geen geldige speeldag.")
await ctx.send(get_matches(day))
@jpl.command(name="Table", aliases=["Ranking", "Rankings", "Ranks", "T"])
async def table(self, ctx, *args):
await ctx.send(getTable())
async def table(self, ctx):
await ctx.send(get_table())
@commands.check(checks.isMe)
@jpl.command(name="Update")

View File

@ -0,0 +1,44 @@
from discord.ext import commands
from dislash import SlashInteraction, slash_command, Option, OptionType
from functions import config, checks
from functions.football import get_matches, get_table, get_jpl_code
from startup.didier import Didier
class FootballSlash(commands.Cog):
def __init__(self, client: Didier):
self.client: Didier = client
@slash_command(name="jpl", description="Jupiler Pro League commands")
async def _jpl_group(self, interaction: SlashInteraction):
pass
@_jpl_group.sub_command(name="matches",
description="Schema voor een bepaalde speeldag",
options=[
Option("day", "Speeldag (default huidige)", OptionType.INTEGER)
]
)
async def _jpl_matches_slash(self, interaction: SlashInteraction, day: int = None):
# Default is current day
if day is None:
day = int(config.get("jpl_day"))
await interaction.reply(get_matches(day))
@_jpl_group.sub_command(name="table", description="Huidige rangschikking")
async def _jpl_table_slash(self, interaction: SlashInteraction):
await interaction.reply(get_table())
@_jpl_group.sub_command(name="update", description="Update de code voor deze competitie (owner-only)")
async def _jpl_update_slash(self, interaction: SlashInteraction):
if not await checks.isMe(interaction):
return await interaction.reply(f"Je hebt geen toegang tot dit commando.")
code = get_jpl_code()
config.config("jpl", code)
await interaction.reply(f"Done (code: {code})")
def setup(client: Didier):
client.add_cog(FootballSlash(client))

View File

@ -1,8 +1,4 @@
import math
import discord
from discord import utils, Member, User
from discord.ext import commands
from data import constants
import requests
from functions.database import currency

View File

@ -39,11 +39,11 @@ class Match:
Parse class attributes out of a dictionary returned from an API request
"""
# The API isn't public, so every single game state is differently formatted
self.status = self._getStatus(self.matchDict[Navigation.Status.value])
self.status = self._get_status(self.matchDict[Navigation.Status.value])
self.home = self.matchDict[Navigation.HomeTeam.value][Navigation.Name.value]
self.away = self.matchDict[Navigation.AwayTeam.value][Navigation.Name.value]
if self._hasStarted():
if self._has_started():
self.homeScore = self.matchDict[Navigation.HomeScore.value]
self.awayScore = self.matchDict[Navigation.AwayScore.value]
@ -53,9 +53,9 @@ class Match:
self.start = None
self.date = self.start.strftime("%d/%m") if self.start is not None else "Uitgesteld"
self.weekDay = self._getWeekday() if self.start is not None else "??"
self.weekDay = self._get_weekday() if self.start is not None else "??"
def _getStatus(self, status: str):
def _get_status(self, status: str):
"""
Gets the string representation for the status of this match
"""
@ -80,7 +80,7 @@ class Match:
return statusses[status.lower()]
def _getWeekday(self):
def _get_weekday(self):
"""
Gets the day of the week this match is played on
"""
@ -88,13 +88,13 @@ class Match:
days = ["Ma", "Di", "Wo", "Do", "Vr", "Za", "Zo"]
return days[day]
def getInfo(self):
def get_info(self):
"""
Returns a list of all the info of this class in order to create a table
"""
return [self.weekDay, self.date, self.home, self._getScore(), self.away, self.status]
return [self.weekDay, self.date, self.home, self._get_score(), self.away, self.status]
def _getScore(self):
def _get_score(self):
"""
Returns a string representing the scoreboard
"""
@ -102,12 +102,12 @@ class Match:
return "??"
# No score to show yet, show time when the match starts
if not self._hasStarted():
if not self._has_started():
return "{}:{}".format(leading_zero(str(self.start.hour)), leading_zero(str(self.start.minute)))
return "{} - {}".format(self.homeScore, self.awayScore)
def _hasStarted(self):
def _has_started(self):
return self.status not in [Status.AfterToday.value, Status.NotStarted.value, Status.Postponed.value]
@ -128,7 +128,7 @@ class Navigation(Enum):
Name = "name"
def getMatches(matchweek: int):
def get_matches(matchweek: int):
"""
Function that constructs the list of matches for a given matchweek
"""
@ -139,7 +139,7 @@ def getMatches(matchweek: int):
return "Er ging iets fout. Probeer het later opnieuw."
matches = list(map(Match, current_day))
matches = list(map(lambda x: x.getInfo(), matches))
matches = list(map(lambda x: x.get_info(), matches))
header = "Jupiler Pro League - Speeldag {}".format(matchweek)
table = tabulate.tabulate(matches, headers=["Dag", "Datum", "Thuis", "Stand", "Uit", "Tijd"])
@ -147,7 +147,7 @@ def getMatches(matchweek: int):
return "```{}\n\n{}```".format(header, table)
def getTable():
def get_table():
"""
Function that constructs the current table of the JPL
"""
@ -157,7 +157,7 @@ def getTable():
return "Er ging iets fout. Probeer het later opnieuw."
# Format every row to work for Tabulate
formatted = [_formatRow(row) for row in rows]
formatted = [_format_row(row) for row in rows]
header = "Jupiler Pro League Klassement"
table = tabulate.tabulate(formatted, headers=["#", "Ploeg", "Punten", "M", "M+", "M-", "M=", "D+", "D-", "D+/-"])
@ -165,7 +165,7 @@ def getTable():
return "```{}\n\n{}```".format(header, table)
def _formatRow(row):
def _format_row(row):
"""
Function that formats a row into a list for Tabulate to use
"""