优化代码

This commit is contained in:
CMHopeSunshine 2022-04-04 16:36:23 +08:00
parent 8d6f208534
commit 4bbc07e52f
2 changed files with 54 additions and 37 deletions

View File

@ -1,21 +1,50 @@
from hoshino.typing import CQEvent, Message,CQHttpError from hoshino.typing import CQEvent, Message,CQHttpError
import hoshino import hoshino
from nonebot import scheduler from nonebot import scheduler
from hoshino import Service, R, aiorequests,util from hoshino import Service
import requests, random, os, json,datetime,re from hoshino.util import filt_message, PriFreqLimiter
import random, os, json,datetime, re
from os import path from os import path
lmt = PriFreqLimiter(30)
sv=hoshino.Service('派蒙聊天') sv=hoshino.Service('派蒙聊天')
res_dir = path.join(path.dirname(__file__), 'res') res_dir = path.join(path.dirname(__file__), 'res')
ban_word = ['禁言','ys','ssbq','十连'] # 不进行复读的关键词
word_cooldown = {'确实':{},'坏了':{},'诶嘿':{},'进不去':{},'派蒙是谁':{},'大佬':{},'好色':{},'无语':{},'祝福':{},'相信':{},'憨批':{},'可爱':{},'绿茶':{},'不是吧':{},'好耶':{},'好听':{},'耽误时间':{},'不可以':{},'好意思':{},'不要啊':{},'羡慕':{},'过分':{},'不明白':{},'哪里不对':{}} ban_word = ['禁言','ys','ssbq','十连','sy','原神']
word_cooldowntime = {'确实':60,'坏了':60,'诶嘿':180,'进不去':240,'派蒙是谁':480,'大佬':300,'好色':300,'无语':300,'祝福':480,'相信':600,'憨批':480,'可爱':480,'绿茶':600,'不是吧':300,'好耶':180,'好听':600,'耽误时间':600,'不可以':180,'好意思':300,'不要啊':180,'羡慕':180,'过分':300,'不明白':300,'哪里不对':300} # 派蒙聊天关键词的配置,第一个为冷却时间,单位秒,第二个为触发概率
word_pro = {'确实':0.6,'坏了':0.6,'诶嘿':0.5,'进不去':0.5,'派蒙是谁':1,'大佬':0.6,'好色':0.5,'无语':0.6,'祝福':1,'相信':0.5,'憨批':0.5,'可爱':0.5,'绿茶':1,'不是吧':0.5,'好耶':0.75,'好听':0.4,'耽误时间':0.6,'不可以':0.6,'好意思':0.5,'不要啊':0.5,'羡慕':0.6,'过分':0.5,'不明白':0.5,'哪里不对':0.5} word_config = {
'确实': (60, 0.6),
'坏了': (60, 0.6),
'诶嘿': (180, 0.5),
'进不去': (240, 0.5),
'派蒙是谁': (480, 1),
'大佬': (300, 0.6),
'好色': (300, 0.5),
'无语': (300, 0.6),
'祝福': (480, 1),
'相信': (600, 0.5),
'憨批': (480, 0.5),
'可爱': (480, 0.5),
'绿茶': (600, 1),
'不是吧': (300, 0.5),
'好耶': (180, 0.75),
'好听': (600, 0.4),
'耽误时间': (600, 0.6),
'不可以': (180, 0.6),
'好意思': (300, 0.5),
'不要啊': (180, 0.5),
'羡慕': (180, 0.5),
'过分': (300, 0.5),
'不明白': (300, 0.5),
'哪里不对': (300, 0.5)
}
PROB_A = 1.6 PROB_A = 1.6
group_stat = {} group_stat = {}
check_rep = {} check_rep = {}
@sv.on_message() @sv.on_message()
async def random_repeater(bot, ev: CQEvent): async def random_repeater(bot, ev: CQEvent):
if ev.message_type == 'group': if ev.message_type == 'group':
@ -33,13 +62,13 @@ async def random_repeater(bot, ev: CQEvent):
check_rep[gid] = False check_rep[gid] = False
last_msg, is_repeated, p = group_stat[gid] last_msg, is_repeated, p = group_stat[gid]
if last_msg == msg and msg not in ban_word: # 群友正在复读 if last_msg == msg and not any(msg in w for w in ban_word): # 群友正在复读
check_rep[gid] = True check_rep[gid] = True
if not is_repeated: # 机器人尚未复读过,开始测试复读 if not is_repeated: # 机器人尚未复读过,开始测试复读
if random.random() < p: # 概率测试通过复读并设flag if random.random() < p: # 概率测试通过复读并设flag
try: try:
group_stat[gid] = (msg, True, 0) group_stat[gid] = (msg, True, 0)
await bot.send(ev, util.filt_message(ev.message)) await bot.send(ev, filt_message(ev.message))
except CQHttpError as e: except CQHttpError as e:
hoshino.logger.error(f'复读失败: {type(e)}') hoshino.logger.error(f'复读失败: {type(e)}')
hoshino.logger.exception(e) hoshino.logger.exception(e)
@ -60,10 +89,13 @@ async def paimonchat(bot, ev:CQEvent):
else: else:
gid = str(ev.user_id) gid = str(ev.user_id)
msg = str(ev.message) msg = str(ev.message)
# 如果当前在判断复读,则不触发语音聊天
if gid not in check_rep: if gid not in check_rep:
check_rep[gid] = False check_rep[gid] = False
if check_rep[gid]: if check_rep[gid]:
return return
if re.match(r'.*派蒙.*坏.*',msg): if re.match(r'.*派蒙.*坏.*',msg):
word = '坏了' word = '坏了'
reply = random.choice(['你才坏!','瞎说啥呢?','派蒙怎么可能会坏!']) + '[CQ:face,id=146]' reply = random.choice(['你才坏!','瞎说啥呢?','派蒙怎么可能会坏!']) + '[CQ:face,id=146]'
@ -82,7 +114,7 @@ async def paimonchat(bot, ev:CQEvent):
word = '派蒙是谁' word = '派蒙是谁'
path = res_dir + random.choice(['/嘿嘿你猜.mp3','/中二派蒙.mp3','/伙伴.mp3']) path = res_dir + random.choice(['/嘿嘿你猜.mp3','/中二派蒙.mp3','/伙伴.mp3'])
reply = f'[CQ:record,file=file:///{path}]' reply = f'[CQ:record,file=file:///{path}]'
elif re.match(r'.*((大|巨)?佬)|带带|帮帮|邦邦.*',msg): elif re.match(r'.*((大|巨)佬)|带带|帮帮|邦邦.*',msg):
word = '大佬' word = '大佬'
path = res_dir + '/大佬nb.mp3' path = res_dir + '/大佬nb.mp3'
reply = f'[CQ:record,file=file:///{path}]' reply = f'[CQ:record,file=file:///{path}]'
@ -160,25 +192,11 @@ async def paimonchat(bot, ev:CQEvent):
reply = f'[CQ:record,file=file:///{path}]' reply = f'[CQ:record,file=file:///{path}]'
else: else:
return return
if gid not in word_cooldown[word]: if not lmt.check(gid,word): # 判断是否在冷却
word_cooldown[word][gid] = True return
if ev.group == 914302664: elif random.random() < word_config[word][1]: # 判断概率是否触发
p = word_pro[word]/2
else:
p = word_pro[word]
if word_cooldown[word][gid] and random.random() < p:
try: try:
await bot.send(ev,reply) await bot.send(ev,reply)
word_cooldown[word][gid] = False lmt.start_cd(gid, word, word_config[word][0]) # 开始冷却
end_time = datetime.datetime.now() + datetime.timedelta(seconds=word_cooldowntime[word])#冷却时间
scheduler.add_job(
resetTime,
'date',
args=(word,gid,),
run_date=end_time
)
except: except:
hoshino.logger.error('派蒙聊天失败') hoshino.logger.error('派蒙聊天语音发送失败请检查是否已安装ffmpeg')
def resetTime(word,gid):
word_cooldown[word][gid] = True

