Compare commits

..

38 Commits

Author SHA1 Message Date
mol
0ce94d8cba test
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2023-12-14 15:05:30 +08:00
mol
cf18fdf532 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 14:59:41 +08:00
mol
16196c46ac test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 14:35:47 +08:00
mol
579d39fa4a test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 13:59:47 +08:00
mol
d7a341afd0 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 13:52:46 +08:00
mol
164b6fa28e test
Some checks are pending
continuous-integration/drone/push Build is running
2023-12-14 13:48:52 +08:00
mol
b1709958e4 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 13:39:51 +08:00
mol
23d56963e4 test
Some checks are pending
continuous-integration/drone/push Build is running
2023-12-14 13:39:26 +08:00
mol
4242665b7a test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 13:30:20 +08:00
mol
c02d693417 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 13:24:14 +08:00
mol
ea06c9fd14 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 13:16:18 +08:00
mol
1f4d251aa1 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 13:11:12 +08:00
mol
5daa6a2dfd test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 13:03:05 +08:00
mol
4d22f53531 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 12:52:50 +08:00
mol
e01a9632ea test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 12:34:58 +08:00
mol
85060429bc test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 12:30:28 +08:00
mol
9e330e2de4 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 12:04:19 +08:00
mol
752b6e8eb9 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 11:59:26 +08:00
mol
91853e4296 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 11:11:55 +08:00
mol
04f6858411 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 11:01:45 +08:00
mol
216d1d90fa test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 10:52:43 +08:00
mol
8fc996ce83 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 10:36:13 +08:00
mol
1fbe397078 test
Some checks are pending
continuous-integration/drone/push Build is running
2023-12-14 10:32:38 +08:00
mol
4a86ca162a test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 10:26:11 +08:00
mol
83964b678d test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 10:07:44 +08:00
mol
eb57171700 test
Some checks reported errors
continuous-integration/drone/push Build encountered an error
2023-12-14 10:01:19 +08:00
mol
7bf234cf57 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 09:56:51 +08:00
mol
c7bb8ec951 test 2023-12-14 09:53:33 +08:00
mol
fda78b5772 test
Some checks reported errors
continuous-integration/drone/push Build encountered an error
2023-12-14 09:32:11 +08:00
mol
a1ef6311c4 test
Some checks reported errors
continuous-integration/drone/push Build was killed
2023-12-14 09:28:57 +08:00
mol
44ad5a973c test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 00:10:20 +08:00
mol
3cec0cad64 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-13 23:59:35 +08:00
mol
c0a732ae55 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-13 23:35:36 +08:00
mol
ae6d120605 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-13 23:23:00 +08:00
mol
65a3a4ac99 test
Some checks are pending
continuous-integration/drone/push Build is running
2023-12-13 23:21:05 +08:00
mol
7f3af6b154 test
Some checks are pending
continuous-integration/drone/push Build is running
2023-12-13 22:17:45 +08:00
mol
98e48140d3 test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-13 21:19:20 +08:00
mol
6be7015b8a test
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-13 20:57:11 +08:00
16 changed files with 253 additions and 70 deletions

View File

@ -1,4 +1,3 @@
node_modules
.env .env
.prettierignore .prettierignore
.prettierrc.js .prettierrc.js

View File

@ -2,6 +2,11 @@ kind: pipeline # 定义一个管道
type: docker # 当前管道的类型 type: docker # 当前管道的类型
name: build # 当前管道的名称 name: build # 当前管道的名称
volumes:
- name: cache
host:
path: /tmp/cache
steps: # 定义管道的执行步骤 steps: # 定义管道的执行步骤
# - name: build-project # 步骤名称 # - name: build-project # 步骤名称
# image: node:18-bullseye # 当前步骤使用的镜像 # image: node:18-bullseye # 当前步骤使用的镜像
@ -13,16 +18,37 @@ steps: # 定义管道的执行步骤
# - npm config set registry https://registry.npm.taobao.org # - npm config set registry https://registry.npm.taobao.org
# - npm i # - 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 - name: build-tags
image: yxs970707/drone-web-tags image: yxs970707/drone-web-tags
depends_on: [clone] pull: if-not-exists
depends_on: [cache]
settings: settings:
tags: tags:
- latest - 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 - name: build-image
image: plugins/docker image: plugins/docker
depends_on: [build-tags] pull: if-not-exists
depends_on: [install]
settings: settings:
username: username:
from_secret: docker_username from_secret: docker_username
@ -33,6 +59,18 @@ steps: # 定义管道的执行步骤
registry: https://git.liliyamol.cn:8081 registry: https://git.liliyamol.cn:8081
repo: git.liliyamol.cn:8081/mol/mol-robot 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 # 定义一个管道 kind: pipeline # 定义一个管道
type: docker # 当前管道的类型 type: docker # 当前管道的类型
@ -64,6 +102,6 @@ steps:
- list=$(docker ps -a | grep mol-robot* | awk '{print $1}') - 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 - test "$list" = "" && echo "none mol-robot containers running" || docker stop $list && docker container rm $list
# 过滤出dockerImages的id, 删除none镜像 # 过滤出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 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}') # - docker rmi $(docker images | grep "none" | awk '{print $3}')
- echo ==-----==部署成功==-----== - echo ==-----==部署成功==-----==

