Compare commits
38 Commits
901f127b8a
...
v1.2.0
Author | SHA1 | Date | |
---|---|---|---|
0ce94d8cba | |||
cf18fdf532 | |||
16196c46ac | |||
579d39fa4a | |||
d7a341afd0 | |||
164b6fa28e | |||
b1709958e4 | |||
23d56963e4 | |||
4242665b7a | |||
c02d693417 | |||
ea06c9fd14 | |||
1f4d251aa1 | |||
5daa6a2dfd | |||
4d22f53531 | |||
e01a9632ea | |||
85060429bc | |||
9e330e2de4 | |||
752b6e8eb9 | |||
91853e4296 | |||
04f6858411 | |||
216d1d90fa | |||
8fc996ce83 | |||
1fbe397078 | |||
4a86ca162a | |||
83964b678d | |||
eb57171700 | |||
7bf234cf57 | |||
c7bb8ec951 | |||
fda78b5772 | |||
a1ef6311c4 | |||
44ad5a973c | |||
3cec0cad64 | |||
c0a732ae55 | |||
ae6d120605 | |||
65a3a4ac99 | |||
7f3af6b154 | |||
98e48140d3 | |||
6be7015b8a |
@ -1,4 +1,3 @@
|
||||
node_modules
|
||||
.env
|
||||
.prettierignore
|
||||
.prettierrc.js
|
46
.drone.yml
46
.drone.yml
@ -2,6 +2,11 @@ kind: pipeline # 定义一个管道
|
||||
type: docker # 当前管道的类型
|
||||
name: build # 当前管道的名称
|
||||
|
||||
volumes:
|
||||
- name: cache
|
||||
host:
|
||||
path: /tmp/cache
|
||||
|
||||
steps: # 定义管道的执行步骤
|
||||
# - name: build-project # 步骤名称
|
||||
# image: node:18-bullseye # 当前步骤使用的镜像
|
||||
@ -13,16 +18,37 @@ steps: # 定义管道的执行步骤
|
||||
# - npm config set registry https://registry.npm.taobao.org
|
||||
# - npm i
|
||||
|
||||
- name: cache # 加载缓存
|
||||
image: drillster/drone-volume-cache
|
||||
pull: if-not-exists
|
||||
depends_on: [clone]
|
||||
volumes:
|
||||
- name: cache
|
||||
path: /cache
|
||||
settings:
|
||||
restore: true
|
||||
mount:
|
||||
- ./node_modules
|
||||
|
||||
- name: build-tags
|
||||
image: yxs970707/drone-web-tags
|
||||
depends_on: [clone]
|
||||
pull: if-not-exists
|
||||
depends_on: [cache]
|
||||
settings:
|
||||
tags:
|
||||
- latest
|
||||
|
||||
- name: install
|
||||
image: node:18
|
||||
pull: if-not-exists
|
||||
depends_on: [build-tags]
|
||||
commands:
|
||||
- npm --registry https://registry.npm.taobao.org install
|
||||
|
||||
- name: build-image
|
||||
image: plugins/docker
|
||||
depends_on: [build-tags]
|
||||
pull: if-not-exists
|
||||
depends_on: [install]
|
||||
settings:
|
||||
username:
|
||||
from_secret: docker_username
|
||||
@ -33,6 +59,18 @@ steps: # 定义管道的执行步骤
|
||||
registry: https://git.liliyamol.cn:8081
|
||||
repo: git.liliyamol.cn:8081/mol/mol-robot
|
||||
|
||||
- name: rebuild cache # 构建缓存
|
||||
image: drillster/drone-volume-cache
|
||||
pull: if-not-exists
|
||||
depends_on: [build-image]
|
||||
volumes:
|
||||
- name: cache
|
||||
path: /cache
|
||||
settings:
|
||||
rebuild: true
|
||||
mount:
|
||||
- ./node_modules
|
||||
|
||||
---
|
||||
kind: pipeline # 定义一个管道
|
||||
type: docker # 当前管道的类型
|
||||
@ -64,6 +102,6 @@ steps:
|
||||
- list=$(docker ps -a | grep mol-robot* | awk '{print $1}')
|
||||
- test "$list" = "" && echo "none mol-robot containers running" || docker stop $list && docker container rm $list
|
||||
# 过滤出dockerImages的id, 删除none镜像
|
||||
- docker run -d -p 16808:8080 --name=mirai_robot_${DRONE_BUILD_NUMBER} --env-file /mnt/data/opt/mol-robot/.env git.liliyamol.cn:8081/mol/mol-robot:latest
|
||||
- docker rmi $(docker images | grep "none" | awk '{print $3}')
|
||||
- docker run -d -p 16808:8080 --name=mol-robot_${DRONE_BUILD_NUMBER} --env-file /mnt/data/opt/mol-robot/.env git.liliyamol.cn:8081/mol/mol-robot:latest
|
||||
# - docker rmi $(docker images | grep "none" | awk '{print $3}')
|
||||
- echo ==-----==部署成功==-----==
|
||||
|
@ -1,12 +1,11 @@
|
||||
FROM node:18-bullseye as dep-builder
|
||||
FROM node:18
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package*.json ./
|
||||
|
||||
RUN npm install
|
||||
# COPY package*.json ./
|
||||
|
||||
COPY . .
|
||||
# RUN npm --registry https://registry.npm.taobao.org install
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
|
28
app.js
28
app.js
@ -1,8 +1,8 @@
|
||||
import fs from 'fs';
|
||||
import { join } from 'path';
|
||||
import CreateBot from '#root/bot/index.js';
|
||||
import CreateWebhookServer from '#root/http/index.js';
|
||||
import logger from '#root/utils/logger.js';
|
||||
import handlers from './handlers/index.js';
|
||||
import plugins from '#root/plugins/index.js';
|
||||
|
||||
let retryCount = 0;
|
||||
|
||||
@ -33,21 +33,23 @@ let retryCount = 0;
|
||||
});
|
||||
logger('bot 连接成功!!');
|
||||
|
||||
bot.mountPlugin(plugins);
|
||||
|
||||
logger('开启 webhook 服务器');
|
||||
const webhook = new CreateWebhookServer();
|
||||
|
||||
logger('开始引入handler函数');
|
||||
const files = fs.readdirSync('./handlers');
|
||||
const handlers = files
|
||||
.filter((it) => /\.js$/.test(it))
|
||||
.map((it) => {
|
||||
let fPath = join(__dirname, './handlers', it);
|
||||
return {
|
||||
event: it.split('.').slice(0, -1).join('.'),
|
||||
handler: require(fPath).bind(this, bot),
|
||||
};
|
||||
});
|
||||
webhook.registerHanlder(handlers);
|
||||
// const files = fs.readdirSync('./handlers');
|
||||
// const handlers = files
|
||||
// .filter((it) => /\.js$/.test(it))
|
||||
// .map((it) => {
|
||||
// let fPath = join(__dirname, './handlers', it);
|
||||
// return {
|
||||
// event: it.split('.').slice(0, -1).join('.'),
|
||||
// handler: require(fPath).bind(this, bot),
|
||||
// };
|
||||
// });
|
||||
webhook.registerHandler(handlers.map(i => ({ event: i.event, handler: i.handler.bind(this, bot) })));
|
||||
|
||||
webhook.startListen(port);
|
||||
logger(`开始监听端口: ${port}`);
|
||||
|
@ -1,7 +1,6 @@
|
||||
import pkg from 'mirai-js';
|
||||
import Queue from '#root/utils/queue.js';
|
||||
import logger from '#root/utils/logger.js';
|
||||
import plugins from '#root/plugins/index.js';
|
||||
|
||||
const { Bot } = pkg;
|
||||
|
||||
@ -19,8 +18,6 @@ export default class CreateBot {
|
||||
verifyKey: config.verifyKey,
|
||||
qq: config.qq,
|
||||
});
|
||||
|
||||
this.mountPlugin(plugins);
|
||||
}
|
||||
|
||||
async sendMessageToFriend(qq, message) {
|
||||
@ -59,7 +56,7 @@ export default class CreateBot {
|
||||
|
||||
mountPlugin(plugins) {
|
||||
for (let plugin of plugins) {
|
||||
plugin.install(this.bot, this.queue);
|
||||
plugin(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,18 +6,18 @@ export const command = {
|
||||
|
||||
export const defaultSubs = {
|
||||
groups: process.env.DEFAULT_SUB_GROUPS
|
||||
? process.env.DEFAULT_SUB_GROUPS.split(',')
|
||||
? process.env.DEFAULT_SUB_GROUPS.split(',').map(item => +item)
|
||||
: [],
|
||||
users: process.env.DEFAULT_SUB_USERS
|
||||
? process.env.DEFAULT_SUB_USERS.split(',')
|
||||
? process.env.DEFAULT_SUB_USERS.split(',').map(item => +item)
|
||||
: [],
|
||||
};
|
||||
|
||||
export const cookSubs = {
|
||||
groups: process.env.COOK_SUB_GROUPS
|
||||
? process.env.COOK_SUB_GROUPS.split(',')
|
||||
? process.env.COOK_SUB_GROUPS.split(',').map(item => +item)
|
||||
: [],
|
||||
users: process.env.COOK_SUB_USERS
|
||||
? process.env.COOK_SUB_USERS.split(',')
|
||||
? process.env.COOK_SUB_USERS.split(',').map(item => +item)
|
||||
: [],
|
||||
};
|
||||
|
14
handlers/index.js
Normal file
14
handlers/index.js
Normal file
@ -0,0 +1,14 @@
|
||||
import sendForwardMessage from './sendForwardMessage.js'
|
||||
import sendMessage from './sendMessage.js'
|
||||
import sendMessage2 from './sendMessage2.js'
|
||||
|
||||
export default [{
|
||||
event: 'sendMessage',
|
||||
handler: sendMessage
|
||||
}, {
|
||||
event: 'sendForwardMessage',
|
||||
handler: sendForwardMessage
|
||||
}, {
|
||||
event: 'sendMessage2',
|
||||
handler: sendMessage2
|
||||
}]
|
@ -1,6 +1,7 @@
|
||||
import { Message } from 'mirai-js';
|
||||
import pkg from 'mirai-js';
|
||||
import { defaultSubs } from '#root/config/index.js';
|
||||
import logger from '#root/utils/logger.js';
|
||||
const { Message } = pkg
|
||||
|
||||
export default function sendForwardMessage(bot, data) {
|
||||
logger('开始执行handler: sendForwardMessage');
|
||||
@ -23,8 +24,8 @@ export default function sendForwardMessage(bot, data) {
|
||||
if (imgUrls) {
|
||||
Array.isArray(imgUrls)
|
||||
? imgUrls
|
||||
.slice(0, process.env.IMG_NUMBER_IN_ONE_MESSAGE || 1)
|
||||
.forEach((url) => msgContent.addImageUrl(url))
|
||||
.slice(0, process.env.IMG_NUMBER_IN_ONE_MESSAGE || 1)
|
||||
.forEach((url) => msgContent.addImageUrl(url))
|
||||
: msgContent.addImageUrl(imgUrls);
|
||||
}
|
||||
// 添加来源地址
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Message } from 'mirai-js';
|
||||
import pkg from 'mirai-js';
|
||||
import { defaultSubs } from '#root/config/index.js';
|
||||
import logger from '#root/utils/logger.js';
|
||||
const { Message } = pkg
|
||||
|
||||
// 方法已废弃
|
||||
export default function sendMessage(bot, data) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Message } from 'mirai-js';
|
||||
import pkg from 'mirai-js';
|
||||
import { defaultSubs } from '#root/config/index.js';
|
||||
import logger from '#root/utils/logger.js';
|
||||
const { Message } = pkg
|
||||
|
||||
export default function sendMessage2(bot, data) {
|
||||
logger('开始执行handler: sendMessage2');
|
||||
@ -30,8 +31,8 @@ export default function sendMessage2(bot, data) {
|
||||
if (imgUrls) {
|
||||
Array.isArray(imgUrls)
|
||||
? imgUrls
|
||||
.slice(0, process.env.IMG_NUMBER_IN_ONE_MESSAGE || 1)
|
||||
.forEach((url) => msgContent.addImageUrl(url))
|
||||
.slice(0, process.env.IMG_NUMBER_IN_ONE_MESSAGE || 1)
|
||||
.forEach((url) => msgContent.addImageUrl(url))
|
||||
: msgContent.addImageUrl(imgUrls);
|
||||
}
|
||||
// 添加来源地址
|
||||
|
@ -51,7 +51,7 @@ export default class CreateWebhookServer {
|
||||
.listen(port || 8080);
|
||||
}
|
||||
|
||||
registerHanlder(handlers) {
|
||||
registerHandler(handlers) {
|
||||
logger(`${handlers.map((item) => item.event).join('、')}函数已引入`);
|
||||
this.handlers.push(...handlers);
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
import getDb from '../db/index.js';
|
||||
import logger from '#root/utils/logger.js'
|
||||
|
||||
export async function getRandomRecipe(limit = 5) {
|
||||
logger(`随机${limit}个食谱`)
|
||||
const data = await getDb();
|
||||
const len = data.length;
|
||||
const list = [];
|
||||
const indexList = new Set();
|
||||
|
||||
while (indexList.size === limit) {
|
||||
while (indexList.size < limit) {
|
||||
const index = Math.floor(Math.random() * len);
|
||||
if (!indexList.has(index)) {
|
||||
indexList.set(index);
|
||||
indexList.add(index);
|
||||
list.push(data[index]);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
93
plugins/cook/db/food.js
Normal file
93
plugins/cook/db/food.js
Normal file
@ -0,0 +1,93 @@
|
||||
export const emojiMap = new Map([
|
||||
[
|
||||
'土豆', '🥔'
|
||||
],
|
||||
[
|
||||
'胡萝卜', '🥕'
|
||||
],
|
||||
[
|
||||
'花菜', '🥦'
|
||||
],
|
||||
[
|
||||
'白萝卜', '🥣'
|
||||
],
|
||||
[
|
||||
'西葫芦', '🥒'
|
||||
],
|
||||
[
|
||||
'番茄', '🍅'
|
||||
],
|
||||
[
|
||||
'西红柿', '🍅'
|
||||
],
|
||||
[
|
||||
'芹菜', '🥬'
|
||||
],
|
||||
[
|
||||
'黄瓜', '🥒'
|
||||
],
|
||||
[
|
||||
'洋葱', '🧅'
|
||||
],
|
||||
[
|
||||
'莴笋', '🎍'
|
||||
],
|
||||
[
|
||||
'菌菇', '🍄'
|
||||
],
|
||||
[
|
||||
'茄子', '🍆'
|
||||
],
|
||||
[
|
||||
'豆腐', '🍲'
|
||||
],
|
||||
[
|
||||
'包菜', '🥗'
|
||||
],
|
||||
[
|
||||
'白菜', '🥬'
|
||||
],
|
||||
[
|
||||
'午餐肉', '🥓'
|
||||
],
|
||||
[
|
||||
'香肠', '🌭'
|
||||
],
|
||||
[
|
||||
'腊肠', '🌭'
|
||||
],
|
||||
[
|
||||
'鸡肉', '🐤'
|
||||
],
|
||||
[
|
||||
'猪肉', '🐷'
|
||||
],
|
||||
[
|
||||
'鸡蛋', '🥚'
|
||||
],
|
||||
[
|
||||
'虾', '🦐'
|
||||
],
|
||||
[
|
||||
'牛肉', '🐮'
|
||||
],
|
||||
[
|
||||
'骨头', '🦴'
|
||||
],
|
||||
[
|
||||
'鱼(Todo)', '🐟'
|
||||
],
|
||||
[
|
||||
'面食', '🍝'
|
||||
],
|
||||
[
|
||||
'面包', '🍞'
|
||||
],
|
||||
[
|
||||
'米', '🍚'
|
||||
],
|
||||
[
|
||||
'方便面', '🍜'
|
||||
]
|
||||
]
|
||||
)
|
@ -1,19 +1,40 @@
|
||||
import { createParser } from 'node-csv';
|
||||
import pkg from 'node-csv';
|
||||
import logger from '#root/utils/logger.js';
|
||||
import { emojiMap } from './food.js'
|
||||
import path from 'path';
|
||||
const __dirname = path.dirname(new URL(import.meta.url).pathname);
|
||||
|
||||
const { createParser } = pkg
|
||||
let recipeData;
|
||||
let loaded = false;
|
||||
|
||||
function parser() {
|
||||
csv = createParser();
|
||||
const csv = createParser();
|
||||
return new Promise((r, j) => {
|
||||
csv.parserFile('./recipe.csv', function (err, data) {
|
||||
csv.parseFile(path.join(__dirname, './recipe.csv'), function (err, data) {
|
||||
if (err) {
|
||||
logger.err(err);
|
||||
return j();
|
||||
}
|
||||
logger('食谱加载成功');
|
||||
recipeData = data;
|
||||
const keys = data.shift()
|
||||
keys[0] = keys[0].slice(1)
|
||||
recipeData = data.map(d => {
|
||||
const res = {}
|
||||
keys.forEach((key, index) => {
|
||||
if (key) {
|
||||
let text = d[index]
|
||||
if (key === 'stuff') {
|
||||
text = text.split('、').map(t => {
|
||||
return `${emojiMap.has(t) ? emojiMap.get(t) + ' ' : ''}${t}`
|
||||
}).join('、')
|
||||
}
|
||||
|
||||
res[key] = text
|
||||
}
|
||||
})
|
||||
return res
|
||||
});
|
||||
loaded = true;
|
||||
r(recipeData);
|
||||
});
|
||||
@ -24,6 +45,11 @@ export default async function getDb() {
|
||||
if (loaded) {
|
||||
return recipeData;
|
||||
} else {
|
||||
return await parser();
|
||||
await parser()
|
||||
return recipeData;
|
||||
}
|
||||
}
|
||||
|
||||
// getDb().then((res) => {
|
||||
// // console.log(recipeData)
|
||||
// })
|
||||
|
@ -2,24 +2,31 @@ 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 { Middleware, Message } = pkg;
|
||||
const cookCommand = command.cook;
|
||||
let Bot;
|
||||
let Queue;
|
||||
|
||||
const commandHandle = {
|
||||
random: randomCook,
|
||||
};
|
||||
|
||||
function randomCook(text, data) {
|
||||
async function randomCook(text, data) {
|
||||
const limit = Number.parseInt(text) > 0 ? Number.parseInt(text) : 5;
|
||||
const recipeList = getRandomRecipe(limit);
|
||||
const recipeList = await getRandomRecipe(limit);
|
||||
const message = genRecipeMessage(recipeList, '给你推荐');
|
||||
|
||||
switch (data.type) {
|
||||
case 'GroupMessage':
|
||||
Bot.sendMessageToGroup(data.sender.group.id, message);
|
||||
const forwardMsg = Message.createForwardMessage();
|
||||
forwardMsg.addForwardNode({
|
||||
senderId: process.env.QQ,
|
||||
time: 0,
|
||||
senderName: '厨娘·模儿',
|
||||
messageChain: message
|
||||
})
|
||||
Bot.sendMessageToGroup(data.sender.group.id, forwardMsg);
|
||||
break;
|
||||
case 'FriendMessage':
|
||||
Bot.sendMessageToFriend(data.sender.id, message);
|
||||
@ -27,20 +34,23 @@ function randomCook(text, data) {
|
||||
}
|
||||
|
||||
function cookInit() {
|
||||
Bot.on(
|
||||
Bot.bot.on(
|
||||
'GroupMessage',
|
||||
new Middleware().groupFilter(cookSubs.groups).done(dealCommon),
|
||||
new Middleware().groupFilter(cookSubs.groups).textProcessor().done(dealCommon),
|
||||
);
|
||||
|
||||
Bot.on(
|
||||
Bot.bot.on(
|
||||
'FriendMessage',
|
||||
new Middleware().friendFilter(cookSubs.users).done(dealCommon),
|
||||
new Middleware().friendFilter(cookSubs.users).textProcessor().done(dealCommon),
|
||||
);
|
||||
Bot.sendMessageToFriend(752753679, '测试');
|
||||
logger('cook 初始化完成')
|
||||
}
|
||||
|
||||
function dealCommon(data) {
|
||||
logger('开始处理指令')
|
||||
const [commandText, ...contentText] = data.text.trim().split(' ');
|
||||
for (let com of cookCommand.keys()) {
|
||||
for (let com of Object.keys(cookCommand)) {
|
||||
if (cookCommand[com].includes(commandText)) {
|
||||
commandHandle[com](contentText, data);
|
||||
break;
|
||||
@ -48,8 +58,8 @@ function dealCommon(data) {
|
||||
}
|
||||
}
|
||||
|
||||
export default function install(bot, queue) {
|
||||
export default function install(bot) {
|
||||
Bot = bot;
|
||||
Queue = queue;
|
||||
cookInit();
|
||||
logger('cook 插件加载成功')
|
||||
}
|
||||
|
@ -1,19 +1,21 @@
|
||||
import pkg from 'mirai-js'
|
||||
const { Message } = pkg
|
||||
|
||||
export function genRecipeMessage(recipeList, header, footer) {
|
||||
const textList = [];
|
||||
|
||||
for (let recipe of recipeList) {
|
||||
textList.push(
|
||||
`吃${recipe.name}\n准备材料:${recipe.stuff}\n使用厨具:${recipe.tools}\nhttps://www.bilibili.com/video/${recipe.bv}`,
|
||||
);
|
||||
}
|
||||
|
||||
const msg = new Message()
|
||||
if (header) {
|
||||
textList.shift(header);
|
||||
msg.addText(`${header}\n\n`);
|
||||
}
|
||||
recipeList.forEach((recipe, index)=> {
|
||||
msg.addText(`${index + 1}. ${recipe.name}\n`)
|
||||
msg.addText(`\t· 材料:${recipe.stuff}\n`)
|
||||
msg.addText(`\t· 厨具:${recipe.tools}\n`)
|
||||
msg.addText(`\t· 制作视频:https://www.bilibili.com/video/${recipe.bv}\n\n`)
|
||||
})
|
||||
|
||||
if (footer) {
|
||||
textList.push(footer);
|
||||
msg.addText(`${footer}`);
|
||||
}
|
||||
|
||||
return textList.join('\n----------\n');
|
||||
return msg
|
||||
}
|
||||
|
Reference in New Issue
Block a user