lenaverse-bot/lenaverse_bot/commands/advent.py

103 lines
3.6 KiB
Python
Raw Normal View History

2023-12-02 23:52:26 +00:00
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")
2023-12-06 17:11:07 +00:00
async def advent(interaction: discord.Interaction, tag: int | None = None) -> None:
2023-12-02 23:52:26 +00:00
"""
Türchen vom Lenaisten-Adventskalender öffnen (https://advent.lenaisten.de)
"""
_logger.debug(
2023-12-06 17:11:07 +00:00
f"User {interaction.user.name}({interaction.user.id}) used /advent ({tag = })"
)
await interaction.response.defer(
thinking=True,
# nur für ausführenden User
ephemeral=True,
2023-12-02 23:52:26 +00:00
)
async with aiohttp.ClientSession(
auth=aiohttp.BasicAuth(login="", password="")
) as session:
2023-12-06 17:11:07 +00:00
if tag is None:
2023-12-02 23:52:26 +00:00
# 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
2023-12-06 17:11:07 +00:00
await interaction.followup.send(
"Fehler: Ich konnte das aktuelle Türchen nicht finden :zany_face: (Probier's nochmal?)",
2023-12-02 23:52:26 +00:00
)
return
if not isinstance(doors := await http_response.json(), list):
# user/doors Antwort falsches Format (keine Liste)
2023-12-06 17:11:07 +00:00
await interaction.followup.send(
"Fehler: Ich konnte das aktuelle Türchen nicht finden :sweat_smile: (Probier's nochmal?)",
2023-12-02 23:52:26 +00:00
)
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)
2023-12-06 17:11:07 +00:00
await interaction.followup.send(
"Fehler: Ich konnte das aktuelle Türchen nicht finden :face_with_monocle: (Probier's nochmal?)",
2023-12-02 23:52:26 +00:00
)
return
days.append(door["day"])
2023-12-06 17:11:07 +00:00
tag = max(days)
2023-12-02 23:52:26 +00:00
async with session.get(
2023-12-06 17:11:07 +00:00
f"https://advent.lenaisten.de/api/user/image_{tag}"
2023-12-02 23:52:26 +00:00
) as http_response:
if http_response.status == 401:
# Bild (noch) nicht verfügbar
2023-12-06 17:11:07 +00:00
await interaction.followup.send(
f"Fehler: Tag {tag} kann ich (noch?) nicht abrufen. Netter Versuch! :woman_technologist_tone2:",
2023-12-02 23:52:26 +00:00
)
return
if http_response.status != 200:
2023-12-06 17:11:07 +00:00
# Bild herunterladen fehlgeschlagen
await interaction.followup.send(CONFIG.command_failed)
2023-12-02 23:52:26 +00:00
return
image = discord.File(
fp=BytesIO(await http_response.read()),
filename="advent.jpg",
)
2023-12-06 17:11:07 +00:00
if CONFIG.ev_info.in_allowed_channel(interaction):
await interaction.followup.send(":ok:")
assert isinstance(interaction.channel, discord.abc.Messageable)
target_channel = interaction.channel
else:
await interaction.followup.send(
f"Hier darf ich leider nicht posten, also schicke ich das Bild hier: <#{CONFIG.post.channel}> :incoming_envelope:"
)
target_channel = CONFIG.post.get_channel(interaction.client)
await target_channel.send(
content=f"<@{interaction.user.id}>, hier ist das Adventskalender-Bild für Tag {tag}!",
2023-12-02 23:52:26 +00:00
file=image,
)
COMMANDS = [
advent,
]