Add muted tips
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
sclement 2023-10-31 11:53:52 +01:00
parent d08d5131f7
commit 7047538a78
11 changed files with 45 additions and 40 deletions

2
.vscode/launch.json vendored
View File

@ -8,7 +8,7 @@
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "main.py",
"program": "src/main.py",
"envFile": "${workspaceFolder}/.env",
"console": "integratedTerminal",
"justMyCode": true

View File

@ -1,6 +1,9 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
},
"python.formatting.provider": "none",
"python.analysis.autoImportCompletions": true,

View File

@ -19,4 +19,4 @@ COPY . /app
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
CMD ["python", "main.py"]
CMD ["python", "src/main.py"]

View File

@ -1,11 +0,0 @@
version: '3.4'
services:
pixivcord:
image: pixivcord
build:
context: .
dockerfile: ./Dockerfile
command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 main.py "]
ports:
- 5678:5678

View File

@ -1,8 +0,0 @@
version: '3.4'
services:
pixivcord:
image: pixivcord
build:
context: .
dockerfile: ./Dockerfile

View File

View File

@ -1,7 +1,10 @@
import math
import os
import discord
TIPS_MESSAGE = ":bulb: TIPS: You can disable Discord's default preview for link by wrapping it around `<` and `>`."
async def send_message_with_embed(
message: discord.Message, files, title: str, tmp_dir: str, url: str
@ -39,7 +42,7 @@ async def send_message_with_embed(
await message.reply(files=to_send_files, embeds=to_send_embeds)
async def send_message(message: discord.Message, files, tmp_dir: str):
async def send_message(message: discord.Message, files, tmp_dir: str, muted: bool):
to_send_files = []
for file in files:
joined = os.path.join(tmp_dir, file)
@ -47,13 +50,17 @@ async def send_message(message: discord.Message, files, tmp_dir: str):
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:]
splited_file = to_send_files[:10]
to_send_files = to_send_files[10:]
if first:
await message.reply(files=splited_file)
await message.reply(
files=splited_file, content=(TIPS_MESSAGE if not muted else None)
)
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)
await message.reply(
files=to_send_files, content=(TIPS_MESSAGE if not muted else None)
)

View File

@ -0,0 +1,18 @@
import re
from typing import Tuple, Union
import discord
link_regexp = r"(?P<muted1><)?(?P<url>https?://(?:www\.)?pixiv.net/(?:\w+/)?artworks/(?P<id>\d+))(?P<muted2>>)?"
def should_process(
message: discord.Message,
) -> Tuple[bool, bool, Union[re.Match, None]]:
if match := re.search(link_regexp, message.content):
muted = bool(match.group("muted1")) and bool(match.group("muted2"))
if len(message.attachments) == 0:
return (True, muted, match)
else:
return (False, False, match)
return (False, False, None)

View File

@ -1,11 +1,12 @@
import math
import os
import re
from shlex import join
from tempfile import TemporaryDirectory
import discord
import pixiv
import send_message
import discord_tools.message
import discord_tools.pixiv_link_extractor
import pixiv.pixiv_api as pixiv_api
REFRESH_TOKEN = os.getenv("PIXIV_REFRESH_TOKEN")
if REFRESH_TOKEN is None:
@ -32,16 +33,11 @@ async def on_ready():
async def on_message(message: discord.Message):
if message.author == client.user:
return
if (
match := re.search(
r"(?P<url>https?://(?:www\.)?pixiv.net/(?:\w+/)?artworks/(?P<id>\d+))",
message.content,
)
) is not None:
(result, muted, match) = discord_tools.pixiv_link_extractor.should_process(message)
if result and match is not None:
with TemporaryDirectory() as tmp_dir:
async with message.channel.typing():
title = pixiv.dowload_pixiv_images(
title = pixiv_api.dowload_pixiv_images(
int(match.group("id")), tmp_dir, REFRESH_TOKEN
)
files = os.listdir(tmp_dir)
@ -49,7 +45,7 @@ async def on_message(message: discord.Message):
# await send_message.send_message_with_embed(
# message, files, title, tmp_dir, match.group("url")
# )
await send_message.send_message(message, files, tmp_dir)
await discord_tools.message.send_message(message, files, tmp_dir, muted)
client.run(DISCORD_TOKEN)

0
src/pixiv/__init__.py Normal file
View File