Use raw messages
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
sclement 2023-10-30 16:37:32 +01:00
parent 7f8949271f
commit 5c45550184
2 changed files with 64 additions and 37 deletions

40
main.py
View File

@ -5,6 +5,7 @@ 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:
@ -44,44 +45,11 @@ async def on_message(message: discord.Message):
int(match.group("id")), tmp_dir, REFRESH_TOKEN
)
files = os.listdir(tmp_dir)
to_send_files = []
to_send_embed = []
files.reverse()
for file in files:
joined = os.path.join(tmp_dir, file)
embed = discord.Embed(url=match.group("url"))
embed.set_image(url=f"attachment://{file}")
to_send_files.append(discord.File(joined, file))
to_send_embed.append(embed)
if len(to_send_embed) > 4:
parts = math.ceil(len(to_send_embed) / 4)
i = 1
while len(to_send_embed) > 4:
splited_file = to_send_files[:4]
to_send_files = to_send_files[4:]
splited_embed = to_send_embed[:4]
to_send_embed = to_send_embed[4:]
for embed in splited_embed:
embed.title = f"{title} - Part {i}/{parts}"
if i == 1:
await message.reply(
files=splited_file, embeds=splited_embed
await send_message.send_message_with_embed(
message, files, title, tmp_dir, match.group("url")
)
else:
await message.channel.send(
files=splited_file, embeds=splited_embed
)
i = i + 1
for embed in to_send_embed:
embed.title = f"{title} - Part {i}/{parts}"
await message.channel.send(
files=to_send_files, embeds=to_send_embed
)
else:
for embed in to_send_embed:
embed.title = title
await message.reply(files=to_send_files, embeds=to_send_embed)
await send_message.send_message(message, files, tmp_dir)
client.run(DISCORD_TOKEN)

59
send_message.py Normal file
View File

@ -0,0 +1,59 @@
import math
import os
import discord
async def send_message_with_embed(
message: discord.Message, files, title: str, tmp_dir: str, url: str
):
to_send_files = []
to_send_embeds = []
for file in files:
joined = os.path.join(tmp_dir, file)
embed = discord.Embed(url=url)
embed.set_image(url=f"attachment://{file}")
to_send_files.append(discord.File(joined, file))
to_send_embeds.append(embed)
if len(to_send_embeds) > 4:
parts = math.ceil(len(to_send_embeds) / 4)
i = 1
while len(to_send_embeds) > 4:
splited_file = to_send_files[:4]
to_send_files = to_send_files[4:]
splited_embed = to_send_embeds[:4]
to_send_embeds = to_send_embeds[4:]
for embed in splited_embed:
embed.title = f"{title} - Part {i}/{parts}"
if i == 1:
await message.reply(files=splited_file, embeds=splited_embed)
else:
await message.channel.send(files=splited_file, embeds=splited_embed)
i = i + 1
for embed in to_send_embeds:
embed.title = f"{title} - Part {i}/{parts}"
await message.channel.send(files=to_send_files, embeds=to_send_embeds)
else:
for embed in to_send_embeds:
embed.title = title
await message.reply(files=to_send_files, embeds=to_send_embeds)
async def send_message(message: discord.Message, files, tmp_dir: str):
to_send_files = []
for file in files:
joined = os.path.join(tmp_dir, file)
to_send_files.append(discord.File(joined, file))
if len(to_send_files) > 10:
first = True
while len(to_send_files) > 10:
splited_file = to_send_files[:4]
to_send_files = to_send_files[4:]
if first:
await message.reply(files=splited_file)
first = False
else:
await message.channel.send(files=splited_file)
await message.channel.send(files=to_send_files)
else:
await message.reply(files=to_send_files)