mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
Add diarioshuffle command and api_diario_random.py
This commit is contained in:
parent
a4c472a79e
commit
70d2b5fee5
4 changed files with 66 additions and 0 deletions
|
@ -36,6 +36,7 @@ from .steammatch import SteammatchCommand
|
||||||
from .dota import DotaCommand
|
from .dota import DotaCommand
|
||||||
from .magickfiorygi import MagickfiorygiCommand
|
from .magickfiorygi import MagickfiorygiCommand
|
||||||
from .brawlhalla import BrawlhallaCommand
|
from .brawlhalla import BrawlhallaCommand
|
||||||
|
from .diarioshuffle import DiarioshuffleCommand
|
||||||
|
|
||||||
# Enter the commands of your Pack here!
|
# Enter the commands of your Pack here!
|
||||||
available_commands = [
|
available_commands = [
|
||||||
|
@ -76,6 +77,7 @@ available_commands = [
|
||||||
DotaCommand,
|
DotaCommand,
|
||||||
MagickfiorygiCommand,
|
MagickfiorygiCommand,
|
||||||
BrawlhallaCommand,
|
BrawlhallaCommand,
|
||||||
|
DiarioshuffleCommand,
|
||||||
]
|
]
|
||||||
|
|
||||||
# Don't change this, it should automatically generate __all__
|
# Don't change this, it should automatically generate __all__
|
||||||
|
|
28
royalpack/commands/diarioshuffle.py
Normal file
28
royalpack/commands/diarioshuffle.py
Normal file
|
@ -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}")
|
|
@ -9,6 +9,7 @@ from .api_wiki_edit import ApiWikiEditStar
|
||||||
from .api_wiki_get import ApiWikiGetStar
|
from .api_wiki_get import ApiWikiGetStar
|
||||||
from .api_wiki_list import ApiWikiListStar
|
from .api_wiki_list import ApiWikiListStar
|
||||||
from .api_fiorygi_get import ApiFiorygiGetStar
|
from .api_fiorygi_get import ApiFiorygiGetStar
|
||||||
|
from .api_diario_random import ApiDiarioRandomStar
|
||||||
|
|
||||||
# Enter the PageStars of your Pack here!
|
# Enter the PageStars of your Pack here!
|
||||||
available_page_stars = [
|
available_page_stars = [
|
||||||
|
@ -22,6 +23,7 @@ available_page_stars = [
|
||||||
ApiWikiGetStar,
|
ApiWikiGetStar,
|
||||||
ApiWikiListStar,
|
ApiWikiListStar,
|
||||||
ApiFiorygiGetStar,
|
ApiFiorygiGetStar,
|
||||||
|
ApiDiarioRandomStar,
|
||||||
]
|
]
|
||||||
|
|
||||||
# Don't change this, it should automatically generate __all__
|
# Don't change this, it should automatically generate __all__
|
||||||
|
|
34
royalpack/stars/api_diario_random.py
Normal file
34
royalpack/stars/api_diario_random.py
Normal file
|
@ -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))
|
Loading…
Reference in a new issue