style: 代码格式优化

This commit is contained in:
Jerry 2025-10-26 00:42:03 +08:00
parent c4060f535f
commit 582b4450fb
2 changed files with 73 additions and 60 deletions

View File

@ -10,7 +10,7 @@ import Group from '../lib/yunzai/group.js';
import Message from '../lib/yunzai/message.js';
import YunzaiUtils from '../lib/yunzai/utils.js';
import { segment } from 'oicq';
import tools from "../components/tool.js";
import tools from '../components/tool.js';
const nickname = await ConfigControl.get('profile')?.nickName;
export class crystelfAI extends plugin {
@ -28,7 +28,7 @@ export class crystelfAI extends plugin {
{
reg: '^(#|/)?重置(对话|会话)$',
fnc: 'clearChatHistory',
}
},
],
});
this.isInitialized = false;
@ -61,18 +61,18 @@ export class crystelfAI extends plugin {
}
}
Bot.on("message.group",async(e)=>{
Bot.on('message.group', async (e) => {
let flag = false;
if (e.message) {
e.message.forEach(message=>{
e.message.forEach((message) => {
if (message.type === 'at' && message.qq == e.bot.uin) {
flag = true;
}
})
});
}
if (!flag) return;
return await index(e);
})
});
async function index(e) {
try {
@ -104,9 +104,10 @@ async function index(e) {
}
} catch (error) {
logger.error(`[crystelf-ai] 处理消息失败: ${error.message}`);
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
const config = await ConfigControl.get();
const aiConfig = config?.ai;
return e.reply(segment.image(await Meme.getMeme(aiConfig.character, 'default')));
//return e.reply(segment.image(await Meme.getMeme(aiConfig.character, 'default')));
}
}
@ -114,26 +115,25 @@ async function extractUserMessage(msg, nickname, e) {
if (e.message) {
let text = [];
let at = [];
e.message.forEach(message => {
e.message.forEach((message) => {
logger.info(message);
if (message.type === 'text') {
text.push(message.text);
} else if (message.type === 'at') {
at.push(message.qq);
}
})
});
let returnMessage = '';
if (text.length > 0) {
text.forEach(message => {
text.forEach((message) => {
returnMessage += `[${e.sender?.nickname},id:${e.user_id}]说:${message}\n`;
})
});
}
if (at.length > 0) {
at.forEach((at) => {
if (at === e.bot.uin) {
returnMessage += `[${e.sender?.nickname},id:${e.user_id}]@(at)了你,你的id是${at}\n`;
}
else{
} else {
returnMessage += `[${e.sender?.nickname},id:${e.user_id}]@(at)了一个人,id是${at}\n`;
}
});
@ -157,7 +157,7 @@ async function extractUserMessage(msg, nickname, e) {
*/
async function processMessage(userMessage, e, aiConfig) {
const mode = aiConfig?.mode || 'mix';
logger.info(`[crystelf-ai] 群${e.group_id} 用户${e.user_id}使用${mode}进行回复..`)
logger.info(`[crystelf-ai] 群${e.group_id} 用户${e.user_id}使用${mode}进行回复..`);
switch (mode) {
case 'keyword':
return await handleKeywordMode(userMessage, e);
@ -209,7 +209,7 @@ async function handleMixMode(userMessage, e, aiConfig) {
if (isTooLong) {
//消息太长,使用AI回复
logger.info('[crystelf-ai] 消息过长,使用ai回复')
logger.info('[crystelf-ai] 消息过长,使用ai回复');
return await callAiForResponse(userMessage, e, aiConfig);
} else {
const matchResult = await KeywordMatcher.matchKeywords(userMessage, 'ai');
@ -243,7 +243,7 @@ async function handleMixMode(userMessage, e, aiConfig) {
return res;
} else {
logger.info('[crystelf-ai] 关键词匹配失败,使用ai回复')
logger.info('[crystelf-ai] 关键词匹配失败,使用ai回复');
//关键词匹配失败,使用AI回复
return await callAiForResponse(userMessage, e, aiConfig);
}
@ -258,11 +258,12 @@ async function callAiForResponse(userMessage, e, aiConfig) {
logger.info(
`[crystelf-ai] 群${e.group_id} , 用户${e.user_id}无法创建session,请检查是否聊天频繁`
);
await Message.emojiLike(e, e.message_id, 128166, e.group_id, adapter);
return null;
}
//搜索相关记忆
const memories = await MemorySystem.searchMemories(e.user_id, e.msg || '', 5);
logger.info(`[crystelf-ai] ${memories}`)
logger.info(`[crystelf-ai] ${memories}`);
//构建聊天历史
const historyLen = aiConfig.chatHistory;
const chatHistory = session.chatHistory.slice(-historyLen | -10);
@ -293,14 +294,10 @@ async function callAiForResponse(userMessage, e, aiConfig) {
SessionManager.deactivateSession(e.group_id, e.user_id);
return processedResponse;
} catch (error) {
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
logger.error(`[crystelf-ai] AI调用失败: ${error.message}`);
SessionManager.deactivateSession(e.group_id, e.user_id);
return [
{
type: 'meme',
data: 'default',
},
];
return [];
}
}
@ -361,6 +358,8 @@ async function sendResponse(e, messages) {
await tools.sleep(40);
}
} catch (error) {
const adapter = await YunzaiUtils.getAdapter(e);
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
logger.error(`[crystelf-ai] 发送回复失败: ${error.message}`);
}
}
@ -369,7 +368,7 @@ async function handleCodeMessage(e, message) {
try {
//渲染代码为图片
logger.info(message);
logger.info(message.language)
logger.info(message.language);
const imagePath = await Renderer.renderCode(message.data, message.language);
if (imagePath) {
await e.reply(segment.image(imagePath));
@ -378,6 +377,8 @@ async function handleCodeMessage(e, message) {
}
} catch (error) {
logger.error(`[crystelf-ai] 处理代码消息失败: ${error.message}`);
const adapter = await YunzaiUtils.getAdapter(e);
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
await e.reply('渲染代码失败了,待会儿再试试吧..', true);
}
}
@ -393,6 +394,8 @@ async function handleMarkdownMessage(e, message) {
await e.reply('渲染markdown失败了,待会儿再试试吧..', true);
}
} catch (error) {
const adapter = await YunzaiUtils.getAdapter(e);
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
logger.error(`[crystelf-ai] 处理Markdown消息失败: ${error.message}`);
await e.reply('渲染markdown失败了,待会儿再试试吧..', true);
}
@ -402,12 +405,7 @@ async function handleMemeMessage(e, message) {
try {
const config = await ConfigControl.get('ai');
const memeConfig = config?.memeConfig || {};
const availableEmotions = memeConfig.availableEmotions || [
'happy',
'sad',
'angry',
'confused',
];
const availableEmotions = memeConfig.availableEmotions || ['happy', 'sad', 'angry', 'confused'];
//情绪是否有效
const emotion = availableEmotions.includes(message.data) ? message.data : 'default';
const character = memeConfig.character || 'default';
@ -415,7 +413,9 @@ async function handleMemeMessage(e, message) {
await e.reply(segment.image(memeUrl));
} catch (error) {
logger.error(`[crystelf-ai] 处理表情消息失败: ${error.message}`);
e.reply(segment.image(await Meme.getMeme(aiConfig.character, 'default')));
const adapter = await YunzaiUtils.getAdapter(e);
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
//e.reply(segment.image(await Meme.getMeme(aiConfig.character, 'default')));
}
}

View File

@ -58,6 +58,19 @@
"?memeConfig": "表情配置",
"memeConfig": {
"character": "zhenxun",
"availableEmotions": ["angry", "bye", "confused", "default", "good", "goodmorning", "goodnight", "happy", "sad", "shy", "sorry", "surprise"]
"availableEmotions": [
"angry",
"bye",
"confused",
"default",
"good",
"goodmorning",
"goodnight",
"happy",
"sad",
"shy",
"sorry",
"surprise"
]
}
}