This commit is contained in:
43
konabot/plugins/krgsay.py
Normal file
43
konabot/plugins/krgsay.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import re
|
||||||
|
from typing import Any
|
||||||
|
from nonebot import on_message
|
||||||
|
from nonebot.adapters import Event
|
||||||
|
from nonebot_plugin_alconna import UniMessage, UniMsg
|
||||||
|
from playwright.async_api import Page
|
||||||
|
|
||||||
|
from konabot.common.nb import match_keyword
|
||||||
|
from konabot.common.web_render import WebRenderer, konaweb
|
||||||
|
|
||||||
|
|
||||||
|
async def render_image(message: str) -> UniMessage[Any]:
|
||||||
|
"""
|
||||||
|
渲染文本为图片
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def page_function(page: Page):
|
||||||
|
await page.wait_for_function("typeof setContent === 'function'")
|
||||||
|
await page.evaluate(
|
||||||
|
"(message) => { return setContent(message); }",
|
||||||
|
message,
|
||||||
|
)
|
||||||
|
|
||||||
|
img_data = await WebRenderer.render(
|
||||||
|
url=konaweb("krgsay"),
|
||||||
|
target="#main",
|
||||||
|
other_function=page_function,
|
||||||
|
)
|
||||||
|
return UniMessage.image(raw=img_data)
|
||||||
|
|
||||||
|
|
||||||
|
cmd = on_message(
|
||||||
|
rule=match_keyword.match_keyword(
|
||||||
|
re.compile(r"^[kK][rR][gG][sS][aA][yY] .+"),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
@cmd.handle()
|
||||||
|
async def _(event: Event, msg: UniMsg):
|
||||||
|
text = msg.extract_plain_text().strip()[6:].strip()
|
||||||
|
msg = await render_image(text)
|
||||||
|
await msg.send(event)
|
||||||
|
|
||||||
Reference in New Issue
Block a user