63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
import pkg from 'mirai-js';
|
|
import { cookSubs, command } from '#root/config/index.js';
|
|
import { getRandomRecipe } from './api/index.js';
|
|
import { genRecipeMessage } from './utils/index.js';
|
|
import logger from '#root/utils/logger.js'
|
|
|
|
const { Middleware } = pkg;
|
|
const cookCommand = command.cook;
|
|
let Bot;
|
|
|
|
const commandHandle = {
|
|
random: randomCook,
|
|
};
|
|
|
|
function randomCook(text, data) {
|
|
const limit = Number.parseInt(text) > 0 ? Number.parseInt(text) : 5;
|
|
const recipeList = getRandomRecipe(limit);
|
|
const message = genRecipeMessage(recipeList, '给你推荐');
|
|
|
|
switch (data.type) {
|
|
case 'GroupMessage':
|
|
Bot.sendMessageToGroup(data.sender.group.id, message);
|
|
break;
|
|
case 'FriendMessage':
|
|
Bot.sendMessageToFriend(data.sender.id, message);
|
|
}
|
|
}
|
|
|
|
function cookInit() {
|
|
Bot.bot.on(
|
|
'GroupMessage',
|
|
new Middleware().groupFilter(cookSubs.groups).done(dealCommon),
|
|
);
|
|
|
|
Bot.bot.on(
|
|
'FriendMessage',
|
|
new Middleware().friendFilter(cookSubs.users).done(dealCommon),
|
|
);
|
|
Bot.sendMessageToFriend(752753679, '测试');
|
|
logger('cook 初始化完成')
|
|
}
|
|
|
|
function dealCommon(data) {
|
|
for (let i of data.keys()) {
|
|
logger(data[i])
|
|
}
|
|
logger('开始处理指令')
|
|
logger(data)
|
|
const [commandText, ...contentText] = data.text.trim().split(' ');
|
|
for (let com of cookCommand.keys()) {
|
|
if (cookCommand[com].includes(commandText)) {
|
|
commandHandle[com](contentText, data);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
export default function install(bot) {
|
|
Bot = bot;
|
|
cookInit();
|
|
logger('cook 插件加载成功')
|
|
}
|