From 5c455501846dc0deab5361b5166be06cc6c4aba9 Mon Sep 17 00:00:00 2001 From: sclement Date: Mon, 30 Oct 2023 16:37:32 +0100 Subject: [PATCH] Use raw messages --- main.py | 42 +++++------------------------------ send_message.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 37 deletions(-) create mode 100644 send_message.py diff --git a/main.py b/main.py index 4d31514..8e7c103 100644 --- a/main.py +++ b/main.py @@ -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 - ) - 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_with_embed( + message, files, title, tmp_dir, match.group("url") + ) + await send_message.send_message(message, files, tmp_dir) client.run(DISCORD_TOKEN) diff --git a/send_message.py b/send_message.py new file mode 100644 index 0000000..753208e --- /dev/null +++ b/send_message.py @@ -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)