View File

@ -1,12 +1,11 @@
FROM node:18-bullseye as dep-builder FROM node:18
WORKDIR /app WORKDIR /app
COPY package*.json ./ # COPY package*.json ./
RUN npm install
COPY . . COPY . .
# RUN npm --registry https://registry.npm.taobao.org install
EXPOSE 8080 EXPOSE 8080

28
app.js
View File

@ -1,8 +1,8 @@
import fs from 'fs';
import { join } from 'path';
import CreateBot from '#root/bot/index.js'; import CreateBot from '#root/bot/index.js';
import CreateWebhookServer from '#root/http/index.js'; import CreateWebhookServer from '#root/http/index.js';
import logger from '#root/utils/logger.js'; import logger from '#root/utils/logger.js';
import handlers from './handlers/index.js';
import plugins from '#root/plugins/index.js';
let retryCount = 0; let retryCount = 0;
@ -33,21 +33,23 @@ let retryCount = 0;
}); });
logger('bot 连接成功!!'); logger('bot 连接成功!!');
bot.mountPlugin(plugins);
logger('开启 webhook 服务器'); logger('开启 webhook 服务器');
const webhook = new CreateWebhookServer(); const webhook = new CreateWebhookServer();
logger('开始引入handler函数'); logger('开始引入handler函数');
const files = fs.readdirSync('./handlers'); // const files = fs.readdirSync('./handlers');
const handlers = files // const handlers = files
.filter((it) => /\.js$/.test(it)) // .filter((it) => /\.js$/.test(it))
.map((it) => { // .map((it) => {
let fPath = join(__dirname, './handlers', it); // let fPath = join(__dirname, './handlers', it);
return { // return {
event: it.split('.').slice(0, -1).join('.'), // event: it.split('.').slice(0, -1).join('.'),
handler: require(fPath).bind(this, bot), // handler: require(fPath).bind(this, bot),
}; // };
}); // });
webhook.registerHanlder(handlers); webhook.registerHandler(handlers.map(i => ({ event: i.event, handler: i.handler.bind(this, bot) })));
webhook.startListen(port); webhook.startListen(port);
logger(`开始监听端口: ${port}`); logger(`开始监听端口: ${port}`);

View File

