alemonjs 2.1.56 → 2.1.58
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/app/api/schedule.d.ts +17 -0
- package/lib/app/api/schedule.js +12 -0
- package/lib/app/hook-use/client.js +0 -1
- package/lib/app/hook-use/me.d.ts +1 -0
- package/lib/app/hook-use/me.js +9 -1
- package/lib/app/hook-use/request.js +0 -1
- package/lib/app/hook-use/user.js +0 -1
- package/lib/app/index.d.ts +2 -0
- package/lib/app/index.js +3 -2
- package/lib/app/load_modules/loadChild.js +4 -0
- package/lib/app/message-format-old.d.ts +2 -0
- package/lib/app/message-format-old.js +35 -136
- package/lib/app/message-format.d.ts +59 -32
- package/lib/app/message-format.js +288 -66
- package/lib/app/schedule-store.d.ts +20 -0
- package/lib/app/schedule-store.js +230 -0
- package/lib/cbp/connects/client.js +1 -1
- package/lib/client.js +1 -1
- package/lib/index.js +3 -2
- package/lib/server/routers/router.js +1 -3
- package/lib/types/index.d.ts +1 -0
- package/lib/types/message/embed.d.ts +29 -0
- package/lib/types/message/embed.js +1 -0
- package/lib/types/message/index.d.ts +7 -1
- package/lib/types/message/modal.d.ts +21 -0
- package/lib/types/message/modal.js +1 -0
- package/lib/types/message/select.d.ts +19 -0
- package/lib/types/message/select.js +1 -0
- package/lib/types/schedule/index.d.ts +23 -0
- package/lib/types/schedule/index.js +1 -0
- package/package.json +2 -1
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { ResultCode } from '../core/variable.js';
|
|
2
|
+
import { CronJob } from 'cron';
|
|
3
|
+
|
|
4
|
+
const scheduleMap = new Map();
|
|
5
|
+
const appDirMap = new Map();
|
|
6
|
+
const registerAppDir = (appName, mainDir) => {
|
|
7
|
+
appDirMap.set(appName, mainDir);
|
|
8
|
+
};
|
|
9
|
+
const unregisterAppDir = (appName) => {
|
|
10
|
+
appDirMap.delete(appName);
|
|
11
|
+
};
|
|
12
|
+
const extractCallerPaths = () => {
|
|
13
|
+
const stack = new Error().stack;
|
|
14
|
+
if (!stack) {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
const paths = [];
|
|
18
|
+
const lineRegex = /(?:file:\/\/|\(|\s)(\/[^):]+)/g;
|
|
19
|
+
let m;
|
|
20
|
+
while ((m = lineRegex.exec(stack)) !== null) {
|
|
21
|
+
paths.push(m[1]);
|
|
22
|
+
}
|
|
23
|
+
return paths;
|
|
24
|
+
};
|
|
25
|
+
const resolveAppName = () => {
|
|
26
|
+
const callerPaths = extractCallerPaths();
|
|
27
|
+
for (const filePath of callerPaths) {
|
|
28
|
+
for (const [name, dir] of appDirMap) {
|
|
29
|
+
if (filePath.startsWith(dir)) {
|
|
30
|
+
return name;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return 'main';
|
|
35
|
+
};
|
|
36
|
+
const generateId = () => {
|
|
37
|
+
return Date.now().toString(36) + Math.random().toString(36).substring(2, 15);
|
|
38
|
+
};
|
|
39
|
+
const safeInvoke = (callback, id, type) => {
|
|
40
|
+
try {
|
|
41
|
+
const result = callback();
|
|
42
|
+
if (result instanceof Promise) {
|
|
43
|
+
result.catch(err => {
|
|
44
|
+
logger.error({
|
|
45
|
+
code: ResultCode.Fail,
|
|
46
|
+
message: `Schedule ${type} error [${id}]: ${err?.message ?? err}`,
|
|
47
|
+
data: null
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
logger.error({
|
|
54
|
+
code: ResultCode.Fail,
|
|
55
|
+
message: `Schedule ${type} error [${id}]: ${err?.message ?? err}`,
|
|
56
|
+
data: null
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const scheduleInterval = (callback, ms, appName) => {
|
|
61
|
+
if (ms <= 0) {
|
|
62
|
+
throw new Error('Interval ms must be greater than 0');
|
|
63
|
+
}
|
|
64
|
+
const resolvedApp = appName ?? resolveAppName();
|
|
65
|
+
const id = generateId();
|
|
66
|
+
const timer = setInterval(() => {
|
|
67
|
+
safeInvoke(callback, id, 'interval');
|
|
68
|
+
}, ms);
|
|
69
|
+
const item = {
|
|
70
|
+
id,
|
|
71
|
+
type: 'interval',
|
|
72
|
+
status: 'active',
|
|
73
|
+
callback,
|
|
74
|
+
ms,
|
|
75
|
+
_timer: timer,
|
|
76
|
+
appName: resolvedApp,
|
|
77
|
+
createdAt: Date.now()
|
|
78
|
+
};
|
|
79
|
+
scheduleMap.set(id, item);
|
|
80
|
+
return id;
|
|
81
|
+
};
|
|
82
|
+
const scheduleTimeout = (callback, ms, appName) => {
|
|
83
|
+
if (ms < 0) {
|
|
84
|
+
throw new Error('Timeout ms must be >= 0');
|
|
85
|
+
}
|
|
86
|
+
const resolvedApp = appName ?? resolveAppName();
|
|
87
|
+
const id = generateId();
|
|
88
|
+
const timer = setTimeout(() => {
|
|
89
|
+
safeInvoke(callback, id, 'timeout');
|
|
90
|
+
const item = scheduleMap.get(id);
|
|
91
|
+
if (item) {
|
|
92
|
+
item.status = 'stopped';
|
|
93
|
+
item._timer = null;
|
|
94
|
+
}
|
|
95
|
+
scheduleMap.delete(id);
|
|
96
|
+
}, ms);
|
|
97
|
+
const item = {
|
|
98
|
+
id,
|
|
99
|
+
type: 'timeout',
|
|
100
|
+
status: 'active',
|
|
101
|
+
callback,
|
|
102
|
+
ms,
|
|
103
|
+
_timer: timer,
|
|
104
|
+
appName: resolvedApp,
|
|
105
|
+
createdAt: Date.now()
|
|
106
|
+
};
|
|
107
|
+
scheduleMap.set(id, item);
|
|
108
|
+
return id;
|
|
109
|
+
};
|
|
110
|
+
const scheduleCron = (expression, callback, appName) => {
|
|
111
|
+
const resolvedApp = appName ?? resolveAppName();
|
|
112
|
+
const id = generateId();
|
|
113
|
+
const job = CronJob.from({
|
|
114
|
+
cronTime: expression,
|
|
115
|
+
onTick: () => {
|
|
116
|
+
safeInvoke(callback, id, 'cron');
|
|
117
|
+
},
|
|
118
|
+
start: true
|
|
119
|
+
});
|
|
120
|
+
const item = {
|
|
121
|
+
id,
|
|
122
|
+
type: 'cron',
|
|
123
|
+
status: 'active',
|
|
124
|
+
callback,
|
|
125
|
+
cron: expression,
|
|
126
|
+
_timer: job,
|
|
127
|
+
appName: resolvedApp,
|
|
128
|
+
createdAt: Date.now()
|
|
129
|
+
};
|
|
130
|
+
scheduleMap.set(id, item);
|
|
131
|
+
return id;
|
|
132
|
+
};
|
|
133
|
+
const clearTimer = (item) => {
|
|
134
|
+
if (item._timer === null || item._timer === undefined) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (item.type === 'cron') {
|
|
138
|
+
void item._timer.stop();
|
|
139
|
+
}
|
|
140
|
+
else if (item.type === 'interval') {
|
|
141
|
+
clearInterval(item._timer);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
clearTimeout(item._timer);
|
|
145
|
+
}
|
|
146
|
+
item._timer = null;
|
|
147
|
+
};
|
|
148
|
+
const schedulePause = (id) => {
|
|
149
|
+
const item = scheduleMap.get(id);
|
|
150
|
+
if (item?.status !== 'active') {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
clearTimer(item);
|
|
154
|
+
item.status = 'paused';
|
|
155
|
+
return true;
|
|
156
|
+
};
|
|
157
|
+
const scheduleResume = (id) => {
|
|
158
|
+
const item = scheduleMap.get(id);
|
|
159
|
+
if (item?.status !== 'paused') {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
if (item.type === 'interval' && item.ms) {
|
|
163
|
+
item._timer = setInterval(() => {
|
|
164
|
+
safeInvoke(item.callback, id, 'interval');
|
|
165
|
+
}, item.ms);
|
|
166
|
+
item.status = 'active';
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
if (item.type === 'cron' && item.cron) {
|
|
170
|
+
const job = CronJob.from({
|
|
171
|
+
cronTime: item.cron,
|
|
172
|
+
onTick: () => {
|
|
173
|
+
safeInvoke(item.callback, id, 'cron');
|
|
174
|
+
},
|
|
175
|
+
start: true
|
|
176
|
+
});
|
|
177
|
+
item._timer = job;
|
|
178
|
+
item.status = 'active';
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
return false;
|
|
182
|
+
};
|
|
183
|
+
const scheduleCancel = (id) => {
|
|
184
|
+
const item = scheduleMap.get(id);
|
|
185
|
+
if (!item) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
clearTimer(item);
|
|
189
|
+
item.status = 'stopped';
|
|
190
|
+
scheduleMap.delete(id);
|
|
191
|
+
return true;
|
|
192
|
+
};
|
|
193
|
+
const scheduleCancelByApp = (appName) => {
|
|
194
|
+
let count = 0;
|
|
195
|
+
for (const [id, item] of scheduleMap) {
|
|
196
|
+
if (item.appName === appName) {
|
|
197
|
+
scheduleCancel(id);
|
|
198
|
+
count++;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return count;
|
|
202
|
+
};
|
|
203
|
+
const scheduleCancelAll = () => {
|
|
204
|
+
let count = 0;
|
|
205
|
+
for (const [id] of scheduleMap) {
|
|
206
|
+
scheduleCancel(id);
|
|
207
|
+
count++;
|
|
208
|
+
}
|
|
209
|
+
return count;
|
|
210
|
+
};
|
|
211
|
+
const scheduleList = (appName) => {
|
|
212
|
+
const result = [];
|
|
213
|
+
for (const [, item] of scheduleMap) {
|
|
214
|
+
if (appName && item.appName !== appName) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
result.push({
|
|
218
|
+
id: item.id,
|
|
219
|
+
type: item.type,
|
|
220
|
+
status: item.status,
|
|
221
|
+
ms: item.ms,
|
|
222
|
+
cron: item.cron,
|
|
223
|
+
appName: item.appName,
|
|
224
|
+
createdAt: item.createdAt
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
export { registerAppDir, scheduleCancel, scheduleCancelAll, scheduleCancelByApp, scheduleCron, scheduleInterval, scheduleList, schedulePause, scheduleResume, scheduleTimeout, unregisterAppDir };
|
|
@@ -16,7 +16,7 @@ import { createResult } from '../../core/utils.js';
|
|
|
16
16
|
import '../../app/hook-event-context.js';
|
|
17
17
|
import { apiResolves, apiTimeouts, actionResolves, actionTimeouts, FULL_RECEIVE_HEADER } from '../processor/config.js';
|
|
18
18
|
import { setDirectSend } from '../processor/transport.js';
|
|
19
|
-
import '
|
|
19
|
+
import 'cron';
|
|
20
20
|
import '../../app/event-utils.js';
|
|
21
21
|
import '../../app/message-api.js';
|
|
22
22
|
import { createWSConnector } from './base.js';
|
package/lib/client.js
CHANGED
|
@@ -27,7 +27,7 @@ import './app/event-middleware.js';
|
|
|
27
27
|
import './app/event-processor.js';
|
|
28
28
|
import './app/event-response.js';
|
|
29
29
|
import './app/hook-event-context.js';
|
|
30
|
-
import '
|
|
30
|
+
import 'cron';
|
|
31
31
|
import './app/event-utils.js';
|
|
32
32
|
import './app/message-api.js';
|
|
33
33
|
import './process/platform.js';
|
package/lib/index.js
CHANGED
|
@@ -40,9 +40,10 @@ export { useRole } from './app/hook-use/role.js';
|
|
|
40
40
|
export { useUser } from './app/hook-use/user.js';
|
|
41
41
|
export { useObserver, useSubscribe } from './app/hook-use/subscribe.js';
|
|
42
42
|
export { createEvent, useEvent } from './app/hook-use/event.js';
|
|
43
|
+
export { clearInterval, clearTimeout, listSchedule, pauseSchedule, resumeSchedule, setCron, setInterval, setTimeout } from './app/api/schedule.js';
|
|
43
44
|
export { getCurrentEvent, getCurrentNext, withEventContext } from './app/hook-event-context.js';
|
|
45
|
+
export { registerAppDir, scheduleCancel, scheduleCancelAll, scheduleCancelByApp, scheduleCron, scheduleInterval, scheduleList, schedulePause, scheduleResume, scheduleTimeout, unregisterAppDir } from './app/schedule-store.js';
|
|
44
46
|
export { createEventValue, createSelects, onSelects, onState, unChildren, unState, useState } from './app/event-utils.js';
|
|
45
47
|
export { MessageDirect, createDataFormat, format, getMessageIntent, sendToChannel, sendToUser } from './app/message-api.js';
|
|
46
|
-
export { Format, FormatButtonGroup, FormatMarkDown } from './app/message-format.js';
|
|
48
|
+
export { Format, FormatButtonGroup, FormatMarkDown, FormatModal, FormatSelect } from './app/message-format.js';
|
|
47
49
|
export { start } from './main.js';
|
|
48
|
-
export { Attachment, Audio, BT, Button, Image, ImageFile, ImageURL, Link, MD, Markdown, MarkdownOriginal, Mention, Text, Video } from './app/message-format-old.js';
|
|
@@ -5,9 +5,7 @@ import mime from 'mime-types';
|
|
|
5
5
|
import hello from './hello.html.js';
|
|
6
6
|
import { safePath, getModuelFile, formatPath, isValidPackageName } from './utils.js';
|
|
7
7
|
import { collectMiddlewares, runMiddlewares } from './middleware.js';
|
|
8
|
-
import { ResultCode } from '
|
|
9
|
-
import 'yaml';
|
|
10
|
-
import '../../core/utils.js';
|
|
8
|
+
import { ResultCode } from 'core';
|
|
11
9
|
import module$1 from 'module';
|
|
12
10
|
|
|
13
11
|
const initRequire = () => { };
|
package/lib/types/index.d.ts
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type DataEmbedField = {
|
|
2
|
+
name: string;
|
|
3
|
+
value: string;
|
|
4
|
+
inline?: boolean;
|
|
5
|
+
};
|
|
6
|
+
export type DataEmbedAuthor = {
|
|
7
|
+
name: string;
|
|
8
|
+
url?: string;
|
|
9
|
+
iconUrl?: string;
|
|
10
|
+
};
|
|
11
|
+
export type DataEmbedFooter = {
|
|
12
|
+
text: string;
|
|
13
|
+
iconUrl?: string;
|
|
14
|
+
};
|
|
15
|
+
export type DataEmbed = {
|
|
16
|
+
type: 'Embed';
|
|
17
|
+
value: {
|
|
18
|
+
title?: string;
|
|
19
|
+
description?: string;
|
|
20
|
+
url?: string;
|
|
21
|
+
color?: number;
|
|
22
|
+
image?: string;
|
|
23
|
+
thumbnail?: string;
|
|
24
|
+
author?: DataEmbedAuthor;
|
|
25
|
+
footer?: DataEmbedFooter;
|
|
26
|
+
fields?: DataEmbedField[];
|
|
27
|
+
timestamp?: string | number | Date;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -8,6 +8,9 @@ import { DataAttachment } from './attachment';
|
|
|
8
8
|
import { DataAudio } from './audio';
|
|
9
9
|
import { DataVideo } from './video';
|
|
10
10
|
import { DataMarkdownOriginal } from './markdown-raw';
|
|
11
|
+
import { DataSelect } from './select';
|
|
12
|
+
import { DataModal, DataTextInput } from './modal';
|
|
13
|
+
import { DataEmbed } from './embed';
|
|
11
14
|
export * from './text';
|
|
12
15
|
export * from './link';
|
|
13
16
|
export * from './mention';
|
|
@@ -18,5 +21,8 @@ export * from './markdown-raw';
|
|
|
18
21
|
export * from './attachment';
|
|
19
22
|
export * from './audio';
|
|
20
23
|
export * from './video';
|
|
21
|
-
export
|
|
24
|
+
export * from './select';
|
|
25
|
+
export * from './modal';
|
|
26
|
+
export * from './embed';
|
|
27
|
+
export type DataEnums = DataText | DataLink | DataImage | DataImageURL | DataImageFile | DataMention | DataButtonGroup | DataMarkDown | DataMarkdownOriginal | DataAttachment | DataAudio | DataVideo | DataSelect | DataModal | DataTextInput | DataEmbed;
|
|
22
28
|
export type MessageDataFormat = DataEnums[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type DataTextInput = {
|
|
2
|
+
type: 'TextInput';
|
|
3
|
+
value: string;
|
|
4
|
+
options: {
|
|
5
|
+
customId: string;
|
|
6
|
+
style?: 'short' | 'paragraph';
|
|
7
|
+
placeholder?: string;
|
|
8
|
+
minLength?: number;
|
|
9
|
+
maxLength?: number;
|
|
10
|
+
required?: boolean;
|
|
11
|
+
defaultValue?: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export type DataModal = {
|
|
15
|
+
type: 'Modal';
|
|
16
|
+
value: DataTextInput[];
|
|
17
|
+
options: {
|
|
18
|
+
customId: string;
|
|
19
|
+
title: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type DataSelectOption = {
|
|
2
|
+
label: string;
|
|
3
|
+
value: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
emoji?: string;
|
|
6
|
+
default?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export type DataSelect = {
|
|
9
|
+
type: 'Select';
|
|
10
|
+
value: DataSelectOption[];
|
|
11
|
+
options?: {
|
|
12
|
+
customId?: string;
|
|
13
|
+
placeholder?: string;
|
|
14
|
+
minValues?: number;
|
|
15
|
+
maxValues?: number;
|
|
16
|
+
kind?: 'string' | 'user' | 'role' | 'channel' | 'mentionable';
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type ScheduleId = string;
|
|
2
|
+
export type ScheduleType = 'interval' | 'timeout' | 'cron';
|
|
3
|
+
export type ScheduleStatus = 'active' | 'paused' | 'stopped';
|
|
4
|
+
export type CronExpression = string;
|
|
5
|
+
export type ScheduleCallback = () => void | Promise<void>;
|
|
6
|
+
export interface ScheduleItem {
|
|
7
|
+
id: ScheduleId;
|
|
8
|
+
type: ScheduleType;
|
|
9
|
+
status: ScheduleStatus;
|
|
10
|
+
callback: ScheduleCallback;
|
|
11
|
+
ms?: number;
|
|
12
|
+
cron?: CronExpression;
|
|
13
|
+
_timer?: any;
|
|
14
|
+
appName?: string;
|
|
15
|
+
createdAt: number;
|
|
16
|
+
}
|
|
17
|
+
export type ScheduleMap = Map<ScheduleId, ScheduleItem>;
|
|
18
|
+
export interface ScheduleTools {
|
|
19
|
+
setInterval: (callback: ScheduleCallback, ms: number) => ScheduleId;
|
|
20
|
+
setTimeout: (callback: ScheduleCallback, ms: number) => ScheduleId;
|
|
21
|
+
setCron: (expression: CronExpression, callback: ScheduleCallback) => ScheduleId;
|
|
22
|
+
cancel: (id: ScheduleId) => boolean;
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alemonjs",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.58",
|
|
4
4
|
"description": "bot script",
|
|
5
5
|
"author": "lemonade",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"axios": "^1.14.0",
|
|
32
32
|
"chalk": "^5.6.2",
|
|
33
33
|
"commander": "^13.1.0",
|
|
34
|
+
"cron": "^4.4.0",
|
|
34
35
|
"file-type": "21.0.0",
|
|
35
36
|
"flatted": "^3.3.3",
|
|
36
37
|
"koa": "^3.0.1",
|