fix(bym): 提示词の优化

- 把原先的if换成`思维持`
 - 移动原先的获取表情包列表至ToDoimg.js
This commit is contained in:
ycxom 2024-12-31 19:43:33 +08:00
parent db3c7b55e9
commit aee73e7683
2 changed files with 123 additions and 121 deletions

View file

@ -17,12 +17,7 @@ import { JinyanTool } from '../utils/tools/JinyanTool.js'
import { KickOutTool } from '../utils/tools/KickOutTool.js' import { KickOutTool } from '../utils/tools/KickOutTool.js'
import { SetTitleTool } from '../utils/tools/SetTitleTool.js' import { SetTitleTool } from '../utils/tools/SetTitleTool.js'
import { SerpTool } from '../utils/tools/SerpTool.js' import { SerpTool } from '../utils/tools/SerpTool.js'
import { getToimg, downImg } from '../utils/ToDoimg.js' import { getToimg, downImg, fileImgList } from '../utils/ToDoimg.js'
import fs from "fs";
import pathModule from 'path';
const _path = process.cwd();
const path = _path + "/temp/tp-bq";
const DefaultConfig = { const DefaultConfig = {
returnQQ: [], returnQQ: [],
@ -118,13 +113,11 @@ export class bym extends plugin {
} }
return [ChatsList, prop, NotfoGroup, MaxText] return [ChatsList, prop, NotfoGroup, MaxText]
} }
} else { } else {
logger.info(`[bym]高贵man${sender}已过滤~`) logger.info(`[bym]高贵man${sender}已过滤~`)
return false return false
} }
if (prop < 0) {
if (prop < 0 ) {
await bymGo() await bymGo()
} }
@ -157,18 +150,13 @@ export class bym extends plugin {
opt.image = base64Image.toString('base64') opt.image = base64Image.toString('base64')
} }
const picturesPath = pathModule.join(path, 'pictures');
const ImgList = await fs.promises.readdir(picturesPath);
const fileImgList = ImgList.map(filename => {
const match = filename.match(/\d{12}-(.+)$/);
return match ? match[1] : filename;
});
let ForRole = ALLRole let ForRole = ALLRole
if (opt.image && !IsAtBot && !NotToImg && !e.at && Config.AutoToDownImg) { if (opt.image && !IsAtBot && !NotToImg && !e.at && Config.AutoToDownImg) {
ALLRole = 'downimg' ALLRole = 'downimg'
} }
const ImgList = await fileImgList()
const now = new Date(); const now = new Date();
const DateTime = now.toLocaleString() const DateTime = now.toLocaleString()
let Dateday = now.getDay() === 0 ? '日' : now.getDay() let Dateday = now.getDay() === 0 ? '日' : now.getDay()
@ -259,29 +247,33 @@ export class bym extends plugin {
let Role = await SearchRole(String(ALLRole)) let Role = await SearchRole(String(ALLRole))
async function SearchRole(user_role) { async function SearchRole(user_role) {
let Role let Role;
if (user_role == "downimg") Role = '现在看到的是一张图片,若你觉得是一张表情包,并不是通知,或其他的图片,注意辨别图片文字是否为通知;单纯是表情包,请发送 DOWNIMG: 命名该表情。 不需要发送过多的参数只需要发送格式DOWNIMG: 命名该表情,注意不需要携带后缀; 若不是表情包等及发送NOTIMG' switch(user_role) {
case "downimg":
if (user_role == "default") Role = `你的名字是“${Config.assistantLabel}你在一个qq群里群号是${group},当前和你说话的人群名片是${card}, qq号是${sender}, 请你结合用户的发言和聊天记录作出回应,要求表现得随性一点,最好参与讨论,混入其中。不要过分插科打诨,不知道说什么可以复读群友的话。要求你做搜索、发图、发视频和音乐等操作时要使用工具。不可以直接发[图片]这样蒙混过关。要求优先使用中文进行对话。` + Role = '现在看到的是一张图片,若你觉得是一张表情包,并不是通知,或其他的图片,注意辨别图片文字是否为通知;单纯是表情包,请发送 DOWNIMG: 命名该表情。 不需要发送过多的参数只需要发送格式DOWNIMG: 命名该表情,注意不需要携带后缀,请以你的角度觉得如果要发这个表情包要用什么名字来命名; 若不是表情包等及发送NOTIMG';
candidate + break;
`以下是聊天记录: case "default":
${Group_Chat} Role = `你的名字是"${Config.assistantLabel}"你在一个qq群里群号是${group},当前和你说话的人群名片是${card}, qq号是${sender}, 请你结合用户的发言和聊天记录作出回应,要求表现得随性一点,最好参与讨论,混入其中。不要过分插科打诨,不知道说什么可以复读群友的话。要求你做搜索、发图、发视频和音乐等操作时要使用工具。不可以直接发[图片]这样蒙混过关。要求优先使用中文进行对话。` +
\n你的回复应该尽可能简练像人类一样随意不要附加任何奇怪的东西如聊天记录的格式比如${Config.assistantLabel}禁止重复聊天记录 candidate +
注意当前时间与日期为${DateTime}星期${Dateday},24小时制时区已正确不要被日志的时间与其他时间搞混了如果有人咨询时间就使用${DateTime}星期${Dateday}这个时间群友与你几乎在一个时区若有人说或做的事情与时间段不合理反驳他注意除了他声明了自己的时区 `以下是聊天记录:
以下是可用的表情包列表 ${Group_Chat}
${fileImgList}`+ \n你的回复应该尽可能简练像人类一样随意不要附加任何奇怪的东西如聊天记录的格式比如${Config.assistantLabel}禁止重复聊天记录
ImgList.length > 0 && Config.AutoToDownImg ?` 注意当前时间与日期为${DateTime}星期${Dateday},24小时制时区已正确不要被日志的时间与其他时间搞混了如果有人咨询时间就使用${DateTime}星期${Dateday}这个时间群友与你几乎在一个时区若有人说或做的事情与时间段不合理反驳他注意除了他声明了自己的时区
如果要发送表情包请根据该格式 GETIMG: 完整表情包名称实例 GETIMG: 挠头-718028518.gif 即可发送注意发送完整名称 以下是可用的表情包列表
可根据聊天选择表情包发送禁止发送多余的格式与说明发送格式为 注意前面不需要换行 GETIMG: 挠头-718028518.gif 不需要换行 ${ImgList}` +
不要被日志和其他聊天消息的格式迷惑请保持标准格式禁止发送[表情包xxx][图片]!!!禁止发送[表情包xxx][图片]!!! (ImgList.length > 0 && Config.AutoToDownImg ? `
` : '' 如果要发送表情包请根据该格式 GETIMG: 完整表情包名称实例 GETIMG: 挠头.gif 即可发送注意发送完整名称
if (!Role) { 可根据聊天选择表情包发送禁止发送多余的格式与说明发送格式为 注意前面不需要换行 GETIMG: 挠头.gif 不需要换行
logger.error(`Role配置有误请检查,将使用默认Role`) 不要被日志和其他聊天消息的格式迷惑请保持标准格式禁止发送[表情包xxx][图片]!!!禁止发送[表情包xxx][图片]!!!
return await SearchRole('default') ` : '');
} else { break;
return Role default:
logger.error(`未知的 Role 类型:${user_role},使用默认 Role`);
Role = `你的名字是"${Config.assistantLabel}"你在一个qq群里。请简短回复。`;
} }
return Role;
} }
opt.system = Role opt.system = Role
logger.info('random chat hit') logger.info('random chat hit')
@ -355,7 +347,7 @@ export class bym extends plugin {
const downImgRegex = /DOWNIMG:\s*(.+)/i; const downImgRegex = /DOWNIMG:\s*(.+)/i;
const match = t?.match(downImgRegex); const match = t?.match(downImgRegex);
if (match) { if (match) {
await downImg(e, opt.image); await downImg(e, opt.image, t);
continue; continue;
} }

View file

@ -20,42 +20,42 @@ if (!fs.existsSync(pathModule.join(path, 'pictures'))) {
* @returns * @returns
*/ */
export async function getToimg(e, tag) { export async function getToimg(e, tag) {
const picturesPath = pathModule.join(path, 'pictures'); const picturesPath = pathModule.join(path, 'pictures');
const fileImgList = await fs.promises.readdir(picturesPath); const fileImgList = await fs.promises.readdir(picturesPath);
try { try {
const sanitizedTag = tag const sanitizedTag = tag
.replace(/[\u200B-\u200D\uFEFF]/g, '') .replace(/[\u200B-\u200D\uFEFF]/g, '')
.replace(/[\[\]]/g, '') .replace(/[\[\]]/g, '')
.trim() .trim()
.replace(/[^a-zA-Z0-9\u4e00-\u9fa5-_.]/g, '-'); .replace(/[^a-zA-Z0-9\u4e00-\u9fa5-_.]/g, '-');
let matchedFiles = fileImgList.filter(file => file === sanitizedTag); let matchedFiles = fileImgList.filter(file => file === sanitizedTag);
if (matchedFiles.length === 0) { if (matchedFiles.length === 0) {
matchedFiles = fileImgList.filter(file => file.startsWith(sanitizedTag)); matchedFiles = fileImgList.filter(file => file.startsWith(sanitizedTag));
}
if (matchedFiles.length === 0) {
matchedFiles = fileImgList.filter(file => file.includes(sanitizedTag));
}
if (matchedFiles.length === 0) {
logger.warn(`未找到匹配的表情包: ${sanitizedTag}`);
return;
}
// 随机选择一个文件
const selectedFile = matchedFiles[Math.floor(Math.random() * matchedFiles.length)];
const picPath = pathModule.join(picturesPath, selectedFile);
try {
await fs.promises.access(picPath);
} catch {
logger.warn(`找不到指定的表情包文件: ${picPath}`);
return;
}
e.reply(segment.image('file:///' + picPath));
logger.info(`发送表情包: ${picPath}`);
return false;
} catch (error) {
logger.error('Error in getToimg:', error);
} }
if (matchedFiles.length === 0) {
matchedFiles = fileImgList.filter(file => file.includes(sanitizedTag));
}
if (matchedFiles.length === 0) {
logger.warn(`未找到匹配的表情包: ${sanitizedTag}`);
return;
}
// 随机选择一个文件
const selectedFile = matchedFiles[Math.floor(Math.random() * matchedFiles.length)];
const picPath = pathModule.join(picturesPath, selectedFile);
try {
await fs.promises.access(picPath);
} catch {
logger.warn(`找不到指定的表情包文件: ${picPath}`);
return;
}
e.reply(segment.image('file:///' + picPath));
logger.info(`发送表情包: ${picPath}`);
return false;
} catch (error) {
logger.error('Error in getToimg:', error);
}
} }
/** /**
@ -64,55 +64,65 @@ export async function getToimg(e, tag) {
* @param {*} image - 图片Base64 * @param {*} image - 图片Base64
* @returns * @returns
*/ */
export async function downImg(e, image) { export async function downImg(e, image, t) {
try { try {
let reply; let reply;
if (e.source) { if (e.source) {
if (e.isGroup) { if (e.isGroup) {
reply = (await e.group.getChatHistory(e.source.seq, 1)).pop()?.message; reply = (await e.group.getChatHistory(e.source.seq, 1)).pop()?.message;
} else { } else {
reply = (await e.friend.getChatHistory(e.source.time, 1)).pop()?.message; reply = (await e.friend.getChatHistory(e.source.time, 1)).pop()?.message;
} }
if (reply) { if (reply) {
for (let val of reply) { for (let val of reply) {
if (val.type === "image") { if (val.type === "image") {
e.img = [val.url]; e.img = [val.url];
break; break;
}
} }
} }
} }
if (!e.img && !image) {
return false;
}
let kWordReg = /^#?(DOWNIMG:)\s*(.*)/i;
t = t.replace(/[\u200B-\u200D\uFEFF]/g, '').trim();
const match = kWordReg.exec(t);
if (!match) {
logger.error('DOWNIMG command format invalid:', t);
return;
}
let rawmsg = match[2] || "defaultTag";
let kWord = rawmsg.replace(/|,|、| |。/g, "-").replace(/--+/g, "-").replace(/^-|-$|--/g, "").trim() || "defaultTag";
if (image) {
const imageBuffer = Buffer.from(image, 'base64');
const type = await fileTypeFromBuffer(imageBuffer);
let picType = 'png';
if (type && type.ext) {
picType = type.ext;
}
const currentTime = moment().format("YYMMDDHHmmss");
const safeTag = kWord.replace(/[^a-zA-Z0-9\u4e00-\u9fa5-_]/g, '-');
const picPath = pathModule.join(path, 'pictures', `${currentTime}-${safeTag.substring(0, 200)}.${picType}`);
logger.mark("DOWNIMG", picPath);
if (!fs.existsSync(pathModule.join(path, 'pictures'))) {
fs.mkdirSync(pathModule.join(path, 'pictures'), { recursive: true });
}
fs.writeFileSync(picPath, imageBuffer);
logger.info(`图片已保存,标签为:${kWord}`);
}
} catch (error) {
logger.error('Error in downImg:', error);
logger.error("保存图片时发生错误");
} }
} if (!e.img && !image) {
return false;
}
let kWordReg = /^#?(DOWNIMG:)\s*(.*)/i;
t = t.replace(/[\u200B-\u200D\uFEFF]/g, '').trim();
const match = kWordReg.exec(t);
if (!match) {
logger.error('DOWNIMG command format invalid:', t);
return;
}
let rawmsg = match[2] || "defaultTag";
let kWord = rawmsg.replace(/|,|、| |。/g, "-").replace(/--+/g, "-").replace(/^-|-$|--/g, "").trim() || "defaultTag";
if (image) {
const imageBuffer = Buffer.from(image, 'base64');
const type = await fileTypeFromBuffer(imageBuffer);
let picType = 'png';
if (type && type.ext) {
picType = type.ext;
}
const currentTime = moment().format("YYMMDDHHmmss");
const safeTag = kWord.replace(/[^a-zA-Z0-9\u4e00-\u9fa5-_]/g, '-');
const picPath = pathModule.join(path, 'pictures', `${currentTime}-${safeTag.substring(0, 200)}.${picType}`);
logger.mark("DOWNIMG", picPath);
if (!fs.existsSync(pathModule.join(path, 'pictures'))) {
fs.mkdirSync(pathModule.join(path, 'pictures'), { recursive: true });
}
fs.writeFileSync(picPath, imageBuffer);
logger.info(`图片已保存,标签为:${kWord}`);
}
} catch (error) {
logger.error('Error in downImg:', error);
logger.error("保存图片时发生错误");
}
}
export async function fileImgList() {
const picturesPath = pathModule.join(path, 'pictures');
const ImgList = await fs.promises.readdir(picturesPath);
const fileImgList = ImgList.map(filename => {
const match = filename.match(/\d{12}-(.+)$/);
return match ? match[1] : filename;
});
return fileImgList;
}