mirror of
https://code.lenaisten.de/Lenaisten/lenaverse-bot.git
synced 2024-11-21 14:33:00 +00:00
/advent deferred followup
This commit is contained in:
parent
2cf963e654
commit
603e634d08
1 changed files with 33 additions and 28 deletions
|
@ -10,19 +10,24 @@ _logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@discord.app_commands.command(name=CONFIG.command_prefix + "advent")
|
@discord.app_commands.command(name=CONFIG.command_prefix + "advent")
|
||||||
async def advent(interaction: discord.Interaction, day: int | None = None) -> None:
|
async def advent(interaction: discord.Interaction, tag: int | None = None) -> None:
|
||||||
"""
|
"""
|
||||||
Türchen vom Lenaisten-Adventskalender öffnen (https://advent.lenaisten.de)
|
Türchen vom Lenaisten-Adventskalender öffnen (https://advent.lenaisten.de)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_logger.debug(
|
_logger.debug(
|
||||||
f"User {interaction.user.name}({interaction.user.id}) used /advent (day: {day})"
|
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,
|
||||||
)
|
)
|
||||||
|
|
||||||
async with aiohttp.ClientSession(
|
async with aiohttp.ClientSession(
|
||||||
auth=aiohttp.BasicAuth(login="", password="")
|
auth=aiohttp.BasicAuth(login="", password="")
|
||||||
) as session:
|
) as session:
|
||||||
if day is None:
|
if tag is None:
|
||||||
# Kein Tag angegeben => neuesten Tag finden
|
# Kein Tag angegeben => neuesten Tag finden
|
||||||
|
|
||||||
async with session.get(
|
async with session.get(
|
||||||
|
@ -30,18 +35,15 @@ async def advent(interaction: discord.Interaction, day: int | None = None) -> No
|
||||||
) as http_response:
|
) as http_response:
|
||||||
if http_response.status != 200:
|
if http_response.status != 200:
|
||||||
# user/doors Anfrage hat nicht geklappt
|
# user/doors Anfrage hat nicht geklappt
|
||||||
await interaction.response.send_message(
|
await interaction.followup.send(
|
||||||
content="Fehler: Ich konnte das aktuelle Türchen nicht finden :zany_face: (Probier's nochmal?)",
|
"Fehler: Ich konnte das aktuelle Türchen nicht finden :zany_face: (Probier's nochmal?)",
|
||||||
# nur für ausführenden User
|
|
||||||
ephemeral=True,
|
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(doors := await http_response.json(), list):
|
if not isinstance(doors := await http_response.json(), list):
|
||||||
# user/doors Antwort falsches Format (keine Liste)
|
# user/doors Antwort falsches Format (keine Liste)
|
||||||
await interaction.response.send_message(
|
await interaction.followup.send(
|
||||||
content="Fehler: Ich konnte das aktuelle Türchen nicht finden :sweat_smile: (Probier's nochmal?)",
|
"Fehler: Ich konnte das aktuelle Türchen nicht finden :sweat_smile: (Probier's nochmal?)",
|
||||||
ephemeral=True,
|
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -49,35 +51,28 @@ async def advent(interaction: discord.Interaction, day: int | None = None) -> No
|
||||||
for door in doors:
|
for door in doors:
|
||||||
if not isinstance(door, dict) or "day" not in door:
|
if not isinstance(door, dict) or "day" not in door:
|
||||||
# user/doors Antwort falsches Format (Liste enthält falsche Daten)
|
# user/doors Antwort falsches Format (Liste enthält falsche Daten)
|
||||||
await interaction.response.send_message(
|
await interaction.followup.send(
|
||||||
content="Fehler: Ich konnte das aktuelle Türchen nicht finden :face_with_monocle: (Probier's nochmal?)",
|
"Fehler: Ich konnte das aktuelle Türchen nicht finden :face_with_monocle: (Probier's nochmal?)",
|
||||||
ephemeral=True,
|
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
days.append(door["day"])
|
days.append(door["day"])
|
||||||
|
|
||||||
day = max(days)
|
tag = max(days)
|
||||||
|
|
||||||
async with session.get(
|
async with session.get(
|
||||||
f"https://advent.lenaisten.de/api/user/image_{day}"
|
f"https://advent.lenaisten.de/api/user/image_{tag}"
|
||||||
) as http_response:
|
) as http_response:
|
||||||
reply_ephemeral = not CONFIG.ev_info.in_allowed_channel(interaction)
|
|
||||||
|
|
||||||
if http_response.status == 401:
|
if http_response.status == 401:
|
||||||
# Bild (noch) nicht verfügbar
|
# Bild (noch) nicht verfügbar
|
||||||
await interaction.response.send_message(
|
await interaction.followup.send(
|
||||||
content=f"Fehler: Tag {day} kann ich (noch?) nicht abrufen. Netter Versuch! :woman_technologist_tone2:",
|
f"Fehler: Tag {tag} kann ich (noch?) nicht abrufen. Netter Versuch! :woman_technologist_tone2:",
|
||||||
ephemeral=reply_ephemeral,
|
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
if http_response.status != 200:
|
if http_response.status != 200:
|
||||||
# Bild (noch) nicht verfügbar
|
# Bild herunterladen fehlgeschlagen
|
||||||
await interaction.response.send_message(
|
await interaction.followup.send(CONFIG.command_failed)
|
||||||
content="Fehler: Ich konnte das Bild nicht herunterladen :sob: (Probier's nochmal?)",
|
|
||||||
ephemeral=True,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
image = discord.File(
|
image = discord.File(
|
||||||
|
@ -85,10 +80,20 @@ async def advent(interaction: discord.Interaction, day: int | None = None) -> No
|
||||||
filename="advent.jpg",
|
filename="advent.jpg",
|
||||||
)
|
)
|
||||||
|
|
||||||
await interaction.response.send_message(
|
if CONFIG.ev_info.in_allowed_channel(interaction):
|
||||||
content=f"Hier ist das Bild für Tag {day}!",
|
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}!",
|
||||||
file=image,
|
file=image,
|
||||||
ephemeral=reply_ephemeral,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue