alemonjs 2.1.47 → 2.1.49
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/event-processor-callHandler.js +3 -2
- package/lib/app/event-processor-cycleFiles.js +1 -1
- package/lib/app/event-processor.js +13 -13
- package/lib/app/event-utils.d.ts +14 -0
- package/lib/app/event-utils.js +86 -0
- package/lib/app/hook-event-context.d.ts +3 -0
- package/lib/app/hook-event-context.js +11 -0
- package/lib/app/hook-use/announce.d.ts +12 -0
- package/lib/app/hook-use/announce.js +49 -0
- package/lib/app/hook-use/channel.d.ts +24 -0
- package/lib/app/hook-use/channel.js +109 -0
- package/lib/app/hook-use/client.d.ts +2 -0
- package/lib/app/hook-use/client.js +49 -0
- package/lib/app/hook-use/common.d.ts +18 -0
- package/lib/app/hook-use/common.js +22 -0
- package/lib/app/hook-use/guild.d.ts +19 -0
- package/lib/app/hook-use/guild.js +102 -0
- package/lib/app/hook-use/history.d.ts +9 -0
- package/lib/app/hook-use/history.js +31 -0
- package/lib/app/hook-use/index.d.ts +15 -0
- package/lib/app/hook-use/index.js +15 -0
- package/lib/app/hook-use/me.d.ts +7 -0
- package/lib/app/hook-use/me.js +90 -0
- package/lib/app/hook-use/media.d.ts +23 -0
- package/lib/app/hook-use/media.js +62 -0
- package/lib/app/hook-use/member.d.ts +54 -0
- package/lib/app/hook-use/member.js +200 -0
- package/lib/app/hook-use/mention.d.ts +9 -0
- package/lib/app/hook-use/mention.js +88 -0
- package/lib/app/hook-use/message.d.ts +28 -0
- package/lib/app/hook-use/message.js +142 -0
- package/lib/app/hook-use/permission.d.ts +13 -0
- package/lib/app/hook-use/permission.js +49 -0
- package/lib/app/hook-use/reaction.d.ts +16 -0
- package/lib/app/hook-use/reaction.js +70 -0
- package/lib/app/hook-use/request.d.ts +14 -0
- package/lib/app/hook-use/request.js +53 -0
- package/lib/app/hook-use/role.d.ts +33 -0
- package/lib/app/hook-use/role.js +125 -0
- package/lib/app/hook-use/user.d.ts +6 -0
- package/lib/app/hook-use/user.js +39 -0
- package/lib/app/hook-use-api.d.ts +3 -3
- package/lib/app/hook-use-api.js +16 -13
- package/lib/app/hook-use-subscribe.d.ts +61 -20
- package/lib/app/hook-use-subscribe.js +21 -7
- package/lib/app/index.d.ts +3 -2
- package/lib/app/index.js +18 -3
- package/lib/app/message-api.d.ts +0 -6
- package/lib/app/message-api.js +1 -4
- package/lib/app/message-format-old.d.ts +2 -2
- package/lib/app/message-format.js +1 -1
- package/lib/cbp/connects/client.js +5 -4
- package/lib/client.js +3 -2
- package/lib/core/config.d.ts +37 -0
- package/lib/core/index.js +1 -1
- package/lib/core/utils.d.ts +2 -1
- package/lib/core/utils.js +12 -9
- package/lib/global.d.ts +3 -1
- package/lib/index.js +19 -4
- package/lib/types/message/markdown.d.ts +1 -1
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@ import 'fs';
|
|
|
2
2
|
import 'path';
|
|
3
3
|
import 'yaml';
|
|
4
4
|
import { showErrorModule } from '../core/utils.js';
|
|
5
|
+
import { withEventContext } from './hook-event-context.js';
|
|
5
6
|
|
|
6
7
|
const createCallHandler = valueEvent => {
|
|
7
8
|
const callHandler = (currents, nextEvent) => {
|
|
@@ -19,10 +20,10 @@ const createCallHandler = valueEvent => {
|
|
|
19
20
|
return;
|
|
20
21
|
}
|
|
21
22
|
try {
|
|
22
|
-
const res = await currents[index](valueEvent, (...cns) => {
|
|
23
|
+
const res = await withEventContext(valueEvent, () => currents[index](valueEvent, (...cns) => {
|
|
23
24
|
isNext = true;
|
|
24
25
|
nextEvent(...cns);
|
|
25
|
-
});
|
|
26
|
+
}));
|
|
26
27
|
if (res !== true) {
|
|
27
28
|
isClose = true;
|
|
28
29
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState } from './
|
|
1
|
+
import { useState } from './event-utils.js';
|
|
2
2
|
import { getCachedRegExp, showErrorModule } from '../core/utils.js';
|
|
3
3
|
import { EventMessageText } from '../core/variable.js';
|
|
4
4
|
import { ResponseMiddleware } from './store.js';
|
|
@@ -2,7 +2,7 @@ import { getConfigValue } from '../core/config.js';
|
|
|
2
2
|
import { processorRepeatedClearTimeMin, processorRepeatedClearTimeMax, processorRepeatedEventTime, processorRepeatedUserTime, processorRepeatedClearSize, processorMaxMapSize } from '../core/variable.js';
|
|
3
3
|
import { expendCycle } from './event-processor-cycle.js';
|
|
4
4
|
import { ProcessorEventAutoClearMap, ProcessorEventUserAutoClearMap } from './store.js';
|
|
5
|
-
import { getCachedRegExp, fastHash } from '../core/utils.js';
|
|
5
|
+
import { getCachedRegExp, matchIn, fastHash } from '../core/utils.js';
|
|
6
6
|
|
|
7
7
|
const filter = ({ Now, store, INTERVAL }, MessageId) => {
|
|
8
8
|
if (store.has(MessageId)) {
|
|
@@ -69,12 +69,12 @@ const onProcessor = (name, event, data) => {
|
|
|
69
69
|
if (disabledSelects[name]) {
|
|
70
70
|
return;
|
|
71
71
|
}
|
|
72
|
-
const disabledUserId = value?.disabled_user_id
|
|
73
|
-
if (event['UserId'] && disabledUserId
|
|
72
|
+
const disabledUserId = value?.disabled_user_id;
|
|
73
|
+
if (event['UserId'] && matchIn(disabledUserId, event['UserId'])) {
|
|
74
74
|
return;
|
|
75
75
|
}
|
|
76
|
-
const disabledUserKey = value?.disabled_user_key
|
|
77
|
-
if (event['UserKey'] && disabledUserKey
|
|
76
|
+
const disabledUserKey = value?.disabled_user_key;
|
|
77
|
+
if (event['UserKey'] && matchIn(disabledUserKey, event['UserKey'])) {
|
|
78
78
|
return;
|
|
79
79
|
}
|
|
80
80
|
const redirectRegular = value?.redirect_regular ?? value?.redirect_text_regular;
|
|
@@ -98,20 +98,20 @@ const onProcessor = (name, event, data) => {
|
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
-
const masterId = value?.master_id
|
|
102
|
-
const masterKey = value?.master_key
|
|
103
|
-
if (event['UserId'] && masterId
|
|
101
|
+
const masterId = value?.master_id;
|
|
102
|
+
const masterKey = value?.master_key;
|
|
103
|
+
if (event['UserId'] && matchIn(masterId, event['UserId'])) {
|
|
104
104
|
event['isMaster'] = true;
|
|
105
105
|
}
|
|
106
|
-
else if (event['UserKey'] && masterKey
|
|
106
|
+
else if (event['UserKey'] && matchIn(masterKey, event['UserKey'])) {
|
|
107
107
|
event['isMaster'] = true;
|
|
108
108
|
}
|
|
109
|
-
const botId = value?.bot_id
|
|
110
|
-
const botKey = value?.bot_key
|
|
111
|
-
if (event['UserId'] && botId
|
|
109
|
+
const botId = value?.bot_id;
|
|
110
|
+
const botKey = value?.bot_key;
|
|
111
|
+
if (event['UserId'] && matchIn(botId, event['UserId'])) {
|
|
112
112
|
event['isBot'] = true;
|
|
113
113
|
}
|
|
114
|
-
else if (event['UserKey'] && botKey
|
|
114
|
+
else if (event['UserKey'] && matchIn(botKey, event['UserKey'])) {
|
|
115
115
|
event['isBot'] = true;
|
|
116
116
|
}
|
|
117
117
|
const Now = Date.now();
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EventKeys } from '../types';
|
|
2
|
+
type BaseMap = {
|
|
3
|
+
[key: string]: unknown;
|
|
4
|
+
};
|
|
5
|
+
export declare const unChildren: (name?: string) => void;
|
|
6
|
+
export declare const onSelects: <T extends EventKeys[] | EventKeys>(values: T) => T;
|
|
7
|
+
export declare const createSelects: <T extends EventKeys[] | EventKeys>(values: T) => T;
|
|
8
|
+
export declare const createEventValue: <T extends keyof R, R extends BaseMap>(event: {
|
|
9
|
+
value: R[T];
|
|
10
|
+
}) => R[T];
|
|
11
|
+
export declare const useState: <T extends string>(name: T, defaultValue?: boolean) => [boolean, (value: boolean) => void];
|
|
12
|
+
export declare const onState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
|
|
13
|
+
export declare const unState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { getConfig } from '../core/config.js';
|
|
2
|
+
import { ChildrenApp, State, StateSubscribe } from './store.js';
|
|
3
|
+
import { ResultCode } from '../core/variable.js';
|
|
4
|
+
|
|
5
|
+
const unChildren = (name = 'main') => {
|
|
6
|
+
if (!name || typeof name !== 'string') {
|
|
7
|
+
logger.error({
|
|
8
|
+
code: ResultCode.FailParams,
|
|
9
|
+
message: 'Invalid name: name must be a string',
|
|
10
|
+
data: null
|
|
11
|
+
});
|
|
12
|
+
throw new Error('Invalid name: name must be a string');
|
|
13
|
+
}
|
|
14
|
+
const app = new ChildrenApp(name);
|
|
15
|
+
app.un();
|
|
16
|
+
};
|
|
17
|
+
const onSelects = (values) => values;
|
|
18
|
+
global.onSelects = onSelects;
|
|
19
|
+
const createSelects = onSelects;
|
|
20
|
+
const createEventValue = (event) => {
|
|
21
|
+
return event.value;
|
|
22
|
+
};
|
|
23
|
+
const useState = (name, defaultValue = true) => {
|
|
24
|
+
if (typeof name !== 'string') {
|
|
25
|
+
logger.error({
|
|
26
|
+
code: ResultCode.FailParams,
|
|
27
|
+
message: 'Invalid name: name must be a string',
|
|
28
|
+
data: null
|
|
29
|
+
});
|
|
30
|
+
throw new Error('Invalid name: name must be a string');
|
|
31
|
+
}
|
|
32
|
+
if (typeof defaultValue !== 'boolean') {
|
|
33
|
+
logger.error({
|
|
34
|
+
code: ResultCode.FailParams,
|
|
35
|
+
message: 'Invalid defaultValue: defaultValue must be a boolean',
|
|
36
|
+
data: null
|
|
37
|
+
});
|
|
38
|
+
throw new Error('Invalid defaultValue: defaultValue must be a boolean');
|
|
39
|
+
}
|
|
40
|
+
const state = new State(name, defaultValue);
|
|
41
|
+
const setValue = (value) => {
|
|
42
|
+
if (state.value === value) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
state.value = value;
|
|
46
|
+
const cfg = getConfig();
|
|
47
|
+
cfg.value.core ??= {};
|
|
48
|
+
cfg.value.core.state ??= [];
|
|
49
|
+
const cfgState = cfg.value.core.state;
|
|
50
|
+
const exists = cfgState.includes(name);
|
|
51
|
+
if (value && exists) {
|
|
52
|
+
cfg.value.core.state = cfg.value.core.state.filter((i) => i !== name);
|
|
53
|
+
}
|
|
54
|
+
else if (!value && !exists) {
|
|
55
|
+
cfg.value.core.state.push(name);
|
|
56
|
+
}
|
|
57
|
+
cfg.saveValue(cfg.value);
|
|
58
|
+
};
|
|
59
|
+
return [state.value, setValue];
|
|
60
|
+
};
|
|
61
|
+
const onState = (name, callback) => {
|
|
62
|
+
if (typeof callback !== 'function') {
|
|
63
|
+
logger.error({
|
|
64
|
+
code: ResultCode.FailParams,
|
|
65
|
+
message: 'Callback must be a function',
|
|
66
|
+
data: null
|
|
67
|
+
});
|
|
68
|
+
throw new Error('Callback must be a function');
|
|
69
|
+
}
|
|
70
|
+
const sub = new StateSubscribe(name);
|
|
71
|
+
sub.on(callback);
|
|
72
|
+
};
|
|
73
|
+
const unState = (name, callback) => {
|
|
74
|
+
if (typeof callback !== 'function') {
|
|
75
|
+
logger.error({
|
|
76
|
+
code: ResultCode.FailParams,
|
|
77
|
+
message: 'Callback must be a function',
|
|
78
|
+
data: null
|
|
79
|
+
});
|
|
80
|
+
throw new Error('Callback must be a function');
|
|
81
|
+
}
|
|
82
|
+
const sub = new StateSubscribe(name);
|
|
83
|
+
sub.un(callback);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export { createEventValue, createSelects, onSelects, onState, unChildren, unState, useState };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
|
|
3
|
+
const eventStore = new AsyncLocalStorage();
|
|
4
|
+
const withEventContext = (event, runner) => {
|
|
5
|
+
return eventStore.run(event, runner);
|
|
6
|
+
};
|
|
7
|
+
const getCurrentEvent = () => {
|
|
8
|
+
return eventStore.getStore();
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export { getCurrentEvent, withEventContext };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EventKeys, Events, Result } from './common';
|
|
2
|
+
export declare const useAnnounce: <T extends EventKeys>(event?: Events[T]) => readonly [{
|
|
3
|
+
set: (params: {
|
|
4
|
+
messageId: string;
|
|
5
|
+
channelId?: string;
|
|
6
|
+
guildId?: string;
|
|
7
|
+
}) => Promise<Result>;
|
|
8
|
+
remove: (params?: {
|
|
9
|
+
messageId?: string;
|
|
10
|
+
guildId?: string;
|
|
11
|
+
}) => Promise<Result>;
|
|
12
|
+
}];
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getEventOrThrow } from './common.js';
|
|
2
|
+
import { createResult } from '../../core/utils.js';
|
|
3
|
+
import { ResultCode } from '../../core/variable.js';
|
|
4
|
+
import { sendAction } from '../../cbp/processor/actions.js';
|
|
5
|
+
|
|
6
|
+
const useAnnounce = (event) => {
|
|
7
|
+
const valueEvent = getEventOrThrow(event);
|
|
8
|
+
const set = async (params) => {
|
|
9
|
+
const gid = params.guildId || valueEvent.GuildId;
|
|
10
|
+
if (!gid || !params.messageId) {
|
|
11
|
+
return createResult(ResultCode.FailParams, 'Missing GuildId or MessageId', null);
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
const results = await sendAction({
|
|
15
|
+
action: 'channel.announce',
|
|
16
|
+
payload: { GuildId: gid, params: { messageId: params.messageId, channelId: params.channelId } }
|
|
17
|
+
});
|
|
18
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
19
|
+
return result || createResult(ResultCode.Warn, 'Announce set not supported or failed', null);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return createResult(ResultCode.Fail, 'Failed to set announcement', null);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const remove = async (params) => {
|
|
26
|
+
const gid = params?.guildId || valueEvent.GuildId;
|
|
27
|
+
if (!gid) {
|
|
28
|
+
return createResult(ResultCode.FailParams, 'Missing GuildId', null);
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const results = await sendAction({
|
|
32
|
+
action: 'channel.announce',
|
|
33
|
+
payload: { GuildId: gid, params: { messageId: params?.messageId || 'all', remove: true } }
|
|
34
|
+
});
|
|
35
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
36
|
+
return result || createResult(ResultCode.Warn, 'Announce remove not supported or failed', null);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return createResult(ResultCode.Fail, 'Failed to remove announcement', null);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const announce = {
|
|
43
|
+
set,
|
|
44
|
+
remove
|
|
45
|
+
};
|
|
46
|
+
return [announce];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export { useAnnounce };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ChannelInfo, EventKeys, Events, Result } from './common';
|
|
2
|
+
export declare const useChannel: <T extends EventKeys>(event?: Events[T]) => readonly [{
|
|
3
|
+
info: (params?: {
|
|
4
|
+
channelId?: string;
|
|
5
|
+
}) => Promise<Result<ChannelInfo | null>>;
|
|
6
|
+
list: (params?: {
|
|
7
|
+
guildId?: string;
|
|
8
|
+
}) => Promise<Result<ChannelInfo[]>>;
|
|
9
|
+
create: (params: {
|
|
10
|
+
name: string;
|
|
11
|
+
type?: string;
|
|
12
|
+
parentId?: string;
|
|
13
|
+
guildId?: string;
|
|
14
|
+
}) => Promise<Result<ChannelInfo | null>>;
|
|
15
|
+
update: (params: {
|
|
16
|
+
channelId: string;
|
|
17
|
+
name?: string;
|
|
18
|
+
topic?: string;
|
|
19
|
+
position?: number;
|
|
20
|
+
}) => Promise<Result>;
|
|
21
|
+
delete: (params: {
|
|
22
|
+
channelId: string;
|
|
23
|
+
}) => Promise<Result>;
|
|
24
|
+
}];
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { getEventOrThrow } from './common.js';
|
|
2
|
+
import { createResult } from '../../core/utils.js';
|
|
3
|
+
import { ResultCode } from '../../core/variable.js';
|
|
4
|
+
import { sendAction } from '../../cbp/processor/actions.js';
|
|
5
|
+
|
|
6
|
+
const useChannel = (event) => {
|
|
7
|
+
const valueEvent = getEventOrThrow(event);
|
|
8
|
+
const info = async (params) => {
|
|
9
|
+
const cid = params?.channelId || valueEvent.ChannelId;
|
|
10
|
+
if (!cid) {
|
|
11
|
+
return createResult(ResultCode.FailParams, 'Missing ChannelId', null);
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
const results = await sendAction({
|
|
15
|
+
action: 'channel.info',
|
|
16
|
+
payload: { ChannelId: cid }
|
|
17
|
+
});
|
|
18
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
19
|
+
if (result) {
|
|
20
|
+
return createResult(ResultCode.Ok, 'Successfully retrieved channel info', result.data ?? null);
|
|
21
|
+
}
|
|
22
|
+
return createResult(ResultCode.Warn, 'No channel info found', null);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return createResult(ResultCode.Fail, 'Failed to get channel info', null);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const list = async (params) => {
|
|
29
|
+
const gid = params?.guildId || valueEvent.GuildId;
|
|
30
|
+
if (!gid) {
|
|
31
|
+
return createResult(ResultCode.FailParams, 'Missing GuildId', []);
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const results = await sendAction({
|
|
35
|
+
action: 'channel.list',
|
|
36
|
+
payload: { GuildId: gid }
|
|
37
|
+
});
|
|
38
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
39
|
+
if (result) {
|
|
40
|
+
return createResult(ResultCode.Ok, 'Successfully retrieved channel list', result.data ?? []);
|
|
41
|
+
}
|
|
42
|
+
return createResult(ResultCode.Warn, 'No channel list found', []);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return createResult(ResultCode.Fail, 'Failed to get channel list', []);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const create = async (params) => {
|
|
49
|
+
const gid = params.guildId || valueEvent.GuildId;
|
|
50
|
+
if (!gid) {
|
|
51
|
+
return createResult(ResultCode.FailParams, 'Missing GuildId', null);
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const results = await sendAction({
|
|
55
|
+
action: 'channel.create',
|
|
56
|
+
payload: { GuildId: gid, params: { name: params.name, type: params.type, parentId: params.parentId } }
|
|
57
|
+
});
|
|
58
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
59
|
+
return result
|
|
60
|
+
? createResult(ResultCode.Ok, 'Channel created', result.data ?? null)
|
|
61
|
+
: createResult(ResultCode.Warn, 'Create not supported or failed', null);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return createResult(ResultCode.Fail, 'Failed to create channel', null);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const update = async (params) => {
|
|
68
|
+
if (!params.channelId) {
|
|
69
|
+
return createResult(ResultCode.FailParams, 'Missing ChannelId', null);
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const results = await sendAction({
|
|
73
|
+
action: 'channel.update',
|
|
74
|
+
payload: { ChannelId: params.channelId, params: { name: params.name, topic: params.topic, position: params.position } }
|
|
75
|
+
});
|
|
76
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
77
|
+
return result || createResult(ResultCode.Warn, 'Update not supported or failed', null);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return createResult(ResultCode.Fail, 'Failed to update channel', null);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const remove = async (params) => {
|
|
84
|
+
if (!params.channelId) {
|
|
85
|
+
return createResult(ResultCode.FailParams, 'Missing ChannelId', null);
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
const results = await sendAction({
|
|
89
|
+
action: 'channel.delete',
|
|
90
|
+
payload: { ChannelId: params.channelId }
|
|
91
|
+
});
|
|
92
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
93
|
+
return result || createResult(ResultCode.Warn, 'Delete not supported or failed', null);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return createResult(ResultCode.Fail, 'Failed to delete channel', null);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const channel = {
|
|
100
|
+
info,
|
|
101
|
+
list,
|
|
102
|
+
create,
|
|
103
|
+
update,
|
|
104
|
+
delete: remove
|
|
105
|
+
};
|
|
106
|
+
return [channel];
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export { useChannel };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { ResultCode } from '../../core/variable.js';
|
|
2
|
+
import '../store.js';
|
|
3
|
+
import '../../core/utils.js';
|
|
4
|
+
import 'flatted';
|
|
5
|
+
import 'fs';
|
|
6
|
+
import 'path';
|
|
7
|
+
import 'yaml';
|
|
8
|
+
import '../../cbp/processor/config.js';
|
|
9
|
+
import { sendAPI } from '../../cbp/processor/api.js';
|
|
10
|
+
import '../message-format-old.js';
|
|
11
|
+
import { getCurrentEvent } from '../hook-event-context.js';
|
|
12
|
+
|
|
13
|
+
function useClient(eventOrClass, _ApiClass) {
|
|
14
|
+
let valueEvent;
|
|
15
|
+
if (eventOrClass !== undefined && typeof eventOrClass === 'function') {
|
|
16
|
+
valueEvent = getCurrentEvent();
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
valueEvent = eventOrClass ?? getCurrentEvent();
|
|
20
|
+
}
|
|
21
|
+
if (!valueEvent || typeof valueEvent !== 'object') {
|
|
22
|
+
logger.error({
|
|
23
|
+
code: ResultCode.FailParams,
|
|
24
|
+
message: 'Invalid event: event must be an object',
|
|
25
|
+
data: null
|
|
26
|
+
});
|
|
27
|
+
throw new Error('Invalid event: event must be an object');
|
|
28
|
+
}
|
|
29
|
+
const client = new Proxy({}, {
|
|
30
|
+
get(_target, prop) {
|
|
31
|
+
if (typeof prop === 'symbol') {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
return (...args) => {
|
|
35
|
+
return sendAPI({
|
|
36
|
+
action: 'client.api',
|
|
37
|
+
payload: {
|
|
38
|
+
event: valueEvent,
|
|
39
|
+
key: String(prop),
|
|
40
|
+
params: args
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return [client];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { useClient };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DataEnums, EventKeys, Events, User, GuildInfo, ChannelInfo, MemberInfo, RoleInfo, PaginationParams, PaginatedResult } from '../../types';
|
|
2
|
+
import { ResultCode } from '../../core/variable';
|
|
3
|
+
import { ChildrenApp } from '../store';
|
|
4
|
+
import { createResult, Result } from '../../core/utils';
|
|
5
|
+
import { sendAction } from '../../cbp/processor/actions';
|
|
6
|
+
import { sendAPI } from '../../cbp/processor/api';
|
|
7
|
+
import { Format } from '../message-format';
|
|
8
|
+
import { getCurrentEvent } from '../hook-event-context';
|
|
9
|
+
export type { DataEnums, EventKeys, Events, User, GuildInfo, ChannelInfo, MemberInfo, RoleInfo, PaginationParams, PaginatedResult, Result };
|
|
10
|
+
export { ResultCode, ChildrenApp, createResult, sendAction, sendAPI, Format, getCurrentEvent };
|
|
11
|
+
export type Options = {
|
|
12
|
+
UserId?: string;
|
|
13
|
+
UserKey?: string;
|
|
14
|
+
UserName?: string;
|
|
15
|
+
IsMaster?: boolean;
|
|
16
|
+
IsBot?: boolean;
|
|
17
|
+
};
|
|
18
|
+
export declare const getEventOrThrow: <T extends EventKeys>(event?: Events[T]) => Events[T];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ResultCode } from '../../core/variable.js';
|
|
2
|
+
export { ChildrenApp } from '../store.js';
|
|
3
|
+
export { createResult } from '../../core/utils.js';
|
|
4
|
+
export { sendAction } from '../../cbp/processor/actions.js';
|
|
5
|
+
export { sendAPI } from '../../cbp/processor/api.js';
|
|
6
|
+
export { Format } from '../message-format.js';
|
|
7
|
+
import { getCurrentEvent } from '../hook-event-context.js';
|
|
8
|
+
|
|
9
|
+
const getEventOrThrow = (event) => {
|
|
10
|
+
const currentEvent = event ?? getCurrentEvent();
|
|
11
|
+
if (!currentEvent || typeof currentEvent !== 'object') {
|
|
12
|
+
logger.error({
|
|
13
|
+
code: ResultCode.FailParams,
|
|
14
|
+
message: 'Invalid event: event must be an object',
|
|
15
|
+
data: null
|
|
16
|
+
});
|
|
17
|
+
throw new Error('Invalid event: event must be an object');
|
|
18
|
+
}
|
|
19
|
+
return currentEvent;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { ResultCode, getCurrentEvent, getEventOrThrow };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EventKeys, Events, GuildInfo, Result } from './common';
|
|
2
|
+
export declare const useGuild: <T extends EventKeys>(event?: Events[T]) => readonly [{
|
|
3
|
+
info: (params?: {
|
|
4
|
+
guildId?: string;
|
|
5
|
+
}) => Promise<Result<GuildInfo | null>>;
|
|
6
|
+
list: () => Promise<Result<GuildInfo[]>>;
|
|
7
|
+
update(params: {
|
|
8
|
+
name?: string;
|
|
9
|
+
guildId?: string;
|
|
10
|
+
}): Promise<Result>;
|
|
11
|
+
leave(params?: {
|
|
12
|
+
guildId?: string;
|
|
13
|
+
isDismiss?: boolean;
|
|
14
|
+
}): Promise<Result>;
|
|
15
|
+
mute(params: {
|
|
16
|
+
enable: boolean;
|
|
17
|
+
guildId?: string;
|
|
18
|
+
}): Promise<Result>;
|
|
19
|
+
}];
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { getEventOrThrow } from './common.js';
|
|
2
|
+
import { createResult } from '../../core/utils.js';
|
|
3
|
+
import { ResultCode } from '../../core/variable.js';
|
|
4
|
+
import { sendAction } from '../../cbp/processor/actions.js';
|
|
5
|
+
|
|
6
|
+
const useGuild = (event) => {
|
|
7
|
+
const valueEvent = getEventOrThrow(event);
|
|
8
|
+
const info = async (params) => {
|
|
9
|
+
const gid = params?.guildId || valueEvent.GuildId;
|
|
10
|
+
if (!gid) {
|
|
11
|
+
return createResult(ResultCode.FailParams, 'Missing GuildId', null);
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
const results = await sendAction({
|
|
15
|
+
action: 'guild.info',
|
|
16
|
+
payload: { GuildId: gid }
|
|
17
|
+
});
|
|
18
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
19
|
+
if (result) {
|
|
20
|
+
return createResult(ResultCode.Ok, 'Successfully retrieved guild info', result.data ?? null);
|
|
21
|
+
}
|
|
22
|
+
return createResult(ResultCode.Warn, 'No guild info found', null);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return createResult(ResultCode.Fail, 'Failed to get guild info', null);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const list = async () => {
|
|
29
|
+
try {
|
|
30
|
+
const results = await sendAction({
|
|
31
|
+
action: 'guild.list',
|
|
32
|
+
payload: {}
|
|
33
|
+
});
|
|
34
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
35
|
+
if (result) {
|
|
36
|
+
return createResult(ResultCode.Ok, 'Successfully retrieved guild list', result.data ?? []);
|
|
37
|
+
}
|
|
38
|
+
return createResult(ResultCode.Warn, 'No guild list found', []);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return createResult(ResultCode.Fail, 'Failed to get guild list', []);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const guild = {
|
|
45
|
+
info,
|
|
46
|
+
list,
|
|
47
|
+
async update(params) {
|
|
48
|
+
const gid = params.guildId || valueEvent.GuildId;
|
|
49
|
+
if (!gid) {
|
|
50
|
+
return createResult(ResultCode.FailParams, 'Missing GuildId', null);
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const results = await sendAction({
|
|
54
|
+
action: 'guild.update',
|
|
55
|
+
payload: { GuildId: gid, params: { name: params.name } }
|
|
56
|
+
});
|
|
57
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
58
|
+
return result || createResult(ResultCode.Warn, 'Update not supported or failed', null);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return createResult(ResultCode.Fail, 'Failed to update guild', null);
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
async leave(params) {
|
|
65
|
+
const gid = params?.guildId || valueEvent.GuildId;
|
|
66
|
+
if (!gid) {
|
|
67
|
+
return createResult(ResultCode.FailParams, 'Missing GuildId', null);
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const results = await sendAction({
|
|
71
|
+
action: 'guild.leave',
|
|
72
|
+
payload: { GuildId: gid, params: { isDismiss: params?.isDismiss } }
|
|
73
|
+
});
|
|
74
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
75
|
+
return result || createResult(ResultCode.Warn, 'Leave not supported or failed', null);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return createResult(ResultCode.Fail, 'Failed to leave guild', null);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
async mute(params) {
|
|
82
|
+
const gid = params.guildId || valueEvent.GuildId;
|
|
83
|
+
if (!gid) {
|
|
84
|
+
return createResult(ResultCode.FailParams, 'Missing GuildId', null);
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
const results = await sendAction({
|
|
88
|
+
action: 'guild.mute',
|
|
89
|
+
payload: { GuildId: gid, params: { enable: params.enable } }
|
|
90
|
+
});
|
|
91
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
92
|
+
return result || createResult(ResultCode.Warn, 'Mute not supported or failed', null);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return createResult(ResultCode.Fail, 'Failed to mute guild', null);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
return [guild];
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export { useGuild };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EventKeys, Events, Result } from './common';
|
|
2
|
+
export declare const useHistory: <T extends EventKeys>(event?: Events[T]) => readonly [{
|
|
3
|
+
list: (params?: {
|
|
4
|
+
channelId?: string;
|
|
5
|
+
limit?: number;
|
|
6
|
+
before?: string;
|
|
7
|
+
after?: string;
|
|
8
|
+
}) => Promise<Result>;
|
|
9
|
+
}];
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { getEventOrThrow } from './common.js';
|
|
2
|
+
import { createResult } from '../../core/utils.js';
|
|
3
|
+
import { ResultCode } from '../../core/variable.js';
|
|
4
|
+
import { sendAction } from '../../cbp/processor/actions.js';
|
|
5
|
+
|
|
6
|
+
const useHistory = (event) => {
|
|
7
|
+
const valueEvent = getEventOrThrow(event);
|
|
8
|
+
const list = async (params) => {
|
|
9
|
+
const cid = params?.channelId || valueEvent.ChannelId;
|
|
10
|
+
if (!cid) {
|
|
11
|
+
return createResult(ResultCode.FailParams, 'Missing ChannelId', null);
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
const results = await sendAction({
|
|
15
|
+
action: 'history.list',
|
|
16
|
+
payload: { ChannelId: cid, params: { limit: params?.limit, before: params?.before, after: params?.after } }
|
|
17
|
+
});
|
|
18
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
19
|
+
return result || createResult(ResultCode.Warn, 'History list not supported or failed', null);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return createResult(ResultCode.Fail, 'Failed to get message history', null);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const history = {
|
|
26
|
+
list
|
|
27
|
+
};
|
|
28
|
+
return [history];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { useHistory };
|