Compare commits
45 Commits
901f127b8a
...
v1.2.1
Author | SHA1 | Date | |
---|---|---|---|
05e936a067 | |||
5c93d3a30f | |||
61e81a1174 | |||
f8840f9024 | |||
ba596d4f06 | |||
baba5dc342 | |||
ba119d85c9 | |||
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
|
||||
|
||||
|
89
README.md
89
README.md
@ -1,3 +1,88 @@
|
||||
### 模儿的 bot
|
||||
### 模儿bot
|
||||
|
||||
友链:[mirai-js](https://drincann.github.io/Mirai-js/#/v2.x/Bot?id=on)
|
||||
模儿bot是一个提供与mirai-api-http交互的webhook服务器,并提供一些小工具的bot。
|
||||
|
||||
#### 功能
|
||||
1. webhook服务器
|
||||
> http请求方式, method="post", enctype="application/json", path=/
|
||||
|
||||
请求示例
|
||||
```
|
||||
{
|
||||
"event": "sendForwardMessage",
|
||||
"subs": {
|
||||
"groups": [],
|
||||
"users": []
|
||||
},
|
||||
"from": "新闻",
|
||||
"messages": [
|
||||
{
|
||||
"message": "PlayStation Plus二档会员十二月阵容:给他爱5、FF起源等",
|
||||
"imgUrls": [
|
||||
"http://img2.a9vg.com/i/a9-article-list_x256/cms3.0/2023/1214/20231214104321-2309-33362.jpg"
|
||||
],
|
||||
"originUrl": "http://www.a9vg.com/article/215182"
|
||||
}
|
||||
],
|
||||
"body": "Hello World",
|
||||
}
|
||||
```
|
||||
|
||||
请求参数说明
|
||||
发送转发消息请求
|
||||
|参数|类型|说明|是否必填|可选项|
|
||||
|--|--|--|--|--|
|
||||
|event|string|发送消息的类型|是|`sendForwardMessage`|
|
||||
|subs|object|指定接收消息的群或者人|是|--|
|
||||
|from|string|转发消息时,指定发送人的名称|否|--|
|
||||
|messages|message[]|转发的消息列表|是|--|
|
||||
|
||||
发送一般消息请求
|
||||
|参数|类型|说明|是否必填|可选项|
|
||||
|--|--|--|--|--|
|
||||
|event|string|发送消息的类型|是|`sendMessage2`|
|
||||
|subs|object|指定接收消息的群或者人|是|--|
|
||||
|from|string|转发消息时,指定发送人的名称|否|--|
|
||||
|messages|string[]|转发的消息列表|是|--|
|
||||
|imgUrls|string[]|图片地址|否|--|
|
||||
|originUrl|string|源地址|否|--|
|
||||
|at|number|需要at的qq号|否|--|
|
||||
|
||||
|
||||
|
||||
2. 聊天指令
|
||||
1. 厨娘
|
||||
- 随机菜肴推荐 (#模儿晚上吃什么)
|
||||
|
||||
#### 安装运行
|
||||
1. 直接运行
|
||||
```shell
|
||||
npm i -g node@18.17.0
|
||||
|
||||
npm i
|
||||
|
||||
touch .env
|
||||
|
||||
# 修改 .env 文件
|
||||
vim .env
|
||||
|
||||
node app.js
|
||||
```
|
||||
2. docker 运行(开发中)
|
||||
|
||||
#### 环境配置
|
||||
```
|
||||
MIRAI_HTTP_API_HOST=<mirai-api-http地址>
|
||||
MIRAI_HTTP_API_VERIFY_KEY=<mirai-api-http密钥>
|
||||
WEB_HOOK_PORT=<webhook服务器端口>
|
||||
QQ=<机器人qq号>
|
||||
DEFAULT_SUB_GROUPS=<默认订阅群(暂时无用)>
|
||||
DEFAULT_SUB_USERS=<默认订阅人 (暂时无用)>
|
||||
IMG_NUMBER_IN_ONE_MESSAGE=<单个消息允许的配图数量>
|
||||
COOK_SUB_GROUPS=<订阅厨娘的群>
|
||||
MASTER=<管理员qq号>
|
||||
```
|
||||
|
||||
#### 友链
|
||||
- [mirai-js](https://drincann.github.io/Mirai-js/#/v2.x/Bot?id=on)
|
||||
- [今天我们来做菜](https://github.com/YunYouJun/cook)
|
||||
|
30
app.js
30
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,22 +33,26 @@ 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}`);
|
||||
|
||||
bot.sendMessageToFriend(+process.env.MASTER, '模儿bot已就绪!');
|
||||
})(process.env.WEB_HOOK_PORT);
|
||||
|
17
bot/index.js
17
bot/index.js
@ -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,27 +18,25 @@ export default class CreateBot {
|
||||
verifyKey: config.verifyKey,
|
||||
qq: config.qq,
|
||||
});
|
||||
|
||||
this.mountPlugin(plugins);
|
||||
}
|
||||
|
||||
async sendMessageToFriend(qq, message) {
|
||||
sendMessageToFriend(qq, message) {
|
||||
logger(`发送好友[${qq}]消息进入消息队列`);
|
||||
return this.queue
|
||||
.addMethod(this.bot.sendMessage.bind(this.bot, { friend: qq, message }))
|
||||
.then(
|
||||
(res) => {
|
||||
logger(`发送好友[${qq}]消息成功 ${res}`);
|
||||
return res;
|
||||
return ['success', res];
|
||||
},
|
||||
(e) => {
|
||||
logger.warning(`发送好友[${qq}]消息失败,错误信息${e}`);
|
||||
return Promise.reject(e);
|
||||
return ['failed', e];
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async sendMessageToGroup(groupId, message) {
|
||||
sendMessageToGroup(groupId, message) {
|
||||
logger(`发送群[${groupId}]消息进入消息队列`);
|
||||
return this.queue
|
||||
.addMethod(
|
||||
@ -48,18 +45,18 @@ export default class CreateBot {
|
||||
.then(
|
||||
(res) => {
|
||||
logger(`发送群[${groupId}]消息成功 ${res}`);
|
||||
return res;
|
||||
return ['success', res];
|
||||
},
|
||||
(e) => {
|
||||
logger.warning(`发送群[${groupId}]消息失败,错误信息${e}`);
|
||||
return Promise.reject(e);
|
||||
return ['failed', e];
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "mirai-middle-server",
|
||||
"version": "1.2.0",
|
||||
"version": "1.2.2",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "mirai-middle-server",
|
||||
"version": "1.2.0",
|
||||
"version": "1.2.2",
|
||||
"dependencies": {
|
||||
"dayjs": "^1.11.7",
|
||||
"mirai-js": "^2.8.11",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "mirai-middle-server",
|
||||
"version": "1.2.0",
|
||||
"version": "1.2.2",
|
||||
"description": "使用miraijs开发,用于连接kuginn等外部服务与mirai通信的中转服务器",
|
||||
"author": "mol",
|
||||
"main": "app.js",
|
||||
@ -20,4 +20,4 @@
|
||||
"devDependencies": {
|
||||
"dotenv": "^16.1.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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().textProcessor().done(dealCommon),
|
||||
);
|
||||
Bot.sendMessageToFriend(+process.env.MASTER, '厨娘系统 启动!');
|
||||
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