zhenxun_bot/plugins/luxun/__init__.py

69 lines
2.5 KiB
Python
Raw Normal View History

2021-05-20 19:23:32 +08:00
from PIL import ImageFont, ImageDraw, Image
import textwrap
from configs.path_config import IMAGE_PATH, TTF_PATH
from nonebot import on_command
from nonebot.typing import T_State
2021-07-30 21:21:51 +08:00
from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
from utils.message_builder import image
2021-05-20 19:23:32 +08:00
from services.log import logger
2021-06-30 19:50:55 +08:00
from utils.utils import UserExistLimiter, get_message_text
2021-07-30 21:21:51 +08:00
from utils.image_utils import pic2b64
2021-05-20 19:23:32 +08:00
2021-07-30 21:21:51 +08:00
__plugin_name__ = "鲁迅说"
2021-06-15 10:57:08 +08:00
2021-07-30 21:21:51 +08:00
__plugin_usage__ = "用法:鲁迅说 [消息]"
2021-06-15 10:57:08 +08:00
2021-05-20 19:23:32 +08:00
_ulmt = UserExistLimiter()
2021-06-30 19:50:55 +08:00
luxun = on_command("鲁迅说过", aliases={"鲁迅说"}, priority=5, block=True)
2021-05-20 19:23:32 +08:00
@luxun.handle()
async def handle(bot: Bot, event: MessageEvent, state: T_State):
if _ulmt.check(event.user_id):
2021-07-30 21:21:51 +08:00
await luxun.finish("你的鲁迅正在说,等会", at_sender=True)
2021-05-20 19:23:32 +08:00
args = get_message_text(event.json())
if args:
2021-07-30 21:21:51 +08:00
state["content"] = args if args else "烦了,不说了"
2021-05-20 19:23:32 +08:00
@luxun.got("content", prompt="你让鲁迅说点啥?")
async def handle_event(bot: Bot, event: MessageEvent, state: T_State):
filename = str(event.user_id) + "_.jpg"
content = state["content"].strip()
2021-07-30 21:21:51 +08:00
if content.startswith(",") or content.startswith(""):
2021-05-20 19:23:32 +08:00
content = content[1:]
_ulmt.set_True(event.user_id)
if len(content) > 20:
_ulmt.set_False(event.user_id)
await luxun.finish("太长了, 鲁迅说不完!", at_sender=True)
else:
if len(content) >= 12:
2021-07-30 21:21:51 +08:00
content = content[:12] + "\n" + content[12:]
2021-05-20 19:23:32 +08:00
img = image(b64=process_pic(content, filename))
2021-07-30 21:21:51 +08:00
logger.info(
f"USER {event.user_id} GROUP "
f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'} 鲁迅说过 {content}"
)
2021-05-20 19:23:32 +08:00
await luxun.send(img)
_ulmt.set_False(event.user_id)
def process_pic(content, filename) -> str:
text = content
para = textwrap.wrap(text, width=15)
MAX_W, MAX_H = 480, 280
bk_img = Image.open(IMAGE_PATH + "other/luxun.jpg")
font_path = TTF_PATH + "/msyh.ttf"
font = ImageFont.truetype(font_path, 37)
font2 = ImageFont.truetype(font_path, 30)
draw = ImageDraw.Draw(bk_img)
current_h, pad = 300, 10
for line in para:
w, h = draw.textsize(line, font=font)
draw.text(((MAX_W - w) / 2, current_h), line, font=font)
current_h += h + pad
draw.text((320, 400), "——鲁迅", font=font2, fill=(255, 255, 255))
return pic2b64(bk_img)