import math import os import re from shlex import join from tempfile import TemporaryDirectory import discord import pixiv import send_message REFRESH_TOKEN = os.getenv("PIXIV_REFRESH_TOKEN") if REFRESH_TOKEN is None: print("Pixiv refresh token is missing, please set PIXIV_REFRESH_TOKEN") exit(1) DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") if DISCORD_TOKEN is None: print("Discord token is missing, please set DISCORD_TOKEN") exit(1) intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) @client.event async def on_ready(): print(f"We have logged in as {client.user}") @client.event async def on_message(message: discord.Message): if message.author == client.user: return if ( match := re.search( r"(?Phttps?://(?:www\.)?pixiv.net/\w+/artworks/(?P\d+))", message.content, ) ) is not None: with TemporaryDirectory() as tmp_dir: async with message.channel.typing(): title = pixiv.dowload_pixiv_images( int(match.group("id")), tmp_dir, REFRESH_TOKEN ) files = os.listdir(tmp_dir) files.reverse() await send_message.send_message_with_embed( message, files, title, tmp_dir, match.group("url") ) await send_message.send_message(message, files, tmp_dir) client.run(DISCORD_TOKEN)