zhenxun_bot/plugins/group_welcome_msg.py

43 lines
1.5 KiB
Python
Raw Normal View History

2021-05-20 19:27:31 +08:00
from nonebot import on_command
from nonebot.typing import T_State
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent
from nonebot.adapters.cqhttp.permission import GROUP
from configs.path_config import DATA_PATH
2021-07-30 21:21:51 +08:00
from utils.message_builder import image
2021-05-20 19:27:31 +08:00
import os
from pathlib import Path
2021-07-30 21:21:51 +08:00
2021-05-20 19:27:31 +08:00
try:
import ujson as json
except ModuleNotFoundError:
import json
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-07-30 21:21:51 +08:00
view_custom_welcome = on_command(
"群欢迎消息", aliases={"查看群欢迎消息", "查看当前群欢迎消息"}, permission=GROUP, priority=5, block=True
)
2021-05-20 19:27:31 +08:00
@view_custom_welcome.handle()
async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
2021-07-30 21:21:51 +08:00
img = ""
msg = ""
if os.path.exists(DATA_PATH + f"custom_welcome_msg/{event.group_id}.jpg"):
img = image(abspath=DATA_PATH + f"custom_welcome_msg/{event.group_id}.jpg")
custom_welcome_msg_json = (
Path() / "data" / "custom_welcome_msg" / "custom_welcome_msg.json"
)
2021-05-20 19:27:31 +08:00
if custom_welcome_msg_json.exists():
2021-07-30 21:21:51 +08:00
data = json.load(open(custom_welcome_msg_json, "r"))
2021-05-20 19:27:31 +08:00
if data.get(str(event.group_id)):
msg = data[str(event.group_id)]
2021-07-30 21:21:51 +08:00
if msg.find("[at]") != -1:
msg = msg.replace("[at]", "")
2021-05-20 19:27:31 +08:00
if img or msg:
await view_custom_welcome.finish(msg + img, at_sender=True)
2021-05-20 20:47:34 +08:00
else:
2021-07-30 21:21:51 +08:00
await view_custom_welcome.finish("当前还没有自定义群欢迎消息哦", at_sender=True)