View File

@ -5,10 +5,13 @@ from os import path
from PIL import Image from PIL import Image
from hoshino import Service, aiorequests from hoshino import Service, aiorequests
from hoshino.typing import HoshinoBot, CQEvent from hoshino.typing import CQEvent
from hoshino.util import FreqLimiter
from .data_source import * from .data_source import *
from ._res import Res as R from ._res import Res as R
lmt = FreqLimiter(20)
HELP_MSG = ''' HELP_MSG = '''
头像相关表情包制作 头像相关表情包制作
摸摸|亲亲|吃掉|贴贴|拍拍|给爷爬|撕掉|精神支柱|扔掉|要我一直+@人/qq号/图片 摸摸|亲亲|吃掉|贴贴|拍拍|给爷爬|撕掉|精神支柱|扔掉|要我一直+@人/qq号/图片
@ -17,8 +20,6 @@ HELP_MSG = '''
sv = Service('表情包', bundle='娱乐', help_=HELP_MSG) sv = Service('表情包', bundle='娱乐', help_=HELP_MSG)
data_dir = path.join(path.dirname(__file__), 'resources') data_dir = path.join(path.dirname(__file__), 'resources')
last_check = {}
cool_down = datetime.timedelta(seconds=20)
@sv.on_rex(r'^#(rua|摸摸|亲亲|吃掉|贴贴|拍拍|给爷爬|撕掉|精神支柱|扔掉|要我一直)(?P<name>.*?)$') @sv.on_rex(r'^#(rua|摸摸|亲亲|吃掉|贴贴|拍拍|给爷爬|撕掉|精神支柱|扔掉|要我一直)(?P<name>.*?)$')
async def main(bot,ev): async def main(bot,ev):
@ -83,11 +84,9 @@ async def poke_back(session):
uid = session.ctx['user_id'] uid = session.ctx['user_id']
tid = session.ctx['target_id'] tid = session.ctx['target_id']
gid = session.ctx['group_id'] gid = session.ctx['group_id']
if str(gid) in last_check: if not lmt.check(gid):
intervals = datetime.datetime.now() - last_check[str(gid)] return
if intervals < cool_down: if random.random() <= 0.5:
return
if random.random() <= 0.4:
if tid == session.ctx['self_id']: if tid == session.ctx['self_id']:
if random.random() <= 0.5: if random.random() <= 0.5:
path = data_dir + random.choice(['/这个仇.mp3','/好生气.mp3','/好气哦.mp3','/好变态.mp3','/坏蛋.mp3','/不要啊.mp3','/好过分.mp3']) path = data_dir + random.choice(['/这个仇.mp3','/好生气.mp3','/好气哦.mp3','/好变态.mp3','/坏蛋.mp3','/不要啊.mp3','/好过分.mp3'])
@ -103,7 +102,7 @@ async def poke_back(session):
head = await get_avatar(str(tid)) head = await get_avatar(str(tid))
res = await random.choice(data_source.avatarFunList2)(data_dir,head) res = await random.choice(data_source.avatarFunList2)(data_dir,head)
await session.send(R.image(res)) await session.send(R.image(res))
last_check[str(gid)] = datetime.datetime.now() lmt.start_cd(gid, 20)
async def get_avatar(qq): async def get_avatar(qq):