zhenxun_bot/zhenxun/plugins/fudu.py

152 lines
4.7 KiB
Python
Raw Normal View History

2024-05-28 14:22:24 +08:00
import random
from nonebot import on_message
from nonebot.adapters import Event
from nonebot.plugin import PluginMetadata
from nonebot_plugin_alconna import Image as alcImg
from nonebot_plugin_alconna import UniMsg
from nonebot_plugin_session import EventSession
2024-08-24 17:06:23 +08:00
from zhenxun.configs.config import BotConfig, Config
2024-05-28 14:22:24 +08:00
from zhenxun.configs.path_config import TEMP_PATH
from zhenxun.configs.utils import PluginExtraData, RegisterConfig, Task
from zhenxun.models.task_info import TaskInfo
from zhenxun.utils.enum import PluginType
2024-08-03 00:34:19 +08:00
from zhenxun.utils.image_utils import get_download_image_hash
2024-08-10 02:25:04 +08:00
from zhenxun.utils.message import MessageUtils
2024-05-28 14:22:24 +08:00
from zhenxun.utils.rules import ensure_group
__plugin_meta__ = PluginMetadata(
name="复读",
description="群友的本质是什么?是复读机哒!",
usage="""
usage
重复3次相同的消息时会复读
""".strip(),
extra=PluginExtraData(
author="HibiKier",
version="0.1",
menu_type="其他",
2024-08-18 22:26:53 +08:00
plugin_type=PluginType.DEPENDANT,
2024-05-28 14:22:24 +08:00
tasks=[Task(module="fudu", name="复读")],
configs=[
RegisterConfig(
key="FUDU_PROBABILITY",
value=0.7,
help="复读概率",
default_value=0.7,
type=float,
),
RegisterConfig(
module="_task",
key="DEFAULT_FUDU",
value=True,
help="被动 复读 进群默认开关状态",
default_value=True,
type=bool,
),
],
).dict(),
)
class Fudu:
def __init__(self):
self.data = {}
def append(self, key, content):
self._create(key)
self.data[key]["data"].append(content)
def clear(self, key):
self._create(key)
self.data[key]["data"] = []
self.data[key]["is_repeater"] = False
def size(self, key) -> int:
self._create(key)
return len(self.data[key]["data"])
def check(self, key, content) -> bool:
self._create(key)
return self.data[key]["data"][0] == content
def get(self, key):
self._create(key)
return self.data[key]["data"][0]
def is_repeater(self, key):
self._create(key)
return self.data[key]["is_repeater"]
def set_repeater(self, key):
self._create(key)
self.data[key]["is_repeater"] = True
def _create(self, key):
if self.data.get(key) is None:
self.data[key] = {"is_repeater": False, "data": []}
_manage = Fudu()
2024-05-29 02:01:26 +08:00
2024-05-28 14:22:24 +08:00
2024-08-08 02:51:11 +08:00
base_config = Config.get("fudu")
2024-05-28 14:22:24 +08:00
_matcher = on_message(rule=ensure_group, priority=999)
@_matcher.handle()
async def _(message: UniMsg, event: Event, session: EventSession):
2024-08-03 00:34:19 +08:00
group_id = session.id2 or ""
if await TaskInfo.is_block("fudu", group_id):
2024-05-28 14:22:24 +08:00
return
if event.is_tome():
return
plain_text = message.extract_plain_text()
image_list = []
for m in message:
if isinstance(m, alcImg):
if m.url:
image_list.append(m.url)
if not plain_text and not image_list:
return
2024-08-24 19:32:52 +08:00
if plain_text and plain_text.startswith(f"@可爱的{BotConfig.self_nickname}"):
2024-08-11 15:57:33 +08:00
await MessageUtils.build_message("复制粘贴的虚空艾特?").send(reply_to=True)
2024-05-28 14:22:24 +08:00
if image_list:
2024-05-29 02:01:26 +08:00
img_hash = await get_download_image_hash(image_list[0], group_id)
2024-05-28 14:22:24 +08:00
else:
img_hash = ""
add_msg = plain_text + "|-|" + img_hash
if _manage.size(group_id) == 0:
_manage.append(group_id, add_msg)
elif _manage.check(group_id, add_msg):
_manage.append(group_id, add_msg)
else:
_manage.clear(group_id)
_manage.append(group_id, add_msg)
if _manage.size(group_id) > 2:
2024-08-08 02:51:11 +08:00
if random.random() < base_config.get(
"FUDU_PROBABILITY"
2024-05-28 14:22:24 +08:00
) and not _manage.is_repeater(group_id):
if random.random() < 0.2:
2024-08-08 02:51:11 +08:00
if plain_text.startswith("打断施法"):
2024-08-10 02:25:04 +08:00
await MessageUtils.build_message("打断" + plain_text).finish()
2024-05-28 14:22:24 +08:00
else:
2024-08-10 02:25:04 +08:00
await MessageUtils.build_message("打断施法!").finish()
2024-05-28 14:22:24 +08:00
_manage.set_repeater(group_id)
rst = None
if image_list and plain_text:
2024-08-10 02:25:04 +08:00
rst = MessageUtils.build_message(
[plain_text, TEMP_PATH / f"compare_download_{group_id}_img.jpg"]
2024-05-28 14:22:24 +08:00
)
elif image_list:
2024-08-10 02:25:04 +08:00
rst = MessageUtils.build_message(
TEMP_PATH / f"compare_download_{group_id}_img.jpg"
)
2024-05-28 14:22:24 +08:00
elif plain_text:
2024-08-10 02:25:04 +08:00
rst = MessageUtils.build_message(plain_text)
2024-05-28 14:22:24 +08:00
if rst:
await rst.finish()