新增派蒙语音、自动处理加好友和加群请求

This commit is contained in:
CMHopeSunshine 2022-05-04 14:43:21 +08:00
parent fd12f2e8ba
commit 2e2f9bbf53
45 changed files with 130 additions and 8 deletions

3
.gitignore vendored
View File

@ -148,6 +148,3 @@ nohup.out
.history .history
Paimon_exchange/* Paimon_exchange/*
user_data/gacha_log_data/* user_data/gacha_log_data/*
res/reliquaries/*
res/role_side_card/*
res/weapon/*

View File

@ -1,12 +1,17 @@
from aiohttp import ClientSession from aiohttp import ClientSession
from urllib.parse import quote from urllib.parse import quote
from nonebot import on_command
from nonebot.params import CommandArg
from typing import Union from typing import Union
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent, MessageSegment from nonebot import on_command, get_driver
from nonebot.params import CommandArg
from nonebot.message import event_preprocessor
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, MessageEvent, MessageSegment, FriendRequestEvent, \
GroupRequestEvent
from ..utils.util import FreqLimiter from ..utils.util import FreqLimiter
from ..utils.config import config from ..utils.config import config
import random from asyncio import sleep
from .chat import *
superuser = int(list(get_driver().config.superusers)[0])
duilian = on_command('对联', aliases={'对对联'}, priority=15, block=True) duilian = on_command('对联', aliases={'对对联'}, priority=15, block=True)
cat_pic = on_command('猫图', aliases={'来点猫片', '看看猫猫', '来个猫猫'}, priority=15, block=True) cat_pic = on_command('猫图', aliases={'来点猫片', '看看猫猫', '来个猫猫'}, priority=15, block=True)
@ -91,3 +96,37 @@ async def ys_pic_handler(event: Union[GroupMessageEvent, MessageEvent]):
res = await session.get(random.choice(urls)) res = await session.get(random.choice(urls))
res = await res.read() res = await res.read()
await ys_pic.finish(MessageSegment.image(res)) await ys_pic.finish(MessageSegment.image(res))
@event_preprocessor
async def addFriend(bot: Bot, event: FriendRequestEvent):
superuser_msg = f'{event.user_id}请求添加派蒙为好友, 验证信息为:{event.comment}'
if config.paimon_add_friend == 1:
superuser_msg += ',已自动同意'
await event.approve()
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 event.reject()
else:
superuser_msg += ',请主人自行处理哦'
await bot.send_private_msg(user_id=superuser, message=superuser_msg)
@event_preprocessor
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:
superuser_msg += ',已自动同意'
await event.approve()
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 event.reject()
else:
superuser_msg += ',请主人自行处理哦'
await bot.send_private_msg(user_id=superuser, message=superuser_msg)

68
Paimon_Plugins/chat.py Normal file
View File

@ -0,0 +1,68 @@
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': ['你才坏!', '瞎说啥呢?', '派蒙怎么可能会坏!']},
'诶嘿': {'pattern': r'.*(诶嘿|哎嘿|欸嘿).*', 'cooldown': 120, 'pro': 0.5, 'files': ['诶嘿.mp3', '诶嘿cn.mp3']},
'进不去': {'pattern': r'.*进不?(来|去).*', 'cooldown': 180, 'pro': 0.5, 'files': ['进不去.mp3']},
'派蒙是谁': {'pattern': r'.*(派蒙.*是(什么|谁))|((什么|谁)是?.*派蒙).*', 'cooldown': 240, 'pro': 1,
'files': ['嘿嘿你猜.mp3', '中二派蒙.mp3', '伙伴.mp3']},
'大佬': {'pattern': r'.*((大|巨)佬)|带带|帮帮|邦邦.*', 'cooldown': 240, 'pro': 0.6, 'files': ['大佬nb.mp3']},
'好色': {'pattern': r'.*好色|变态(哦|噢)?', 'cooldown': 300, 'pro': 0.5, 'files': ['好色哦.mp3', '好变态.mp3']},
'无语': {'pattern': r'.*(\.{3,9})|(\{3,9})|无语|无话可说.*', 'cooldown': 300, 'pro': 0.6, 'files': ['无语.mp3', '冷淡反应.mp3']},
'祝福': {'pattern': r'.*派蒙.*(祝福|吟唱|施法).*', 'cooldown': 360, 'pro': 1, 'files': ['来个祝福.mp3']},
'相信': {'pattern': r'.*信?不信|信我.*', 'cooldown': 360, 'pro': 0.5, 'files': ['我信你个鬼.mp3', '真的假的.mp3']},
'憨批': {'pattern': r'.*(憨批|傻(逼|B|b)).*', 'cooldown': 360, 'pro': 0.5, 'files': ['憨批.mp3']},
'可爱': {'pattern': r'.*可爱|卡哇伊', 'cooldown': 360, 'pro': 0.5, 'files': ['真是个小可爱.mp3']},
'绿茶': {'pattern': r'.*派蒙.*(giegie|绿茶).*', 'cooldown': 360, 'pro': 1, 'files': ['绿茶派.mp3']},
'不是吧': {'pattern': r'不(是|会)吧', 'cooldown': 300, 'pro': 0.5, 'files': ['不是吧阿sir.mp3']},
'好耶': {'pattern': r'好耶|太(好|棒)(了|啦)|好(\!|\)', 'cooldown': 180, 'pro': 0.75, 'files': ['好耶.mp3', '太好啦.mp3']},
'好听': {'pattern': r'(好|真|非常)?好听(\!|\)?', 'cooldown': 360, 'pro': 0.4, 'files': ['好听.mp3']},
'耽误时间': {'pattern': r'{1,4}|gkd|搞?快点|赶紧的?|(.*耽误.*时间.*)', 'cooldown': 360, 'pro': 0.6, 'files': ['耽误时间.mp3']},
'不可以': {'pattern': r'不(可以|行|能)(吧|的|噢)?', 'cooldown': 180, 'pro': 0.6, 'files': ['不可以.mp3']},
'好意思': {'pattern': r'.*好意思.*', 'cooldown': 300, 'pro': 0.5, 'files': ['好意思.mp3']},
'不要啊': {'pattern': r'不(好|要)(吧|啊)?', 'cooldown': 180, 'pro': 0.5, 'files': ['不要啊.mp3']},
'羡慕': {'pattern': r'.*羡慕.*', 'cooldown': 180, 'pro': 0.5, 'files': ['羡慕.mp3']},
'过分': {'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=16, temp=True)
@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:
print(os.path.join(res_path, response))
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

@ -11,6 +11,7 @@ nonebot2版本的小派蒙
* 来点猫片 * 来点猫片
* 来点二次元 * 来点二次元
* 来点原神壁纸 * 来点原神壁纸
* 派蒙聊天(关键词触发)
具体插件命令与hoshino版一致<br> 具体插件命令与hoshino版一致<br>
nonebot2刚上手测试时间较短如有bug请及时反馈。 nonebot2刚上手测试时间较短如有bug请及时反馈。

BIN
res/voice/不可以.mp3 Normal file

Binary file not shown.

BIN
res/voice/不明白.mp3 Normal file

Binary file not shown.

Binary file not shown.

BIN
res/voice/不要啊.mp3 Normal file

Binary file not shown.

BIN
res/voice/中二派蒙.mp3 Normal file

Binary file not shown.

BIN
res/voice/什么问题.mp3 Normal file

Binary file not shown.

BIN
res/voice/伙伴.mp3 Normal file

Binary file not shown.

BIN
res/voice/冷淡反应.mp3 Normal file

Binary file not shown.

Binary file not shown.

BIN
res/voice/勉强自己.mp3 Normal file

Binary file not shown.

BIN
res/voice/哪里不对.mp3 Normal file

Binary file not shown.

BIN
res/voice/嘿嘿你猜.mp3 Normal file

Binary file not shown.

BIN
res/voice/坏蛋.mp3 Normal file

Binary file not shown.

BIN
res/voice/大佬nb.mp3 Normal file

Binary file not shown.

BIN
res/voice/太好啦.mp3 Normal file

Binary file not shown.

BIN
res/voice/好变态.mp3 Normal file

Binary file not shown.

BIN
res/voice/好听.mp3 Normal file

Binary file not shown.

BIN
res/voice/好意思.mp3 Normal file

Binary file not shown.

BIN
res/voice/好气哦.mp3 Normal file

Binary file not shown.

BIN
res/voice/好生气.mp3 Normal file

Binary file not shown.

BIN
res/voice/好耶.mp3 Normal file

Binary file not shown.

BIN
res/voice/好色哦.mp3 Normal file

Binary file not shown.

BIN
res/voice/好过分.mp3 Normal file

Binary file not shown.

BIN
res/voice/憨批.mp3 Normal file

Binary file not shown.

Binary file not shown.

BIN
res/voice/无语.mp3 Normal file

Binary file not shown.

BIN
res/voice/来个祝福.mp3 Normal file

Binary file not shown.

BIN
res/voice/泪目.mp3 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
res/voice/真的假的.mp3 Normal file

Binary file not shown.

BIN
res/voice/给爷爬.mp3 Normal file

Binary file not shown.

BIN
res/voice/绿茶派.mp3 Normal file

Binary file not shown.

BIN
res/voice/羡慕.mp3 Normal file

Binary file not shown.

BIN
res/voice/耽误时间.mp3 Normal file

Binary file not shown.

BIN
res/voice/诶嘿.mp3 Normal file

Binary file not shown.

BIN
res/voice/诶嘿cn.mp3 Normal file

Binary file not shown.

BIN
res/voice/这个仇.mp3 Normal file

Binary file not shown.

BIN
res/voice/进不去.mp3 Normal file

Binary file not shown.

View File

@ -15,6 +15,8 @@ class PluginConfig(BaseModel):
paimon_cat_cd: int = 12 paimon_cat_cd: int = 12
paimon_ecy_cd: int = 6 paimon_ecy_cd: int = 6
paimon_ysp_cd: int = 10 paimon_ysp_cd: int = 10
paimon_add_friend: int = 0
paimon_add_group: int = 0
driver = get_driver() driver = get_driver()

View File

@ -14,7 +14,6 @@ from json import JSONDecodeError
from aiohttp import ClientSession from aiohttp import ClientSession
from nonebot import get_bot from nonebot import get_bot
from nonebot import logger from nonebot import logger
from nonebot.matcher import Matcher
from nonebot.exception import FinishedException from nonebot.exception import FinishedException
from nonebot.adapters.onebot.v11 import MessageEvent, Message from nonebot.adapters.onebot.v11 import MessageEvent, Message
from nonebot.adapters.onebot.v11.exception import ActionFailed from nonebot.adapters.onebot.v11.exception import ActionFailed
@ -103,6 +102,22 @@ class FreqLimiter:
return int(self.next_time[key] - time()) + 1 return int(self.next_time[key] - time()) + 1
# 双参数冷却时间限制器
class FreqLimiter2:
def __init__(self, default_cd_seconds: int = 60):
self.next_time = defaultdict(lambda: defaultdict(float))
self.default_cd = default_cd_seconds
def check(self, key1, key2) -> bool:
return bool(time() >= self.next_time[key1][key2])
def start_cd(self, key1, key2, cd_time=0):
self.next_time[key1][key2] = time() + (cd_time if cd_time > 0 else self.default_cd)
def left_time(self, key1, key2) -> int:
return int(self.next_time[key1][key2] - time()) + 1
# 获取可用的cookie # 获取可用的cookie
async def get_use_cookie(user_id, uid='', mys_id='', action=''): async def get_use_cookie(user_id, uid='', mys_id='', action=''):
cache_cookie = await get_cookie_cache(uid, 'uid') cache_cookie = await get_cookie_cache(uid, 'uid')