Compare commits
13 Commits
feature/LL
...
feature/me
| Author | SHA1 | Date | |
|---|---|---|---|
| 8ef084c22a | |||
| 57f0cd728f | |||
| 627a29f57e | |||
| 650c500f47 | |||
| 86acbe51e9 | |||
| 4900a7e0ad | |||
| 34da08126b | |||
| 00f416c8bc | |||
| 9c7d0a4486 | |||
| e3b9d6723f | |||
| ef80399a90 | |||
| bfbfa9d9be | |||
| 6b7be4d3b0 |
BIN
assets/img/meme/kiosay.jpg
Executable file
BIN
assets/img/meme/kiosay.jpg
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 71 KiB |
@ -8,6 +8,8 @@ import nonebot
|
|||||||
from nonebot.matcher import Matcher
|
from nonebot.matcher import Matcher
|
||||||
from nonebot.adapters import Bot, Event, Message
|
from nonebot.adapters import Bot, Event, Message
|
||||||
from nonebot.adapters.discord import Bot as DiscordBot
|
from nonebot.adapters.discord import Bot as DiscordBot
|
||||||
|
from nonebot.adapters.discord import MessageEvent as DiscordMessageEvent
|
||||||
|
from nonebot.adapters.discord.config import Config as DiscordConfig
|
||||||
from nonebot.adapters.onebot.v11 import Bot as OnebotV11Bot
|
from nonebot.adapters.onebot.v11 import Bot as OnebotV11Bot
|
||||||
from nonebot.adapters.onebot.v11 import Message as OnebotV11Message
|
from nonebot.adapters.onebot.v11 import Message as OnebotV11Message
|
||||||
from nonebot.adapters.onebot.v11 import MessageEvent as OnebotV11MessageEvent
|
from nonebot.adapters.onebot.v11 import MessageEvent as OnebotV11MessageEvent
|
||||||
@ -17,11 +19,14 @@ from PIL import UnidentifiedImageError
|
|||||||
from returns.result import Failure, Result, Success
|
from returns.result import Failure, Result, Success
|
||||||
|
|
||||||
|
|
||||||
async def download_image_bytes(url: str) -> Result[bytes, str]:
|
discordConfig = nonebot.get_plugin_config(DiscordConfig)
|
||||||
|
|
||||||
|
|
||||||
|
async def download_image_bytes(url: str, proxy: str | None = None) -> Result[bytes, str]:
|
||||||
# if "/matcha/cache/" in url:
|
# if "/matcha/cache/" in url:
|
||||||
# url = url.replace('127.0.0.1', '10.126.126.101')
|
# url = url.replace('127.0.0.1', '10.126.126.101')
|
||||||
logger.debug(f"开始从 {url} 下载图片")
|
logger.debug(f"开始从 {url} 下载图片")
|
||||||
async with httpx.AsyncClient() as c:
|
async with httpx.AsyncClient(proxy=proxy) as c:
|
||||||
try:
|
try:
|
||||||
response = await c.get(url)
|
response = await c.get(url)
|
||||||
except (httpx.ConnectError, httpx.RemoteProtocolError) as e:
|
except (httpx.ConnectError, httpx.RemoteProtocolError) as e:
|
||||||
@ -121,6 +126,14 @@ async def extract_image_from_message(
|
|||||||
logger.debug('获取图片的路径 Fallback 到 QQ 模块')
|
logger.debug('获取图片的路径 Fallback 到 QQ 模块')
|
||||||
return await extract_image_from_qq_message(msg, evt, bot, allow_reply)
|
return await extract_image_from_qq_message(msg, evt, bot, allow_reply)
|
||||||
|
|
||||||
|
if isinstance(evt, DiscordMessageEvent):
|
||||||
|
logger.debug('获取图片的路径方式走 Discord')
|
||||||
|
for a in evt.attachments:
|
||||||
|
if "image/" not in a.content_type:
|
||||||
|
continue
|
||||||
|
url = a.proxy_url
|
||||||
|
return (await download_image_bytes(url, discordConfig.discord_proxy)).bind(bytes_to_pil)
|
||||||
|
|
||||||
for seg in UniMessage.of(msg, bot):
|
for seg in UniMessage.of(msg, bot):
|
||||||
logger.info(seg)
|
logger.info(seg)
|
||||||
if isinstance(seg, Image):
|
if isinstance(seg, Image):
|
||||||
|
|||||||
17
konabot/common/utils/to_async.py
Normal file
17
konabot/common/utils/to_async.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import asyncio
|
||||||
|
import functools
|
||||||
|
|
||||||
|
from typing import Awaitable, Callable, ParamSpec, TypeVar
|
||||||
|
|
||||||
|
|
||||||
|
TA = ParamSpec("TA")
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
|
def make_async(func: Callable[TA, T]) -> Callable[TA, Awaitable[T]]:
|
||||||
|
@functools.wraps(func, assigned=("__module__", "__name__", "__qualname__", "__doc__", "__annotations__"))
|
||||||
|
async def wrapper(*args: TA.args, **kwargs: TA.kwargs):
|
||||||
|
return await asyncio.to_thread(func, *args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
@ -24,6 +24,7 @@ from konabot.plugins.memepack.drawing.display import (
|
|||||||
from konabot.plugins.memepack.drawing.saying import (
|
from konabot.plugins.memepack.drawing.saying import (
|
||||||
draw_cute_ten,
|
draw_cute_ten,
|
||||||
draw_geimao,
|
draw_geimao,
|
||||||
|
draw_kiosay,
|
||||||
draw_mnk,
|
draw_mnk,
|
||||||
draw_pt,
|
draw_pt,
|
||||||
draw_suan,
|
draw_suan,
|
||||||
@ -275,3 +276,29 @@ async def _(msg: UniMsg, evt: Event, bot: Bot):
|
|||||||
.export()
|
.export()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
kiosay = on_alconna(
|
||||||
|
Alconna(
|
||||||
|
"西多说",
|
||||||
|
Args[
|
||||||
|
"saying",
|
||||||
|
MultiVar(str, "+"),
|
||||||
|
Field(missing_tips=lambda: "你没有写西多说了什么"),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
use_cmd_start=True,
|
||||||
|
use_cmd_sep=False,
|
||||||
|
skip_for_unmatch=False,
|
||||||
|
aliases=set(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@kiosay.handle()
|
||||||
|
async def _(saying: list[str]):
|
||||||
|
img = await draw_kiosay("\n".join(saying))
|
||||||
|
img_bytes = BytesIO()
|
||||||
|
img.save(img_bytes, format="PNG")
|
||||||
|
|
||||||
|
await kiosay.send(await UniMessage().image(raw=img_bytes).export())
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import imagetext_py
|
|||||||
import PIL.Image
|
import PIL.Image
|
||||||
|
|
||||||
from konabot.common.path import ASSETS_PATH
|
from konabot.common.path import ASSETS_PATH
|
||||||
|
from konabot.common.utils.to_async import make_async
|
||||||
|
|
||||||
from .base.fonts import HARMONYOS_SANS_SC_BLACK, HARMONYOS_SANS_SC_REGULAR, LXGWWENKAI_REGULAR
|
from .base.fonts import HARMONYOS_SANS_SC_BLACK, HARMONYOS_SANS_SC_REGULAR, LXGWWENKAI_REGULAR
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ mnk_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "mnksay.jpg").convert(
|
|||||||
dasuan_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "dss.png").convert("RGBA")
|
dasuan_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "dss.png").convert("RGBA")
|
||||||
suan_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "suanleba.png").convert("RGBA")
|
suan_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "suanleba.png").convert("RGBA")
|
||||||
cute_ten_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "tententen.png").convert("RGBA")
|
cute_ten_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "tententen.png").convert("RGBA")
|
||||||
|
kio_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "kiosay.jpg").convert("RGBA")
|
||||||
|
|
||||||
|
|
||||||
def _draw_geimao(saying: str):
|
def _draw_geimao(saying: str):
|
||||||
@ -29,7 +31,7 @@ def _draw_geimao(saying: str):
|
|||||||
draw_emojis=True,
|
draw_emojis=True,
|
||||||
)
|
)
|
||||||
return img
|
return img
|
||||||
|
|
||||||
|
|
||||||
async def draw_geimao(saying: str):
|
async def draw_geimao(saying: str):
|
||||||
return await asyncio.to_thread(_draw_geimao, saying)
|
return await asyncio.to_thread(_draw_geimao, saying)
|
||||||
@ -106,3 +108,18 @@ def _draw_cute_ten(saying: str):
|
|||||||
|
|
||||||
async def draw_cute_ten(saying: str):
|
async def draw_cute_ten(saying: str):
|
||||||
return await asyncio.to_thread(_draw_cute_ten, saying)
|
return await asyncio.to_thread(_draw_cute_ten, saying)
|
||||||
|
|
||||||
|
|
||||||
|
@make_async
|
||||||
|
def draw_kiosay(saying: str):
|
||||||
|
img = kio_image.copy()
|
||||||
|
with imagetext_py.Writer(img) as iw:
|
||||||
|
iw.draw_text_wrapped(
|
||||||
|
saying, 450, 540, 0.5, 0.5, 900, 96, LXGWWENKAI_REGULAR,
|
||||||
|
imagetext_py.Paint.Color(imagetext_py.Color.from_hex("000000FF")),
|
||||||
|
1.0,
|
||||||
|
imagetext_py.TextAlign.Center,
|
||||||
|
draw_emojis=True,
|
||||||
|
)
|
||||||
|
return img
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user