mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-03 19:00:00 +08:00
update 0.0.6.2
This commit is contained in:
Regular → Executable
-2
@@ -24,8 +24,6 @@ __plugin_version__ = 0.1
|
||||
__plugin_author__ = 'HibiKier'
|
||||
__plugin_settings__ = {
|
||||
"level": 5,
|
||||
"default_status": True,
|
||||
"limit_superuser": False,
|
||||
"cmd": ["Ai", "ai", "AI", "aI"],
|
||||
}
|
||||
__plugin_configs__ = {
|
||||
|
||||
Regular → Executable
+65
-65
@@ -1,13 +1,12 @@
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
|
||||
import aiohttp
|
||||
from aiohttp.client import ClientSession
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from configs.path_config import IMAGE_PATH, DATA_PATH
|
||||
from services.log import logger
|
||||
from utils.message_builder import image, face
|
||||
from configs.config import Config, NICKNAME
|
||||
from .utils import ai_message_manager
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
@@ -26,7 +25,7 @@ anime_data = json.load(open(DATA_PATH + "anime.json", "r", encoding="utf8"))
|
||||
|
||||
async def get_chat_result(text: str, img_url: str, user_id: int, nickname: str) -> str:
|
||||
"""
|
||||
获取 AI 返回值,顺序:图灵 -> 青云客
|
||||
获取 AI 返回值,顺序: 特殊回复 -> 图灵 -> 青云客
|
||||
:param text: 问题
|
||||
:param img_url: 图片链接
|
||||
:param user_id: 用户id
|
||||
@@ -34,6 +33,11 @@ async def get_chat_result(text: str, img_url: str, user_id: int, nickname: str)
|
||||
:return: 回答
|
||||
"""
|
||||
global index
|
||||
ai_message_manager.add_message(user_id, text)
|
||||
special_rst = await ai_message_manager.get_result(user_id, nickname)
|
||||
if special_rst:
|
||||
ai_message_manager.add_result(user_id, special_rst)
|
||||
return special_rst
|
||||
if index == 5:
|
||||
index = 0
|
||||
if len(text) < 6 and random.random() < 0.6:
|
||||
@@ -41,10 +45,9 @@ async def get_chat_result(text: str, img_url: str, user_id: int, nickname: str)
|
||||
for key in keys:
|
||||
if text.find(key) != -1:
|
||||
return random.choice(anime_data[key]).replace("你", nickname)
|
||||
async with aiohttp.ClientSession() as sess:
|
||||
rst = await tu_ling(text, img_url, user_id, sess)
|
||||
if not rst:
|
||||
rst = await xie_ai(text, sess)
|
||||
rst = await tu_ling(text, img_url, user_id)
|
||||
if not rst:
|
||||
rst = await xie_ai(text)
|
||||
if not rst:
|
||||
return no_result()
|
||||
if nickname:
|
||||
@@ -55,23 +58,25 @@ async def get_chat_result(text: str, img_url: str, user_id: int, nickname: str)
|
||||
if nickname.find("大人") == -1:
|
||||
nickname += "大~人~"
|
||||
rst = rst.replace("小主人", nickname).replace("小朋友", nickname)
|
||||
ai_message_manager.add_result(user_id, rst)
|
||||
print(ai_message_manager)
|
||||
return rst
|
||||
|
||||
|
||||
# 图灵接口
|
||||
async def tu_ling(text: str, img_url: str, user_id: int, sess: ClientSession) -> str:
|
||||
async def tu_ling(text: str, img_url: str, user_id: int) -> str:
|
||||
"""
|
||||
获取图灵接口的回复
|
||||
:param text: 问题
|
||||
:param img_url: 图片链接
|
||||
:param user_id: 用户id
|
||||
:param sess: AIOHTTP SESSION
|
||||
:return: 图灵回复
|
||||
"""
|
||||
global index
|
||||
TL_KEY = Config.get_config("ai", "TL_KEY")
|
||||
req = None
|
||||
if not TL_KEY:
|
||||
return ''
|
||||
return ""
|
||||
try:
|
||||
if text:
|
||||
req = {
|
||||
@@ -98,62 +103,59 @@ async def tu_ling(text: str, img_url: str, user_id: int, sess: ClientSession) ->
|
||||
index = 0
|
||||
return ""
|
||||
text = ""
|
||||
async with sess.post(url, json=req) as response:
|
||||
if response.status != 200:
|
||||
return no_result()
|
||||
resp_payload = json.loads(await response.text())
|
||||
if int(resp_payload["intent"]["code"]) in [4003]:
|
||||
return ""
|
||||
if resp_payload["results"]:
|
||||
for result in resp_payload["results"]:
|
||||
if result["resultType"] == "text":
|
||||
text = result["values"]["text"]
|
||||
if "请求次数超过" in text:
|
||||
text = ""
|
||||
response = await AsyncHttpx.post(url, json=req)
|
||||
if response.status_code != 200:
|
||||
return no_result()
|
||||
resp_payload = json.loads(response.text)
|
||||
if int(resp_payload["intent"]["code"]) in [4003]:
|
||||
return ""
|
||||
if resp_payload["results"]:
|
||||
for result in resp_payload["results"]:
|
||||
if result["resultType"] == "text":
|
||||
text = result["values"]["text"]
|
||||
if "请求次数超过" in text:
|
||||
text = ""
|
||||
return text
|
||||
|
||||
|
||||
# 屑 AI
|
||||
async def xie_ai(text: str, sess: ClientSession) -> str:
|
||||
async def xie_ai(text: str) -> str:
|
||||
"""
|
||||
获取青云客回复
|
||||
:param text: 问题
|
||||
:param sess: AIOHTTP SESSION
|
||||
:return: 青云可回复
|
||||
"""
|
||||
async with sess.get(
|
||||
f"http://api.qingyunke.com/api.php?key=free&appid=0&msg={text}"
|
||||
) as res:
|
||||
content = ""
|
||||
data = json.loads(await res.text())
|
||||
if data["result"] == 0:
|
||||
content = data["content"]
|
||||
if "菲菲" in content:
|
||||
content = content.replace("菲菲", NICKNAME)
|
||||
if "艳儿" in content:
|
||||
content = content.replace("艳儿", NICKNAME)
|
||||
if "公众号" in content:
|
||||
content = ""
|
||||
if "{br}" in content:
|
||||
content = content.replace("{br}", "\n")
|
||||
if "提示" in content:
|
||||
content = content[: content.find("提示")]
|
||||
if "淘宝" in content:
|
||||
return ""
|
||||
while True:
|
||||
r = re.search("{face:(.*)}", content)
|
||||
if r:
|
||||
id_ = r.group(1)
|
||||
content = content.replace(
|
||||
"{" + f"face:{id_}" + "}", str(face(int(id_)))
|
||||
)
|
||||
else:
|
||||
break
|
||||
return (
|
||||
content
|
||||
if not content and not Config.get_config("ai", "ALAPI_AI_CHECK")
|
||||
else await check_text(content, sess)
|
||||
)
|
||||
res = await AsyncHttpx.get(f"http://api.qingyunke.com/api.php?key=free&appid=0&msg={text}")
|
||||
content = ""
|
||||
data = json.loads(res.text)
|
||||
if data["result"] == 0:
|
||||
content = data["content"]
|
||||
if "菲菲" in content:
|
||||
content = content.replace("菲菲", NICKNAME)
|
||||
if "艳儿" in content:
|
||||
content = content.replace("艳儿", NICKNAME)
|
||||
if "公众号" in content:
|
||||
content = ""
|
||||
if "{br}" in content:
|
||||
content = content.replace("{br}", "\n")
|
||||
if "提示" in content:
|
||||
content = content[: content.find("提示")]
|
||||
if "淘宝" in content:
|
||||
return ""
|
||||
while True:
|
||||
r = re.search("{face:(.*)}", content)
|
||||
if r:
|
||||
id_ = r.group(1)
|
||||
content = content.replace(
|
||||
"{" + f"face:{id_}" + "}", str(face(int(id_)))
|
||||
)
|
||||
else:
|
||||
break
|
||||
return (
|
||||
content
|
||||
if not content and not Config.get_config("ai", "ALAPI_AI_CHECK")
|
||||
else await check_text(content)
|
||||
)
|
||||
|
||||
|
||||
def hello() -> str:
|
||||
@@ -196,21 +198,19 @@ def no_result() -> str:
|
||||
)
|
||||
|
||||
|
||||
async def check_text(text: str, sess: ClientSession) -> str:
|
||||
async def check_text(text: str) -> str:
|
||||
"""
|
||||
ALAPI文本检测,主要针对青云客API,检测为恶俗文本改为无回复的回答
|
||||
:param text: 回复
|
||||
:param sess: AIOHTTP SESSION
|
||||
"""
|
||||
if not Config.get_config("alapi", "ALAPI_TOKEN"):
|
||||
return text
|
||||
params = {"token": Config.get_config("alapi", "ALAPI_TOKEN"), "text": text}
|
||||
try:
|
||||
async with sess.get(check_url, timeout=2, params=params) as response:
|
||||
data = await response.json()
|
||||
if data["code"] == 200:
|
||||
if data["data"]["conclusion_type"] == 2:
|
||||
return ""
|
||||
data = (await AsyncHttpx.get(check_url, timeout=2, params=params)).json()
|
||||
if data["code"] == 200:
|
||||
if data["data"]["conclusion_type"] == 2:
|
||||
return ""
|
||||
except Exception as e:
|
||||
logger.error(f"检测违规文本错误...{type(e)}:{e}")
|
||||
return text
|
||||
|
||||
Executable
+137
@@ -0,0 +1,137 @@
|
||||
from utils.manager import StaticData
|
||||
from configs.config import NICKNAME
|
||||
from models.ban_user import BanUser
|
||||
from typing import Optional
|
||||
import random
|
||||
import time
|
||||
|
||||
|
||||
class AiMessageManager(StaticData):
|
||||
def __init__(self):
|
||||
super().__init__(None)
|
||||
self._same_message = [
|
||||
"为什么要发一样的话?",
|
||||
"请不要再重复对我说一句话了,不然我就要生气了!",
|
||||
"别再发这句话了,我已经知道了...",
|
||||
"你是只会说这一句话吗?",
|
||||
"[*],你发我也发!",
|
||||
"[uname],[*]",
|
||||
f"救命!有笨蛋一直给{NICKNAME}发一样的话!",
|
||||
"这句话你已经给我发了{}次了,再发就生气!",
|
||||
]
|
||||
self._repeat_message = [
|
||||
f"请不要学{NICKNAME}说话",
|
||||
f"为什么要一直学{NICKNAME}说话?",
|
||||
"你再学!你再学我就生气了!",
|
||||
f"呜呜,你是想欺负{NICKNAME}嘛..",
|
||||
"[uname]不要再学我说话了!",
|
||||
"再学我说话,我就把你拉进黑名单(生气",
|
||||
"你再学![uname]是个笨蛋!",
|
||||
"你已经学我说话{}次了!别再学了!",
|
||||
]
|
||||
|
||||
def add_message(self, user_id: int, message: str):
|
||||
"""
|
||||
添加用户消息
|
||||
:param user_id: 用户id
|
||||
:param message: 消息内容
|
||||
"""
|
||||
if message:
|
||||
if self._data.get(user_id) is None:
|
||||
self._data[user_id] = {
|
||||
"time": time.time(),
|
||||
"message": [],
|
||||
"result": [],
|
||||
"repeat_count": 0,
|
||||
}
|
||||
if time.time() - self._data[user_id]["time"] > 60 * 10:
|
||||
self._data[user_id]["message"].clear()
|
||||
self._data[user_id]["time"] = time.time()
|
||||
self._data[user_id]["message"].append(message.strip())
|
||||
|
||||
def add_result(self, user_id: int, message: str):
|
||||
"""
|
||||
添加回复用户的消息
|
||||
:param user_id: 用户id
|
||||
:param message: 回复消息内容
|
||||
"""
|
||||
if message:
|
||||
if self._data.get(user_id) is None:
|
||||
self._data[user_id] = {
|
||||
"time": time.time(),
|
||||
"message": [],
|
||||
"result": [],
|
||||
"repeat_count": 0,
|
||||
}
|
||||
if time.time() - self._data[user_id]["time"] > 60 * 10:
|
||||
self._data[user_id]["result"].clear()
|
||||
self._data[user_id]["repeat_count"] = 0
|
||||
self._data[user_id]["time"] = time.time()
|
||||
self._data[user_id]["result"].append(message.strip())
|
||||
|
||||
async def get_result(self, user_id: int, nickname: str) -> Optional[str]:
|
||||
"""
|
||||
特殊消息特殊回复
|
||||
:param user_id: 用户id
|
||||
:param nickname: 用户昵称
|
||||
"""
|
||||
if len(self._data[user_id]["message"]) < 2:
|
||||
return None
|
||||
msg = await self._get_user_repeat_message_result(user_id)
|
||||
if not msg:
|
||||
msg = await self._get_user_same_message_result(user_id)
|
||||
if msg:
|
||||
if "[uname]" in msg:
|
||||
msg = msg.replace("[uname]", nickname)
|
||||
if not msg.startswith("生气了!你好烦,闭嘴!") and "[*]" in msg:
|
||||
msg = msg.replace("[*]", self._data[user_id]["message"][-1])
|
||||
return msg
|
||||
|
||||
async def _get_user_same_message_result(self, user_id: int) -> Optional[str]:
|
||||
"""
|
||||
重复消息回复
|
||||
:param user_id: 用户id
|
||||
"""
|
||||
msg = self._data[user_id]["message"][-1]
|
||||
cnt = 0
|
||||
_tmp = self._data[user_id]["message"][:-1]
|
||||
_tmp.reverse()
|
||||
for s in _tmp:
|
||||
if s == msg:
|
||||
cnt += 1
|
||||
else:
|
||||
break
|
||||
if cnt > 1:
|
||||
if random.random() < 0.5 and cnt > 3:
|
||||
rand = random.randint(60, 300)
|
||||
await BanUser.ban(user_id, 9, rand)
|
||||
self._data[user_id]["message"].clear()
|
||||
return f"生气了!你好烦,闭嘴!给我老实安静{rand}秒"
|
||||
return random.choice(self._same_message).format(cnt)
|
||||
return None
|
||||
|
||||
async def _get_user_repeat_message_result(self, user_id: int) -> Optional[str]:
|
||||
"""
|
||||
复读真寻的消息回复
|
||||
:param user_id: 用户id
|
||||
"""
|
||||
msg = self._data[user_id]["message"][-1]
|
||||
if self._data[user_id]["result"]:
|
||||
rst = self._data[user_id]["result"][-1]
|
||||
else:
|
||||
return None
|
||||
if msg == rst:
|
||||
self._data[user_id]["repeat_count"] += 1
|
||||
cnt = self._data[user_id]["repeat_count"]
|
||||
if cnt > 1:
|
||||
if random.random() < 0.5 and cnt > 3:
|
||||
rand = random.randint(60, 300)
|
||||
await BanUser.ban(user_id, 9, rand)
|
||||
self._data[user_id]["result"].clear()
|
||||
self._data[user_id]["repeat_count"] = 0
|
||||
return f"生气了!你好烦,闭嘴!给我老实安静{rand}秒"
|
||||
return random.choice(self._repeat_message).format(cnt)
|
||||
return None
|
||||
|
||||
|
||||
ai_message_manager = AiMessageManager()
|
||||
Reference in New Issue
Block a user