mirror of
https://code.lenaisten.de/Lenaisten/lenaverse-bot.git
synced 2024-11-22 23:13:01 +00:00
98 lines
3.5 KiB
Python
98 lines
3.5 KiB
Python
|
import logging
|
||
|
from io import BytesIO
|
||
|
|
||
|
import aiohttp
|
||
|
import discord
|
||
|
|
||
|
from ..core.config import CONFIG
|
||
|
|
||
|
_logger = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
@discord.app_commands.command(name=CONFIG.command_prefix + "advent")
|
||
|
async def advent(interaction: discord.Interaction, day: int | None = None) -> None:
|
||
|
"""
|
||
|
Türchen vom Lenaisten-Adventskalender öffnen (https://advent.lenaisten.de)
|
||
|
"""
|
||
|
|
||
|
_logger.debug(
|
||
|
f"User {interaction.user.name}({interaction.user.id}) used /advent (day: {day})"
|
||
|
)
|
||
|
|
||
|
async with aiohttp.ClientSession(
|
||
|
auth=aiohttp.BasicAuth(login="", password="")
|
||
|
) as session:
|
||
|
if day is None:
|
||
|
# Kein Tag angegeben => neuesten Tag finden
|
||
|
|
||
|
async with session.get(
|
||
|
"https://advent.lenaisten.de/api/user/doors"
|
||
|
) as http_response:
|
||
|
if http_response.status != 200:
|
||
|
# user/doors Anfrage hat nicht geklappt
|
||
|
await interaction.response.send_message(
|
||
|
content="Fehler: Ich konnte das aktuelle Türchen nicht finden :zany_face: (Probier's nochmal?)",
|
||
|
# nur für ausführenden User
|
||
|
ephemeral=True,
|
||
|
)
|
||
|
return
|
||
|
|
||
|
if not isinstance(doors := await http_response.json(), list):
|
||
|
# user/doors Antwort falsches Format (keine Liste)
|
||
|
await interaction.response.send_message(
|
||
|
content="Fehler: Ich konnte das aktuelle Türchen nicht finden :sweat_smile: (Probier's nochmal?)",
|
||
|
ephemeral=True,
|
||
|
)
|
||
|
return
|
||
|
|
||
|
days: list[int] = []
|
||
|
for door in doors:
|
||
|
if not isinstance(door, dict) or "day" not in door:
|
||
|
# user/doors Antwort falsches Format (Liste enthält falsche Daten)
|
||
|
await interaction.response.send_message(
|
||
|
content="Fehler: Ich konnte das aktuelle Türchen nicht finden :face_with_monocle: (Probier's nochmal?)",
|
||
|
ephemeral=True,
|
||
|
)
|
||
|
return
|
||
|
|
||
|
days.append(door["day"])
|
||
|
|
||
|
day = max(days)
|
||
|
|
||
|
async with session.get(
|
||
|
f"https://advent.lenaisten.de/api/user/image_{day}"
|
||
|
) as http_response:
|
||
|
reply_ephemeral = not CONFIG.ev_info.in_allowed_channel(interaction)
|
||
|
|
||
|
if http_response.status == 401:
|
||
|
# Bild (noch) nicht verfügbar
|
||
|
await interaction.response.send_message(
|
||
|
content=f"Fehler: Tag {day} kann ich (noch?) nicht abrufen. Netter Versuch! :woman_technologist_tone2:",
|
||
|
ephemeral=reply_ephemeral,
|
||
|
)
|
||
|
return
|
||
|
|
||
|
if http_response.status != 200:
|
||
|
# Bild (noch) nicht verfügbar
|
||
|
await interaction.response.send_message(
|
||
|
content="Fehler: Ich konnte das Bild nicht herunterladen :sob: (Probier's nochmal?)",
|
||
|
ephemeral=True,
|
||
|
)
|
||
|
return
|
||
|
|
||
|
image = discord.File(
|
||
|
fp=BytesIO(await http_response.read()),
|
||
|
filename="advent.jpg",
|
||
|
)
|
||
|
|
||
|
await interaction.response.send_message(
|
||
|
content=f"Hier ist das Bild für Tag {day}!",
|
||
|
file=image,
|
||
|
ephemeral=reply_ephemeral,
|
||
|
)
|
||
|
|
||
|
|
||
|
COMMANDS = [
|
||
|
advent,
|
||
|
]
|