@ -1,7 +1,6 @@
import pkg from 'mirai-js'; import pkg from 'mirai-js';
import Queue from '#root/utils/queue.js'; import Queue from '#root/utils/queue.js';
import logger from '#root/utils/logger.js'; import logger from '#root/utils/logger.js';
import plugins from '#root/plugins/index.js';
const { Bot } = pkg; const { Bot } = pkg;
@ -19,8 +18,6 @@ export default class CreateBot {
verifyKey: config.verifyKey, verifyKey: config.verifyKey,
qq: config.qq, qq: config.qq,
}); });
this.mountPlugin(plugins);
} }
async sendMessageToFriend(qq, message) { async sendMessageToFriend(qq, message) {
@ -59,7 +56,7 @@ export default class CreateBot {
mountPlugin(plugins) { mountPlugin(plugins) {
for (let plugin of plugins) { for (let plugin of plugins) {
plugin.install(this.bot, this.queue); plugin(this);
} }
} }
} }

View File

@ -6,18 +6,18 @@ export const command = {
export const defaultSubs = { export const defaultSubs = {
groups: process.env.DEFAULT_SUB_GROUPS 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 users: process.env.DEFAULT_SUB_USERS
? process.env.DEFAULT_SUB_USERS.split(',') ? process.env.DEFAULT_SUB_USERS.split(',').map(item => +item)
: [], : [],
}; };
export const cookSubs = { export const cookSubs = {
groups: process.env.COOK_SUB_GROUPS 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 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
View 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
}]

View File

@ -1,6 +1,7 @@
import { Message } from 'mirai-js'; import pkg from 'mirai-js';
import { defaultSubs } from '#root/config/index.js'; import { defaultSubs } from '#root/config/index.js';
import logger from '#root/utils/logger.js'; import logger from '#root/utils/logger.js';
const { Message } = pkg
export default function sendForwardMessage(bot, data) { export default function sendForwardMessage(bot, data) {
logger('开始执行handler: sendForwardMessage'); logger('开始执行handler: sendForwardMessage');
@ -23,8 +24,8 @@ export default function sendForwardMessage(bot, data) {
if (imgUrls) { if (imgUrls) {
Array.isArray(imgUrls) Array.isArray(imgUrls)
? imgUrls ? imgUrls
.slice(0, process.env.IMG_NUMBER_IN_ONE_MESSAGE || 1) .slice(0, process.env.IMG_NUMBER_IN_ONE_MESSAGE || 1)
.forEach((url) => msgContent.addImageUrl(url)) .forEach((url) => msgContent.addImageUrl(url))
: msgContent.addImageUrl(imgUrls); : msgContent.addImageUrl(imgUrls);
} }
// 添加来源地址 // 添加来源地址

View File

@ -1,6 +1,7 @@
import { Message } from 'mirai-js'; import pkg from 'mirai-js';
import { defaultSubs } from '#root/config/index.js'; import { defaultSubs } from '#root/config/index.js';
import logger from '#root/utils/logger.js'; import logger from '#root/utils/logger.js';
const { Message } = pkg
// 方法已废弃 // 方法已废弃
export default function sendMessage(bot, data) { export default function sendMessage(bot, data) {

View File

@ -1,6 +1,7 @@
import { Message } from 'mirai-js'; import pkg from 'mirai-js';
import { defaultSubs } from '#root/config/index.js'; import { defaultSubs } from '#root/config/index.js';
import logger from '#root/utils/logger.js'; import logger from '#root/utils/logger.js';
const { Message } = pkg
export default function sendMessage2(bot, data) { export default function sendMessage2(bot, data) {
logger('开始执行handler: sendMessage2'); logger('开始执行handler: sendMessage2');
@ -30,8 +31,8 @@ export default function sendMessage2(bot, data) {
if (imgUrls) { if (imgUrls) {
Array.isArray(imgUrls) Array.isArray(imgUrls)
? imgUrls ? imgUrls
.slice(0, process.env.IMG_NUMBER_IN_ONE_MESSAGE || 1) .slice(0, process.env.IMG_NUMBER_IN_ONE_MESSAGE || 1)
.forEach((url) => msgContent.addImageUrl(url)) .forEach((url) => msgContent.addImageUrl(url))
: msgContent.addImageUrl(imgUrls); : msgContent.addImageUrl(imgUrls);
} }
// 添加来源地址 // 添加来源地址

View File

@ -51,7 +51,7 @@ export default class CreateWebhookServer {
.listen(port || 8080); .listen(port || 8080);
} }
registerHanlder(handlers) { registerHandler(handlers) {
logger(`${handlers.map((item) => item.event).join('、')}函数已引入`); logger(`${handlers.map((item) => item.event).join('、')}函数已引入`);
this.handlers.push(...handlers); this.handlers.push(...handlers);
} }

View File

@ -1,18 +1,18 @@
import getDb from '../db/index.js'; import getDb from '../db/index.js';
import logger from '#root/utils/logger.js'
export async function getRandomRecipe(limit = 5) { export async function getRandomRecipe(limit = 5) {
logger(`随机${limit}个食谱`)
const data = await getDb(); const data = await getDb();
const len = data.length; const len = data.length;
const list = []; const list = [];
const indexList = new Set(); const indexList = new Set();
while (indexList.size < limit) {
while (indexList.size === limit) {
const index = Math.floor(Math.random() * len); const index = Math.floor(Math.random() * len);
if (!indexList.has(index)) { if (!indexList.has(index)) {
indexList.set(index); indexList.add(index);
list.push(data[index]); list.push(data[index]);
} }
} }
return list; return list;
} }

93
plugins/cook/db/food.js Normal file
View File

@ -0,0 +1,93 @@
export const emojiMap = new Map([
[
'土豆', '🥔'
],
[
'胡萝卜', '🥕'
],
[
'花菜', '🥦'
],
[
'白萝卜', '🥣'
],
[
'西葫芦', '🥒'
],
[
'番茄', '🍅'
],
[
'西红柿', '🍅'
],
[
'芹菜', '🥬'
],
[
'黄瓜', '🥒'
],
[
'洋葱', '🧅'
],
[
'莴笋', '🎍'
],
[
'菌菇', '🍄'
],
[
'茄子', '🍆'
],
[
'豆腐', '🍲'
],
[
'包菜', '🥗'
],
[
'白菜', '🥬'
],
[
'午餐肉', '🥓'
],
[
'香肠', '🌭'
],
[
'腊肠', '🌭'
],
[
'鸡肉', '🐤'
],
[
'猪肉', '🐷'
],
[
'鸡蛋', '🥚'
],
[
'虾', '🦐'
],
[
'牛肉', '🐮'
],
[
'骨头', '🦴'
],
[
'鱼Todo', '🐟'
],
[
'面食', '🍝'
],
[
'面包', '🍞'
],
[
'米', '🍚'
],
[
'方便面', '🍜'
]
]
)

View File

@ -1,19 +1,40 @@
import { createParser } from 'node-csv'; import pkg from 'node-csv';
import logger from '#root/utils/logger.js'; 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 recipeData;
let loaded = false; let loaded = false;
function parser() { function parser() {
csv = createParser(); const csv = createParser();
return new Promise((r, j) => { return new Promise((r, j) => {
csv.parserFile('./recipe.csv', function (err, data) { csv.parseFile(path.join(__dirname, './recipe.csv'), function (err, data) {
if (err) { if (err) {
logger.err(err); logger.err(err);
return j(); return j();
} }
logger('食谱加载成功'); 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; loaded = true;
r(recipeData); r(recipeData);
}); });
@ -24,6 +45,11 @@ export default async function getDb() {
if (loaded) { if (loaded) {
return recipeData; return recipeData;
} else { } else {
return await parser(); await parser()
return recipeData;
} }
} }
// getDb().then((res) => {
// // console.log(recipeData)
// })

View File

@ -2,24 +2,31 @@ import pkg from 'mirai-js';
import { cookSubs, command } from '#root/config/index.js'; import { cookSubs, command } from '#root/config/index.js';
import { getRandomRecipe } from './api/index.js'; import { getRandomRecipe } from './api/index.js';
import { genRecipeMessage } from './utils/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; const cookCommand = command.cook;
let Bot; let Bot;
let Queue;
const commandHandle = { const commandHandle = {
random: randomCook, random: randomCook,
}; };
function randomCook(text, data) { async function randomCook(text, data) {
const limit = Number.parseInt(text) > 0 ? Number.parseInt(text) : 5; const limit = Number.parseInt(text) > 0 ? Number.parseInt(text) : 5;
const recipeList = getRandomRecipe(limit); const recipeList = await getRandomRecipe(limit);
const message = genRecipeMessage(recipeList, '给你推荐'); const message = genRecipeMessage(recipeList, '给你推荐');
switch (data.type) { switch (data.type) {
case 'GroupMessage': 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; break;
case 'FriendMessage': case 'FriendMessage':
Bot.sendMessageToFriend(data.sender.id, message); Bot.sendMessageToFriend(data.sender.id, message);
@ -27,20 +34,23 @@ function randomCook(text, data) {
} }
function cookInit() { function cookInit() {
Bot.on( Bot.bot.on(
'GroupMessage', 'GroupMessage',
new Middleware().groupFilter(cookSubs.groups).done(dealCommon), new Middleware().groupFilter(cookSubs.groups).textProcessor().done(dealCommon),
); );
Bot.on( Bot.bot.on(
'FriendMessage', 'FriendMessage',
new Middleware().friendFilter(cookSubs.users).done(dealCommon), new Middleware().friendFilter(cookSubs.users).textProcessor().done(dealCommon),
); );
Bot.sendMessageToFriend(752753679, '测试');
logger('cook 初始化完成')
} }
function dealCommon(data) { function dealCommon(data) {
logger('开始处理指令')
const [commandText, ...contentText] = data.text.trim().split(' '); 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)) { if (cookCommand[com].includes(commandText)) {
commandHandle[com](contentText, data); commandHandle[com](contentText, data);
break; break;
@ -48,8 +58,8 @@ function dealCommon(data) {
} }
} }
export default function install(bot, queue) { export default function install(bot) {
Bot = bot; Bot = bot;
Queue = queue;
cookInit(); cookInit();
logger('cook 插件加载成功')
} }

View File

@ -1,19 +1,21 @@
import pkg from 'mirai-js'
const { Message } = pkg
export function genRecipeMessage(recipeList, header, footer) { export function genRecipeMessage(recipeList, header, footer) {
const textList = []; const msg = new Message()
for (let recipe of recipeList) {
textList.push(
`${recipe.name}\n准备材料:${recipe.stuff}\n使用厨具:${recipe.tools}\nhttps://www.bilibili.com/video/${recipe.bv}`,
);
}
if (header) { 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) { if (footer) {
textList.push(footer); msg.addText(`${footer}`);
} }
return textList.join('\n----------\n'); return msg
} }