pixivcord/src/main.py

162 lines
5.0 KiB
Python
Raw Normal View History

2023-11-02 21:41:53 +01:00
import asyncio
2023-11-02 12:02:03 +01:00
import logging
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
2023-11-02 12:02:03 +01:00
logging.basicConfig(
format="[%(asctime)s] [%(levelname)8s] %(name)s: %(message)s", level=logging.INFO
)
2023-10-30 14:58:04 +01:00
REFRESH_TOKEN = os.getenv("PIXIV_REFRESH_TOKEN")
if REFRESH_TOKEN is None:
2023-11-02 12:02:03 +01:00
logging.fatal("Pixiv refresh token is missing, please set PIXIV_REFRESH_TOKEN")
2023-10-30 14:58:04 +01:00
exit(1)
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
if DISCORD_TOKEN is None:
2023-11-02 12:02:03 +01:00
logging.fatal("Discord token is missing, please set DISCORD_TOKEN")
2023-10-30 14:58:04 +01:00
exit(1)
intents = discord.Intents.default()
intents.message_content = True
2023-10-31 15:31:07 +01:00
class MyClient(discord.Client):
def __init__(self, *, intents: discord.Intents):
super().__init__(intents=intents)
self.tree = discord.app_commands.CommandTree(self)
async def setup_hook(self):
await self.tree.sync()
client = MyClient(intents=intents)
2023-10-30 14:58:04 +01:00
@client.event
async def on_ready():
2023-11-02 12:02:03 +01:00
logging.info(f"We have logged in as {client.user}")
2023-10-30 14:58:04 +01:00
2023-10-31 15:31:07 +01:00
@client.tree.command()
@discord.app_commands.describe(
url="Pixiv art url",
)
async def pixivprev(interaction: discord.Interaction, url: str):
"""Create pixiv preview"""
(result, match) = discord_tools.pixiv_link_extractor.check_pixiv_url(url)
if result and match is not None:
await interaction.response.defer()
with TemporaryDirectory() as tmp_dir:
2023-11-02 21:41:53 +01:00
loop = asyncio.get_running_loop()
data = await loop.run_in_executor(
None,
2023-11-02 22:20:33 +01:00
pixiv_api.download_pixiv_images,
2023-11-02 21:41:53 +01:00
int(match.group("id")),
tmp_dir,
REFRESH_TOKEN,
2023-10-31 15:31:07 +01:00
)
2023-11-02 16:29:28 +01:00
if data is None:
2023-11-01 21:33:13 +01:00
await interaction.response.send_message(
"Fail to extract pixiv image", ephemeral=True
)
return
2023-11-02 16:29:28 +01:00
(title, author) = data
2023-10-31 15:31:07 +01:00
files = os.listdir(tmp_dir)
files.sort()
await discord_tools.message.send_command_reply(
interaction,
files,
tmp_dir,
True,
title=title,
2023-11-02 16:29:28 +01:00
art_author=author,
2023-10-31 15:31:07 +01:00
url=url,
author=interaction.user,
)
else:
await interaction.response.send_message("Invalid pixiv url", ephemeral=True)
@client.tree.context_menu(name="Create pixiv preview")
async def pixiv_preview(interaction: discord.Interaction, message: discord.Message):
(result, muted, match) = discord_tools.pixiv_link_extractor.should_process(message)
if result and match is not None:
await interaction.response.defer()
with TemporaryDirectory() as tmp_dir:
2023-11-02 21:41:53 +01:00
loop = asyncio.get_running_loop()
data = await loop.run_in_executor(
None,
2023-11-02 22:20:33 +01:00
pixiv_api.download_pixiv_images,
2023-11-02 21:41:53 +01:00
int(match.group("id")),
tmp_dir,
REFRESH_TOKEN,
2023-10-31 15:31:07 +01:00
)
2023-11-02 16:29:28 +01:00
if data is None:
2023-11-01 21:33:13 +01:00
await interaction.response.send_message(
"Fail to extract pixiv image", ephemeral=True
)
return
2023-10-31 15:31:07 +01:00
files = os.listdir(tmp_dir)
files.sort()
2023-11-02 16:29:28 +01:00
(title, author) = data
2023-10-31 15:31:07 +01:00
await discord_tools.message.send_command_reply(
interaction=interaction,
files=files,
tmp_dir=tmp_dir,
muted=muted,
title=title,
2023-11-02 16:29:28 +01:00
art_author=author,
2023-10-31 15:31:07 +01:00
url=match.group("url"),
2023-11-02 22:20:33 +01:00
author=message.author,
2023-10-31 15:31:07 +01:00
)
else:
await interaction.response.send_message(
"Can't find any valid pixiv link.", ephemeral=True
)
@client.event
async def on_guild_join(guild: discord.Guild):
2023-11-02 12:02:03 +01:00
logging.info(f"Joined new guild: {guild.name}")
2023-10-31 15:31:07 +01:00
2023-10-30 14:58:04 +01:00
@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-11-02 21:41:53 +01:00
loop = asyncio.get_running_loop()
data = await loop.run_in_executor(
None,
2023-11-02 22:20:33 +01:00
pixiv_api.download_pixiv_images,
2023-11-02 21:41:53 +01:00
int(match.group("id")),
tmp_dir,
REFRESH_TOKEN,
2023-10-30 14:58:04 +01:00
)
2023-11-02 16:29:28 +01:00
if data is None:
2023-11-01 21:33:13 +01:00
return
2024-01-25 11:13:49 +01:00
(title, author) = data
2023-10-30 14:58:04 +01:00
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")
# )
2024-01-25 11:13:49 +01:00
await discord_tools.message.send_message(
message, files, tmp_dir, muted, title, author
)
2023-10-30 14:58:04 +01:00
client.run(DISCORD_TOKEN)