-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrassPic.js
86 lines (81 loc) · 2.51 KB
/
grassPic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import fetch from 'node-fetch';
import plugin from '../../lib/plugins/plugin.js';
/**
* 是否启用开发模式
* @type {boolean}
*/
let devMode = false;
/**
* API基础URL
*/
let getPicUrl = `https://oss.grass.starxw.com/service/image?type=download`;
let getStatusUrl = `https://oss.grass.starxw.com/service/status`;
// let postUploadUrl = `https://oss.grass.starxw.com/service/upload`
// let getInfoUrl = `https://oss.grass.starxw.com/service/info`
if (devMode) {
getPicUrl += `&devmode=normal`;
getStatusUrl += `?devmode=normal`;
}
export class GrassPic extends plugin {
constructor() {
super({
name: '[草图大全]grassPic.js',
dsc: '发送草图',
event: 'message',
priority: 114,
rule: [
{
reg: '^(#)?生草$',
fnc: 'sendGrassPic'
},
{
reg: '^(#)?草图状态$',
fnc: 'sendServiceStatus'
}
]
})
}
/** 发送生草 */
async sendGrassPic(e) {
// 从api获取图片
const response = await fetch(getPicUrl);
// 判断请求是否成功
if (response.status !== 200) {
// 请求失败,发送错误信息
await e.reply('请求草图失败,请稍后重试。');
return;
}
// 获取图片数据
const imageData = await response.arrayBuffer();
// 转换为Buffer类型
const buffer = Buffer.from(imageData);
// 发送图片
await e.reply(segment.image(buffer), true);
}
/** 发送服务状态 */
async sendServiceStatus(e) {
try {
// 发送服务状态请求
const response = await fetch(getStatusUrl);
if (!response.ok) {
throw new Error(`请求草图服务状态失败: ${response.status} ${response.statusText}`);
}
// 解析服务状态
const data = await response.json();
const model = `
图库中图片的总数:${data.totalImage}
图库占用的空间大小:${data.totalImageSizeHuman}
审核队列的状态:${data.waitImage}
图片接口的累计请求数:${data.apiCount}
图片接口的今日请求数:${data.apiCountToday}
图片接口的今日流量:${data.apiFlowTodayHuman}
服务是否正常:${data.service ? '是' : '否'}
`;
// 发送服务状态
await e.reply(model, true);
} catch (error) {
// 发送请求错误信息
await e.reply(`请求草图服务状态失败: ${error.message}`);
}
}
}