优化项目结构,派蒙聊天默认关闭

This commit is contained in:
CMHopeSunshine 2022-05-05 11:11:58 +08:00
parent da334ddda2
commit 8d92fbb1b2
4 changed files with 52 additions and 54 deletions

44
Paimon_Chat/__init__.py Normal file
View File

@ -0,0 +1,44 @@
from nonebot import on_regex, logger
from nonebot.exception import FinishedException
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageSegment
from ..utils.util import FreqLimiter2
from ..utils.config import config
from .chat_list import chat_list
from pathlib import Path
import random
import os
res_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'res', 'voice')
chat_lmt = FreqLimiter2(60)
def create_matcher(chat_word: str, pattern: str, cooldown: int, pro: float, responses):
hammer = on_regex(pattern, priority=10)
@hammer.handle()
async def handler(event: GroupMessageEvent):
if event.group_id not in config.paimon_chat_group:
return
if not chat_lmt.check(event.group_id, chat_word):
return
else:
if not random.random() < pro:
return
else:
try:
chat_lmt.start_cd(event.group_id, chat_word, cooldown)
response = random.choice(responses)
if '.mp3' not in response:
await hammer.finish(response)
else:
await hammer.finish(MessageSegment.record(file=Path(os.path.join(res_path, response))))
except FinishedException:
raise
except Exception as e:
logger.error('派蒙发送语音失败', e)
for k, v in chat_list.items():
create_matcher(k, v['pattern'], v['cooldown'], v['pro'], v['files'])

View File

@ -1,11 +1,3 @@
from nonebot import on_regex, logger
from nonebot.exception import FinishedException
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageSegment
from ..utils.util import FreqLimiter2
from pathlib import Path
import random
import os
chat_list = {
'确实': {'pattern': r'.*(雀食|确实).*', 'cooldown': 60, 'pro': 0.6, 'files': ['雀食', '确实']},
'坏了': {'pattern': r'.*派蒙.*坏.*', 'cooldown': 60, 'pro': 0.6, 'files': ['你才坏!', '瞎说啥呢?', '派蒙怎么可能会坏!']},
@ -32,36 +24,4 @@ chat_list = {
'过分': {'pattern': r'.*过分.*', 'cooldown': 300, 'pro': 0.5, 'files': ['好过分.mp3']},
'不明白': {'pattern': r'.*明白.*', 'cooldown': 300, 'pro': 0.5, 'files': ['不明白.mp3']},
'哪里不对': {'pattern': r'(是|对)(吧|吗)', 'cooldown': 300, 'pro': 0.5, 'files': ['哪里不对.mp3']}
}
res_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'res', 'voice')
chat_lmt = FreqLimiter2(60)
def create_matcher(chat_word: str, pattern: str, cooldown: int, pro: float, responses):
hammer = on_regex(pattern, priority=10)
@hammer.handle()
async def handler(event: GroupMessageEvent):
if not chat_lmt.check(event.group_id, chat_word):
return
else:
if not random.random() < pro:
return
else:
try:
chat_lmt.start_cd(event.group_id, chat_word, cooldown)
response = random.choice(responses)
if '.mp3' not in response:
await hammer.finish(response)
else:
await hammer.finish(MessageSegment.record(file=Path(os.path.join(res_path, response))))
except FinishedException:
raise
except Exception as e:
logger.error('派蒙发送语音失败', e)
for k, v in chat_list.items():
create_matcher(k, v['pattern'], v['cooldown'], v['pro'], v['files'])
}

View File

@ -9,7 +9,7 @@ from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, MessageEvent, Me
from ..utils.util import FreqLimiter
from ..utils.config import config
from asyncio import sleep
from .chat import *
import random
superuser = int(list(get_driver().config.superusers)[0])
@ -101,16 +101,12 @@ async def ys_pic_handler(event: Union[GroupMessageEvent, MessageEvent]):
@event_preprocessor
async def addFriend(bot: Bot, event: FriendRequestEvent):
superuser_msg = f'{event.user_id}请求添加派蒙为好友, 验证信息为:{event.comment}'
if config.paimon_add_friend == 1:
if config.paimon_add_friend:
superuser_msg += ',已自动同意'
await sleep(random.randint(2, 4))
await event.approve(bot)
await sleep(random.randint(3, 6))
await bot.send_private_msg(user_id=event.user_id, message=f'旅行者你好呀,这里是小派蒙,发送/help查看帮助哦')
elif config.paimon_add_friend == 2:
superuser_msg += ',已自动拒绝'
await sleep(random.randint(2, 4))
await event.reject(bot)
else:
superuser_msg += ',请主人自行处理哦'
await bot.send_private_msg(user_id=superuser, message=superuser_msg)
@ -121,16 +117,12 @@ async def addFriend(bot: Bot, event: GroupRequestEvent):
if event.sub_type != 'invite':
return
superuser_msg = f'{event.user_id}邀请派蒙加入群{event.group_id}'
if config.paimon_add_group == 1 or event.user_id == superuser:
if config.paimon_add_group or event.user_id == superuser:
superuser_msg += ',已自动同意'
await sleep(random.randint(2, 4))
await event.approve(bot)
await sleep(random.randint(3, 6))
await bot.send_group_msg(group_id=event.group_id, message=f'旅行者们大家好呀,这里是小派蒙,发送/help查看帮助哦')
elif config.paimon_add_group == 2:
superuser_msg += ',已自动拒绝'
await sleep(random.randint(2, 4))
await event.reject(bot)
else:
superuser_msg += ',请主人自行处理哦'
await bot.send_private_msg(user_id=superuser, message=superuser_msg)

View File

@ -1,5 +1,6 @@
from pydantic import BaseModel
from nonebot import get_driver
from typing import List
class PluginConfig(BaseModel):
@ -15,8 +16,9 @@ class PluginConfig(BaseModel):
paimon_cat_cd: int = 12
paimon_ecy_cd: int = 6
paimon_ysp_cd: int = 10
paimon_add_friend: int = 0
paimon_add_group: int = 0
paimon_add_friend: bool = False
paimon_add_group: bool = False
paimon_chat_group: List[int] = []
driver = get_driver()