2023-10-30 14:58:04 +01:00
|
|
|
import os
|
|
|
|
from shlex import join
|
|
|
|
from tempfile import TemporaryDirectory
|
2023-10-31 11:53:52 +01:00
|
|
|
|
2023-10-30 14:58:04 +01:00
|
|
|
import discord
|
2023-10-31 11:53:52 +01:00
|
|
|
|
|
|
|
import discord_tools.message
|
|
|
|
import discord_tools.pixiv_link_extractor
|
|
|
|
import pixiv.pixiv_api as pixiv_api
|
2023-10-30 14:58:04 +01:00
|
|
|
|
|
|
|
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
|
2023-10-31 11:53:52 +01:00
|
|
|
(result, muted, match) = discord_tools.pixiv_link_extractor.should_process(message)
|
|
|
|
if result and match is not None:
|
2023-10-30 14:58:04 +01:00
|
|
|
with TemporaryDirectory() as tmp_dir:
|
|
|
|
async with message.channel.typing():
|
2023-10-31 11:53:52 +01:00
|
|
|
title = pixiv_api.dowload_pixiv_images(
|
2023-10-30 14:58:04 +01:00
|
|
|
int(match.group("id")), tmp_dir, REFRESH_TOKEN
|
|
|
|
)
|
|
|
|
files = os.listdir(tmp_dir)
|
2023-10-30 16:55:16 +01:00
|
|
|
files.sort()
|
|
|
|
# await send_message.send_message_with_embed(
|
|
|
|
# message, files, title, tmp_dir, match.group("url")
|
|
|
|
# )
|
2023-10-31 11:53:52 +01:00
|
|
|
await discord_tools.message.send_message(message, files, tmp_dir, muted)
|
2023-10-30 14:58:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
client.run(DISCORD_TOKEN)
|