koishi-plugin-gl-bot 0.0.10 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/gl/index.d.ts +9 -0
- package/lib/gl/index.js +9 -2
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -2
- package/lib/mcsManager/api.d.ts +15 -2
- package/lib/mcsManager/api.js +136 -1
- package/lib/mcsManager/bot.d.ts +5 -3
- package/lib/mcsManager/bot.js +17 -9
- package/lib/mcsManager/commands/ark/create.d.ts +15 -0
- package/lib/mcsManager/commands/ark/create.js +66 -0
- package/lib/mcsManager/commands/ark/list.d.ts +14 -0
- package/lib/mcsManager/commands/ark/list.js +35 -0
- package/lib/mcsManager/commands/ark/restart.d.ts +15 -0
- package/lib/mcsManager/commands/ark/restart.js +62 -0
- package/lib/mcsManager/commands/ark/start.d.ts +15 -0
- package/lib/mcsManager/commands/ark/start.js +54 -0
- package/lib/mcsManager/commands/ark/stop.d.ts +15 -0
- package/lib/mcsManager/commands/ark/stop.js +54 -0
- package/lib/mcsManager/commands/base.d.ts +21 -0
- package/lib/mcsManager/commands/base.js +44 -0
- package/lib/mcsManager/commands/create.d.ts +16 -0
- package/lib/mcsManager/commands/create.js +135 -0
- package/lib/mcsManager/commands/index.d.ts +10 -10
- package/lib/mcsManager/commands/index.js +19 -14
- package/lib/mcsManager/commands/list copy.d.ts +13 -0
- package/lib/mcsManager/commands/list copy.js +34 -0
- package/lib/mcsManager/commands/list.d.ts +6 -3
- package/lib/mcsManager/commands/list.js +18 -9
- package/lib/mcsManager/commands/mc/create.d.ts +15 -0
- package/lib/mcsManager/commands/mc/create.js +79 -0
- package/lib/mcsManager/commands/mc/list.d.ts +14 -0
- package/lib/mcsManager/commands/mc/list.js +39 -0
- package/lib/mcsManager/commands/mc/online.d.ts +24 -0
- package/lib/mcsManager/commands/mc/online.js +112 -0
- package/lib/mcsManager/commands/mc/restart copy.d.ts +15 -0
- package/lib/mcsManager/commands/mc/restart copy.js +62 -0
- package/lib/mcsManager/commands/mc/restart.d.ts +15 -0
- package/lib/mcsManager/commands/mc/restart.js +62 -0
- package/lib/mcsManager/commands/mc/start.d.ts +15 -0
- package/lib/mcsManager/commands/mc/start.js +54 -0
- package/lib/mcsManager/commands/mc/stop.d.ts +15 -0
- package/lib/mcsManager/commands/mc/stop.js +54 -0
- package/lib/mcsManager/commands/mc copy/create.d.ts +16 -0
- package/lib/mcsManager/commands/mc copy/create.js +135 -0
- package/lib/mcsManager/commands/mc copy/list.d.ts +14 -0
- package/lib/mcsManager/commands/mc copy/list.js +35 -0
- package/lib/mcsManager/commands/mc copy/restart.d.ts +15 -0
- package/lib/mcsManager/commands/mc copy/restart.js +62 -0
- package/lib/mcsManager/commands/mc copy/start.d.ts +15 -0
- package/lib/mcsManager/commands/mc copy/start.js +54 -0
- package/lib/mcsManager/commands/mc copy/stop.d.ts +15 -0
- package/lib/mcsManager/commands/mc copy/stop.js +54 -0
- package/lib/mcsManager/commands/restart.d.ts +7 -4
- package/lib/mcsManager/commands/restart.js +11 -10
- package/lib/mcsManager/commands/start copy.d.ts +14 -0
- package/lib/mcsManager/commands/start copy.js +53 -0
- package/lib/mcsManager/commands/start.d.ts +7 -4
- package/lib/mcsManager/commands/start.js +10 -9
- package/lib/mcsManager/commands/stop.d.ts +15 -0
- package/lib/mcsManager/commands/stop.js +54 -0
- package/lib/mcsManager/index.d.ts +4 -3
- package/lib/mcsManager/index.js +5 -2
- package/lib/mcsManager/json/createInstanceUpload.json +49 -0
- package/lib/mcsManager/panel.d.ts +1 -1
- package/lib/mcsManager/panel.js +1 -1
- package/lib/mcsManager/schedules/index.d.ts +8 -0
- package/lib/mcsManager/schedules/index.js +41 -0
- package/lib/mcsManager/schedules/mc.schedule.d.ts +11 -0
- package/lib/mcsManager/schedules/mc.schedule.js +15 -0
- package/lib/mcsManager/type.d.ts +102 -0
- package/lib/mcsManager/ws.js +1 -1
- package/lib/napCat/api.d.ts +21 -0
- package/lib/napCat/api.js +43 -0
- package/lib/napCat/config.d.ts +10 -0
- package/lib/napCat/config.js +16 -0
- package/lib/napCat/index.d.ts +16 -0
- package/lib/napCat/index.js +15 -0
- package/lib/queQiao/index.d.ts +1 -0
- package/lib/queQiao/index.js +68 -1
- package/lib/queQiao/locale/zh-CN.json +1 -1
- package/lib/queQiao/locale/zh-CN.yml +6 -6
- package/lib/utils/file.download.d.ts +47 -0
- package/lib/utils/file.download.js +142 -0
- package/lib/utils/file.examples.d.ts +12 -0
- package/lib/utils/file.examples.js +73 -0
- package/lib/utils/index.d.ts +7 -0
- package/lib/utils/index.js +58 -0
- package/lib/utils/napcat.file.d.ts +63 -0
- package/lib/utils/napcat.file.js +133 -0
- package/package.json +2 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = (ctx) => {
|
|
4
|
+
ctx.model.extend('mcUser', {
|
|
5
|
+
id: { type: 'unsigned', length: 8 },
|
|
6
|
+
nickname: 'string',
|
|
7
|
+
uuid: 'string',
|
|
8
|
+
lastTime: 'timestamp',
|
|
9
|
+
level: 'integer',
|
|
10
|
+
onlineTimeJSON: 'text',
|
|
11
|
+
}, {
|
|
12
|
+
primary: 'id',
|
|
13
|
+
autoInc: true,
|
|
14
|
+
});
|
|
15
|
+
};
|
package/lib/mcsManager/type.d.ts
CHANGED
|
@@ -113,3 +113,105 @@ export interface ServiceInstanceConnectAuth {
|
|
|
113
113
|
addr: string;
|
|
114
114
|
prefix: string;
|
|
115
115
|
}
|
|
116
|
+
export interface CreateInstanceData {
|
|
117
|
+
nickname?: string;
|
|
118
|
+
startCommand?: string;
|
|
119
|
+
stopCommand?: string;
|
|
120
|
+
cwd?: string;
|
|
121
|
+
ie?: string;
|
|
122
|
+
oe?: string;
|
|
123
|
+
processType?: string;
|
|
124
|
+
createDatetime?: string;
|
|
125
|
+
lastDatetime?: string;
|
|
126
|
+
type?: string;
|
|
127
|
+
tag?: string[];
|
|
128
|
+
maxSpace?: null;
|
|
129
|
+
endTime?: string;
|
|
130
|
+
docker?: {
|
|
131
|
+
containerName?: string;
|
|
132
|
+
image?: string;
|
|
133
|
+
ports?: string[];
|
|
134
|
+
extraVolumes?: string[];
|
|
135
|
+
networkMode?: 'bridge';
|
|
136
|
+
networkAliases?: string[];
|
|
137
|
+
cpusetCpus?: string;
|
|
138
|
+
workingDir?: '/data';
|
|
139
|
+
changeWorkdir?: false;
|
|
140
|
+
env?: [];
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
export type TerminalOption = {
|
|
144
|
+
haveColor: boolean;
|
|
145
|
+
pty: boolean;
|
|
146
|
+
ptyWindowCol: number;
|
|
147
|
+
ptyWindowRow: number;
|
|
148
|
+
};
|
|
149
|
+
export type EventTaskConfig = {
|
|
150
|
+
autoStart: boolean;
|
|
151
|
+
autoRestart: boolean;
|
|
152
|
+
autoRestartMaxTimes: number;
|
|
153
|
+
ignore: boolean;
|
|
154
|
+
};
|
|
155
|
+
export type DockerConfig = {
|
|
156
|
+
containerName: string;
|
|
157
|
+
image: string;
|
|
158
|
+
ports: unknown[];
|
|
159
|
+
extraVolumes: unknown[];
|
|
160
|
+
networkMode: string;
|
|
161
|
+
networkAliases: string[];
|
|
162
|
+
cpusetCpus: string;
|
|
163
|
+
workingDir: string;
|
|
164
|
+
env: unknown[];
|
|
165
|
+
changeWorkdir: boolean;
|
|
166
|
+
};
|
|
167
|
+
export type PingConfig = {
|
|
168
|
+
ip: string;
|
|
169
|
+
type: number;
|
|
170
|
+
};
|
|
171
|
+
export type ExtraServiceConfig = {
|
|
172
|
+
openFrpTunnelId: string;
|
|
173
|
+
openFrpToken: string;
|
|
174
|
+
};
|
|
175
|
+
export type CreateInstanceConfig = {
|
|
176
|
+
nickname: string;
|
|
177
|
+
startCommand: string;
|
|
178
|
+
stopCommand: string;
|
|
179
|
+
cwd: string;
|
|
180
|
+
ie: string;
|
|
181
|
+
oe: string;
|
|
182
|
+
createDatetime: number;
|
|
183
|
+
lastDatetime: number;
|
|
184
|
+
type: string;
|
|
185
|
+
tag: string[];
|
|
186
|
+
endTime: number;
|
|
187
|
+
fileCode: string;
|
|
188
|
+
processType: string;
|
|
189
|
+
updateCommand: string;
|
|
190
|
+
runAs: string;
|
|
191
|
+
actionCommandList: unknown[];
|
|
192
|
+
crlf: number;
|
|
193
|
+
category: number;
|
|
194
|
+
enableRcon: boolean;
|
|
195
|
+
rconPassword: string;
|
|
196
|
+
rconIp: string;
|
|
197
|
+
terminalOption: TerminalOption;
|
|
198
|
+
eventTask: EventTaskConfig;
|
|
199
|
+
docker: DockerConfig;
|
|
200
|
+
pingConfig: PingConfig;
|
|
201
|
+
extraServiceConfig: ExtraServiceConfig;
|
|
202
|
+
};
|
|
203
|
+
export interface McUser {
|
|
204
|
+
nickname: string;
|
|
205
|
+
uuid: string;
|
|
206
|
+
address: string;
|
|
207
|
+
health: number;
|
|
208
|
+
max_health: number;
|
|
209
|
+
experience_level: number;
|
|
210
|
+
experience_progress: number;
|
|
211
|
+
total_experience: number;
|
|
212
|
+
is_op: boolean;
|
|
213
|
+
walk_speed: number;
|
|
214
|
+
x: number;
|
|
215
|
+
y: number;
|
|
216
|
+
z: number;
|
|
217
|
+
}
|
package/lib/mcsManager/ws.js
CHANGED
|
@@ -62,7 +62,7 @@ class MCSManagerWebSocketIO {
|
|
|
62
62
|
reconnectionAttempts: 2000,
|
|
63
63
|
});
|
|
64
64
|
this.bindEvents();
|
|
65
|
-
logger.
|
|
65
|
+
logger.debug(`正在连接到 MCSManager 实例服务器 [${this.remote.remarks}] - ${this.instance.config.nickname} ...`);
|
|
66
66
|
}
|
|
67
67
|
// 发送MC指令到远程实例
|
|
68
68
|
sendCommand(command) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { HTTP } from 'koishi';
|
|
2
|
+
export declare class NapCatApi {
|
|
3
|
+
private http;
|
|
4
|
+
private baseUrl;
|
|
5
|
+
private token;
|
|
6
|
+
get requestHeaders(): {
|
|
7
|
+
authorization: string;
|
|
8
|
+
};
|
|
9
|
+
constructor(http: HTTP, baseUrl: string, token: string);
|
|
10
|
+
/**
|
|
11
|
+
* 获取文件信息
|
|
12
|
+
* @param fileId 文件ID
|
|
13
|
+
* @returns 文件信息或false
|
|
14
|
+
*/
|
|
15
|
+
getFile(fileId: string): Promise<false | {
|
|
16
|
+
file: string;
|
|
17
|
+
url: string;
|
|
18
|
+
file_size: string;
|
|
19
|
+
file_name: string;
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NapCatApi = void 0;
|
|
4
|
+
class NapCatApi {
|
|
5
|
+
get requestHeaders() {
|
|
6
|
+
return {
|
|
7
|
+
authorization: this.token,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
constructor(http, baseUrl, token) {
|
|
11
|
+
this.http = http;
|
|
12
|
+
this.baseUrl = baseUrl;
|
|
13
|
+
this.token = token;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 获取文件信息
|
|
17
|
+
* @param fileId 文件ID
|
|
18
|
+
* @returns 文件信息或false
|
|
19
|
+
*/
|
|
20
|
+
async getFile(fileId) {
|
|
21
|
+
console.log(`${this.baseUrl}/get_file`, {
|
|
22
|
+
file_id: fileId,
|
|
23
|
+
});
|
|
24
|
+
try {
|
|
25
|
+
const result = await this.http(`${this.baseUrl}/get_file`, {
|
|
26
|
+
method: 'POST',
|
|
27
|
+
headers: this.requestHeaders,
|
|
28
|
+
data: {
|
|
29
|
+
file_id: fileId,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
console.log(result?.data);
|
|
33
|
+
if (result.status === 200 && result.data) {
|
|
34
|
+
return result.data.data;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.error('Error fetching file:', error);
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.NapCatApi = NapCatApi;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Schema } from 'koishi';
|
|
2
|
+
export declare class NapCatConfig {
|
|
3
|
+
static Base: Schema<Schemastery.ObjectS<{
|
|
4
|
+
napCatBaseUrl: Schema<string, string>;
|
|
5
|
+
napCatToken: Schema<string, string>;
|
|
6
|
+
}>, Schemastery.ObjectT<{
|
|
7
|
+
napCatBaseUrl: Schema<string, string>;
|
|
8
|
+
napCatToken: Schema<string, string>;
|
|
9
|
+
}>>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NapCatConfig = void 0;
|
|
4
|
+
const koishi_1 = require("koishi");
|
|
5
|
+
class NapCatConfig {
|
|
6
|
+
}
|
|
7
|
+
exports.NapCatConfig = NapCatConfig;
|
|
8
|
+
NapCatConfig.Base = koishi_1.Schema.object({
|
|
9
|
+
napCatBaseUrl: koishi_1.Schema.string()
|
|
10
|
+
.default('http://localhost:3000')
|
|
11
|
+
.description('地址'),
|
|
12
|
+
napCatToken: koishi_1.Schema.string()
|
|
13
|
+
.role('secret')
|
|
14
|
+
.default('admin')
|
|
15
|
+
.description('密码'),
|
|
16
|
+
}).description('NapCat 基础配置');
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Context, Schema } from 'koishi';
|
|
2
|
+
import { GLBotConfigType } from '../gl';
|
|
3
|
+
import { NapCatApi } from './api';
|
|
4
|
+
export declare class NapCat {
|
|
5
|
+
private readonly ctx;
|
|
6
|
+
private readonly config;
|
|
7
|
+
static Config: Schema<Schemastery.ObjectS<{
|
|
8
|
+
napCatBaseUrl: Schema<string, string>;
|
|
9
|
+
napCatToken: Schema<string, string>;
|
|
10
|
+
}>, {
|
|
11
|
+
napCatBaseUrl: string;
|
|
12
|
+
napCatToken: string;
|
|
13
|
+
} & import("cosmokit").Dict>;
|
|
14
|
+
readonly api: NapCatApi;
|
|
15
|
+
constructor(ctx: Context, config: GLBotConfigType);
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NapCat = void 0;
|
|
4
|
+
const koishi_1 = require("koishi");
|
|
5
|
+
const api_1 = require("./api");
|
|
6
|
+
const config_1 = require("./config");
|
|
7
|
+
class NapCat {
|
|
8
|
+
constructor(ctx, config) {
|
|
9
|
+
this.ctx = ctx;
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.api = new api_1.NapCatApi(ctx.http, config.napCatBaseUrl, config.napCatToken);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.NapCat = NapCat;
|
|
15
|
+
NapCat.Config = koishi_1.Schema.intersect([config_1.NapCatConfig.Base]);
|
package/lib/queQiao/index.d.ts
CHANGED
package/lib/queQiao/index.js
CHANGED
|
@@ -9,6 +9,7 @@ const lodash_1 = require("lodash");
|
|
|
9
9
|
const rcon_client_1 = require("rcon-client");
|
|
10
10
|
const ws_1 = require("ws");
|
|
11
11
|
const constants_1 = require("../constants");
|
|
12
|
+
const online_1 = require("../mcsManager/commands/mc/online");
|
|
12
13
|
const utils_1 = require("../utils");
|
|
13
14
|
const mcwss_1 = __importDefault(require("./mcwss"));
|
|
14
15
|
const values_1 = require("./values");
|
|
@@ -120,8 +121,11 @@ class MinecraftSyncMsg {
|
|
|
120
121
|
logger.error('Failed to parse WebSocket message:', err);
|
|
121
122
|
return;
|
|
122
123
|
}
|
|
124
|
+
// 在线时间逻辑
|
|
125
|
+
this.updatePlayerOnlineTime(data);
|
|
123
126
|
const eventName = data.event_name ? (0, values_1.getListeningEvent)(data.event_name) : '';
|
|
124
|
-
if (eventName
|
|
127
|
+
if (!eventName ||
|
|
128
|
+
['PlayerCommandPreprocessEvent', 'PlayerDeathEvent'].includes(eventName)) {
|
|
125
129
|
return;
|
|
126
130
|
}
|
|
127
131
|
// console.log(data);
|
|
@@ -149,6 +153,69 @@ class MinecraftSyncMsg {
|
|
|
149
153
|
this.broadcastToChannels(sendMsg);
|
|
150
154
|
}
|
|
151
155
|
}
|
|
156
|
+
async updatePlayerOnlineTime(data) {
|
|
157
|
+
if (data.player) {
|
|
158
|
+
online_1.MCBotGameOnline.list[data.player?.uuid] = data.player;
|
|
159
|
+
}
|
|
160
|
+
console.log(data);
|
|
161
|
+
if (!['PlayerJoinEvent', 'PlayerQuitEvent'].includes(data.event_name)) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const user = await this.ctx.database.get('mcUser', {
|
|
165
|
+
uuid: data.player?.uuid,
|
|
166
|
+
});
|
|
167
|
+
online_1.MCBotGameOnline.list[data.player?.uuid] = data.player;
|
|
168
|
+
if (user.length === 0) {
|
|
169
|
+
await this.ctx.database.create('mcUser', {
|
|
170
|
+
nickname: data.player?.nickname,
|
|
171
|
+
uuid: data.player?.uuid,
|
|
172
|
+
lastTime: new Date(),
|
|
173
|
+
level: data.player?.level || 0,
|
|
174
|
+
onlineTimeJSON: JSON.stringify({
|
|
175
|
+
mc: {
|
|
176
|
+
[data.player?.uuid]: 0,
|
|
177
|
+
},
|
|
178
|
+
}),
|
|
179
|
+
});
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (data.event_name === 'PlayerJoinEvent') {
|
|
183
|
+
await this.ctx.database.set('mcUser', { uuid: data.player?.uuid }, {
|
|
184
|
+
nickname: data.player?.nickname,
|
|
185
|
+
level: data.player?.experience_level || 0,
|
|
186
|
+
lastTime: new Date(),
|
|
187
|
+
});
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
// 更新在线时间 (加入游戏时会更新lastTime,退出游戏时通过lastTime算出本次在线时间,加到onlineTimeJSON中)
|
|
191
|
+
if (data.event_name === 'PlayerQuitEvent') {
|
|
192
|
+
const lastTime = new Date(user[0].lastTime).getTime();
|
|
193
|
+
const nowTime = Date.now();
|
|
194
|
+
const onlineDuration = Math.floor((nowTime - lastTime) / 1000); // 在线时长,单位秒
|
|
195
|
+
delete online_1.MCBotGameOnline.list[user[0].uuid];
|
|
196
|
+
let onlineTimeJSON = {};
|
|
197
|
+
try {
|
|
198
|
+
onlineTimeJSON = JSON.parse(user[0].onlineTimeJSON);
|
|
199
|
+
}
|
|
200
|
+
catch (err) {
|
|
201
|
+
onlineTimeJSON = {};
|
|
202
|
+
}
|
|
203
|
+
if (!onlineTimeJSON.mc) {
|
|
204
|
+
onlineTimeJSON.mc = {};
|
|
205
|
+
}
|
|
206
|
+
if (!onlineTimeJSON.mc[data.player?.uuid]) {
|
|
207
|
+
onlineTimeJSON.mc[data.player?.uuid] = 0;
|
|
208
|
+
}
|
|
209
|
+
onlineTimeJSON.mc[data.player?.uuid] += onlineDuration;
|
|
210
|
+
await this.ctx.database.set('mcUser', { uuid: data.player?.uuid }, {
|
|
211
|
+
nickname: data.player?.nickname,
|
|
212
|
+
lastTime: new Date(),
|
|
213
|
+
level: data.player?.experience_level || 0,
|
|
214
|
+
onlineTimeJSON: JSON.stringify(onlineTimeJSON),
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
console.log({ user });
|
|
218
|
+
}
|
|
152
219
|
handleWsClose() {
|
|
153
220
|
if (this.isDisposing) {
|
|
154
221
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"minecraft-sync-msg":{"action":{"PlayerJoinEvent":"
|
|
1
|
+
{"minecraft-sync-msg":{"action":{"PlayerJoinEvent":"{0} 加入了服务器!","PlayerCommandPreprocessEvent":"[指令]{0} 执行了指令","PlayerDeathEvent":"{0} 与山长眠!","AsyncPlayerChatEvent":"[MC] {0}: {1}","PlayerQuitEvent":"{0} 离开了服务器!"},"message":{"MCReceivePrefix":"({0})[{1}]"}}}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
minecraft-sync-msg:
|
|
2
2
|
action:
|
|
3
|
-
PlayerJoinEvent:
|
|
4
|
-
PlayerCommandPreprocessEvent:
|
|
5
|
-
PlayerDeathEvent:
|
|
6
|
-
AsyncPlayerChatEvent:
|
|
7
|
-
PlayerQuitEvent:
|
|
3
|
+
PlayerJoinEvent: "{0} 加入了服务器!"
|
|
4
|
+
PlayerCommandPreprocessEvent: "[指令]{0} 执行了指令"
|
|
5
|
+
PlayerDeathEvent: "{0} 与山长眠!"
|
|
6
|
+
AsyncPlayerChatEvent: "[MC] {0}: {1}"
|
|
7
|
+
PlayerQuitEvent: "{0} 离开了服务器!"
|
|
8
8
|
message:
|
|
9
|
-
MCReceivePrefix:
|
|
9
|
+
MCReceivePrefix: "({0})[{1}]"
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Context } from 'koishi';
|
|
2
|
+
/**
|
|
3
|
+
* 文件下载工具
|
|
4
|
+
*/
|
|
5
|
+
export declare class FileDownloader {
|
|
6
|
+
private ctx;
|
|
7
|
+
constructor(ctx: Context);
|
|
8
|
+
/**
|
|
9
|
+
* 从消息内容中提取文件元素
|
|
10
|
+
* @param content 消息内容
|
|
11
|
+
* @returns 文件元素数组
|
|
12
|
+
*/
|
|
13
|
+
extractFileElements(content: string): {
|
|
14
|
+
src: string;
|
|
15
|
+
fileId: string;
|
|
16
|
+
fileSize?: number;
|
|
17
|
+
fileName?: string;
|
|
18
|
+
}[];
|
|
19
|
+
/**
|
|
20
|
+
* 下载文件到本地
|
|
21
|
+
* @param fileId 文件ID
|
|
22
|
+
* @param fileName 文件名
|
|
23
|
+
* @param downloadDir 下载目录
|
|
24
|
+
* @param bot 机器人实例(可选,如果平台支持直接获取文件URL)
|
|
25
|
+
* @returns 下载的文件路径
|
|
26
|
+
*/
|
|
27
|
+
downloadFile(fileId: string, fileName: string, downloadDir?: string, bot?: any): Promise<string | null>;
|
|
28
|
+
/**
|
|
29
|
+
* 从URL下载文件
|
|
30
|
+
* @param url 文件URL
|
|
31
|
+
* @param filePath 保存路径
|
|
32
|
+
* @returns 文件路径或null
|
|
33
|
+
*/
|
|
34
|
+
private downloadFromUrl;
|
|
35
|
+
/**
|
|
36
|
+
* 根据文件ID构造可能的文件URL
|
|
37
|
+
* @param fileId 文件ID
|
|
38
|
+
* @returns 可能的URL数组
|
|
39
|
+
*/
|
|
40
|
+
private constructPossibleFileUrls;
|
|
41
|
+
/**
|
|
42
|
+
* 格式化文件大小
|
|
43
|
+
* @param bytes 字节数
|
|
44
|
+
* @returns 格式化的文件大小
|
|
45
|
+
*/
|
|
46
|
+
formatFileSize(bytes: number): string;
|
|
47
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FileDownloader = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const koishi_1 = require("koishi");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const promises_1 = require("stream/promises");
|
|
8
|
+
/**
|
|
9
|
+
* 文件下载工具
|
|
10
|
+
*/
|
|
11
|
+
class FileDownloader {
|
|
12
|
+
constructor(ctx) {
|
|
13
|
+
this.ctx = ctx;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 从消息内容中提取文件元素
|
|
17
|
+
* @param content 消息内容
|
|
18
|
+
* @returns 文件元素数组
|
|
19
|
+
*/
|
|
20
|
+
extractFileElements(content) {
|
|
21
|
+
const fileElements = koishi_1.h.select(content, 'file');
|
|
22
|
+
return fileElements.map(element => ({
|
|
23
|
+
src: element.attrs?.src || '',
|
|
24
|
+
fileId: element.attrs?.['file-id'] || '',
|
|
25
|
+
fileSize: element.attrs?.['file-size']
|
|
26
|
+
? parseInt(element.attrs['file-size'])
|
|
27
|
+
: undefined,
|
|
28
|
+
fileName: element.attrs?.file || element.attrs?.src || '',
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 下载文件到本地
|
|
33
|
+
* @param fileId 文件ID
|
|
34
|
+
* @param fileName 文件名
|
|
35
|
+
* @param downloadDir 下载目录
|
|
36
|
+
* @param bot 机器人实例(可选,如果平台支持直接获取文件URL)
|
|
37
|
+
* @returns 下载的文件路径
|
|
38
|
+
*/
|
|
39
|
+
async downloadFile(fileId, fileName, downloadDir = './downloads', bot) {
|
|
40
|
+
try {
|
|
41
|
+
// 确保下载目录存在
|
|
42
|
+
if (!(0, fs_1.existsSync)(downloadDir)) {
|
|
43
|
+
(0, fs_1.mkdirSync)(downloadDir, { recursive: true });
|
|
44
|
+
}
|
|
45
|
+
const filePath = (0, path_1.join)(downloadDir, fileName);
|
|
46
|
+
// 方法1: 尝试通过bot获取文件URL(如果平台支持)
|
|
47
|
+
if (bot && typeof bot.getFileUrl === 'function') {
|
|
48
|
+
try {
|
|
49
|
+
const fileUrl = await bot.getFileUrl(fileId);
|
|
50
|
+
return await this.downloadFromUrl(fileUrl, filePath);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
console.warn('通过bot.getFileUrl下载失败,尝试其他方式:', error.message);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// 方法2: 尝试通过HTTP直接访问文件ID
|
|
57
|
+
if (fileId.startsWith('http://') || fileId.startsWith('https://')) {
|
|
58
|
+
return await this.downloadFromUrl(fileId, filePath);
|
|
59
|
+
}
|
|
60
|
+
// 方法3: 构造可能的文件URL(需要根据具体平台调整)
|
|
61
|
+
const possibleUrls = this.constructPossibleFileUrls(fileId);
|
|
62
|
+
for (const url of possibleUrls) {
|
|
63
|
+
try {
|
|
64
|
+
const result = await this.downloadFromUrl(url, filePath);
|
|
65
|
+
if (result) {
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
continue; // 尝试下一个URL
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
throw new Error('无法下载文件:所有下载方式都失败了');
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
console.error('文件下载失败:', error);
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 从URL下载文件
|
|
82
|
+
* @param url 文件URL
|
|
83
|
+
* @param filePath 保存路径
|
|
84
|
+
* @returns 文件路径或null
|
|
85
|
+
*/
|
|
86
|
+
async downloadFromUrl(url, filePath) {
|
|
87
|
+
try {
|
|
88
|
+
const response = await this.ctx.http.get(url, {
|
|
89
|
+
responseType: 'stream',
|
|
90
|
+
timeout: 60000, // 60秒超时
|
|
91
|
+
});
|
|
92
|
+
// 确保目录存在
|
|
93
|
+
const dir = (0, path_1.dirname)(filePath);
|
|
94
|
+
if (!(0, fs_1.existsSync)(dir)) {
|
|
95
|
+
(0, fs_1.mkdirSync)(dir, { recursive: true });
|
|
96
|
+
}
|
|
97
|
+
const writeStream = (0, fs_1.createWriteStream)(filePath);
|
|
98
|
+
await (0, promises_1.pipeline)(response, writeStream);
|
|
99
|
+
console.log(`文件下载成功: ${filePath}`);
|
|
100
|
+
return filePath;
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
console.error(`从 ${url} 下载文件失败:`, error);
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 根据文件ID构造可能的文件URL
|
|
109
|
+
* @param fileId 文件ID
|
|
110
|
+
* @returns 可能的URL数组
|
|
111
|
+
*/
|
|
112
|
+
constructPossibleFileUrls(fileId) {
|
|
113
|
+
const urls = [];
|
|
114
|
+
// 如果fileId就是完整URL
|
|
115
|
+
if (fileId.startsWith('http')) {
|
|
116
|
+
urls.push(fileId);
|
|
117
|
+
}
|
|
118
|
+
// QQ群文件的常见URL格式(需要根据实际情况调整)
|
|
119
|
+
if (fileId.startsWith('/')) {
|
|
120
|
+
// 可能的QQ文件服务器地址
|
|
121
|
+
urls.push(`https://groupfiles.qq.com${fileId}`);
|
|
122
|
+
urls.push(`https://grouptalk.c2c.qq.com${fileId}`);
|
|
123
|
+
}
|
|
124
|
+
// 其他平台的文件URL格式可以在这里添加
|
|
125
|
+
return urls;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 格式化文件大小
|
|
129
|
+
* @param bytes 字节数
|
|
130
|
+
* @returns 格式化的文件大小
|
|
131
|
+
*/
|
|
132
|
+
formatFileSize(bytes) {
|
|
133
|
+
if (bytes === 0) {
|
|
134
|
+
return '0 B';
|
|
135
|
+
}
|
|
136
|
+
const k = 1024;
|
|
137
|
+
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
138
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
139
|
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.FileDownloader = FileDownloader;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Session } from 'koishi';
|
|
2
|
+
/**
|
|
3
|
+
* Koishi 文件处理示例
|
|
4
|
+
*/
|
|
5
|
+
export declare function getFileFromSession(session: Session): {
|
|
6
|
+
src: any;
|
|
7
|
+
fileId: any;
|
|
8
|
+
fileName: any;
|
|
9
|
+
fileSize: any;
|
|
10
|
+
};
|
|
11
|
+
export declare function downloadFileFromSession(session: Session, fileId: string): Promise<string>;
|
|
12
|
+
export declare function processFileMessage(session: Session): Promise<string>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFileFromSession = getFileFromSession;
|
|
4
|
+
exports.downloadFileFromSession = downloadFileFromSession;
|
|
5
|
+
exports.processFileMessage = processFileMessage;
|
|
6
|
+
const koishi_1 = require("koishi");
|
|
7
|
+
/**
|
|
8
|
+
* Koishi 文件处理示例
|
|
9
|
+
*/
|
|
10
|
+
// 示例1: 直接从 session 中获取文件元素
|
|
11
|
+
function getFileFromSession(session) {
|
|
12
|
+
// 方法1: 使用 h.select 选择文件元素
|
|
13
|
+
const fileElements = koishi_1.h.select(session.elements, 'file');
|
|
14
|
+
for (const element of fileElements) {
|
|
15
|
+
const fileInfo = {
|
|
16
|
+
src: element.attrs?.src,
|
|
17
|
+
fileId: element.attrs?.['file-id'],
|
|
18
|
+
fileName: element.attrs?.file,
|
|
19
|
+
fileSize: element.attrs?.['file-size'],
|
|
20
|
+
};
|
|
21
|
+
console.log('检测到文件:', fileInfo);
|
|
22
|
+
return fileInfo;
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
// 示例2: 通过平台API下载文件
|
|
27
|
+
async function downloadFileFromSession(session, fileId) {
|
|
28
|
+
try {
|
|
29
|
+
// 不同平台的文件下载方法可能不同
|
|
30
|
+
// QQ平台示例
|
|
31
|
+
if (session.platform === 'onebot') {
|
|
32
|
+
// OneBot协议中可能需要调用get_group_file_url等API
|
|
33
|
+
// const fileUrl = await session.bot.internal.getGroupFileUrl(session.guildId, fileId);
|
|
34
|
+
// return fileUrl;
|
|
35
|
+
}
|
|
36
|
+
// Discord平台示例
|
|
37
|
+
if (session.platform === 'discord') {
|
|
38
|
+
// Discord文件通常直接有URL
|
|
39
|
+
return fileId; // 在Discord中,file-id通常就是直接的URL
|
|
40
|
+
}
|
|
41
|
+
// 通用方法:尝试直接访问fileId作为URL
|
|
42
|
+
if (fileId.startsWith('http')) {
|
|
43
|
+
return fileId;
|
|
44
|
+
}
|
|
45
|
+
throw new Error(`平台 ${session.platform} 不支持文件下载或文件ID格式不正确`);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
console.error('获取文件URL失败:', error);
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// 示例3: 完整的文件处理流程
|
|
53
|
+
async function processFileMessage(session) {
|
|
54
|
+
const fileInfo = getFileFromSession(session);
|
|
55
|
+
if (!fileInfo) {
|
|
56
|
+
return '未检测到文件';
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const fileUrl = await downloadFileFromSession(session, fileInfo.fileId);
|
|
60
|
+
// 使用 session.bot.ctx.http 下载文件
|
|
61
|
+
const response = await session.bot.ctx.http.get(fileUrl, {
|
|
62
|
+
responseType: 'arraybuffer',
|
|
63
|
+
});
|
|
64
|
+
// 这里你可以:
|
|
65
|
+
// 1. 保存文件到本地
|
|
66
|
+
// 2. 处理文件内容
|
|
67
|
+
// 3. 上传到其他服务
|
|
68
|
+
return `文件 "${fileInfo.fileName}" 下载成功,大小:${response.byteLength} 字节`;
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
return `文件下载失败:${error.message}`;
|
|
72
|
+
}
|
|
73
|
+
}
|
package/lib/utils/index.d.ts
CHANGED