From 70d2b5fee5546cf9058efc943d9139907670e8eb Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 23 Mar 2020 02:55:25 +0100 Subject: [PATCH] Add diarioshuffle command and api_diario_random.py --- royalpack/commands/__init__.py | 2 ++ royalpack/commands/diarioshuffle.py | 28 +++++++++++++++++++++++ royalpack/stars/__init__.py | 2 ++ royalpack/stars/api_diario_random.py | 34 ++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 royalpack/commands/diarioshuffle.py create mode 100644 royalpack/stars/api_diario_random.py diff --git a/royalpack/commands/__init__.py b/royalpack/commands/__init__.py index d3057a48..0df9b6f8 100644 --- a/royalpack/commands/__init__.py +++ b/royalpack/commands/__init__.py @@ -36,6 +36,7 @@ from .steammatch import SteammatchCommand from .dota import DotaCommand from .magickfiorygi import MagickfiorygiCommand from .brawlhalla import BrawlhallaCommand +from .diarioshuffle import DiarioshuffleCommand # Enter the commands of your Pack here! available_commands = [ @@ -76,6 +77,7 @@ available_commands = [ DotaCommand, MagickfiorygiCommand, BrawlhallaCommand, + DiarioshuffleCommand, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/commands/diarioshuffle.py b/royalpack/commands/diarioshuffle.py new file mode 100644 index 00000000..8baa7a37 --- /dev/null +++ b/royalpack/commands/diarioshuffle.py @@ -0,0 +1,28 @@ +from typing import * +from royalnet.commands import * +from royalnet.utils import * +from ..tables import Diario +from sqlalchemy import func + + +class DiarioshuffleCommand(Command): + name: str = "diarioshuffle" + + description: str = "Cita una riga casuale del diario." + + aliases = ["dis", "dishuffle", "dish"] + + syntax = "" + + async def run(self, args: CommandArgs, data: CommandData) -> None: + DiarioT = self.alchemy.get(Diario) + entry: List[Diario] = await asyncify( + data.session + .query(DiarioT) + .order_by(func.random()) + .limit(1) + .one_or_none + ) + if entry is None: + raise CommandError("Nessuna riga del diario trovata.") + await data.reply(f"ℹ️ {entry}") diff --git a/royalpack/stars/__init__.py b/royalpack/stars/__init__.py index a36a1347..140044c3 100644 --- a/royalpack/stars/__init__.py +++ b/royalpack/stars/__init__.py @@ -9,6 +9,7 @@ from .api_wiki_edit import ApiWikiEditStar from .api_wiki_get import ApiWikiGetStar from .api_wiki_list import ApiWikiListStar from .api_fiorygi_get import ApiFiorygiGetStar +from .api_diario_random import ApiDiarioRandomStar # Enter the PageStars of your Pack here! available_page_stars = [ @@ -22,6 +23,7 @@ available_page_stars = [ ApiWikiGetStar, ApiWikiListStar, ApiFiorygiGetStar, + ApiDiarioRandomStar, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/stars/api_diario_random.py b/royalpack/stars/api_diario_random.py new file mode 100644 index 00000000..3a2fc413 --- /dev/null +++ b/royalpack/stars/api_diario_random.py @@ -0,0 +1,34 @@ +from typing import * +from royalnet.constellation.api import * +from royalnet.utils import * +from ..tables import * +from sqlalchemy import func + + +class ApiDiarioRandomStar(ApiStar): + path = "/api/diario/random/v1" + + summary = "Get random diario entries." + + parameters = { + "amount": "The number of diario entries to get." + } + + tags = ["diario"] + + async def api(self, data: ApiData) -> JSON: + DiarioT = self.alchemy.get(Diario) + try: + amount = int(data["amount"]) + except ValueError: + raise InvalidParameterError("'amount' is not a valid int.") + entries: List[Diario] = await asyncify( + data.session + .query(DiarioT) + .order_by(func.random()) + .limit(amount) + .all + ) + if len(entries) < amount: + raise NotFoundError("Not enough diario entries.") + return list(map(lambda e: e.json(), entries))