alemonjs 2.1.48 → 2.1.50
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-format.d.ts +19 -0
- package/lib/app/event-format.js +128 -0
- package/lib/app/event-processor-callHandler.js +3 -2
- package/lib/app/event-processor-cycleFiles.js +1 -1
- 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 +53 -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/event.d.ts +20 -0
- package/lib/app/hook-use/event.js +38 -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 +17 -0
- package/lib/app/hook-use/index.js +17 -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/subscribe.d.ts +73 -0
- package/lib/app/hook-use/subscribe.js +112 -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 +4 -3
- package/lib/app/index.js +21 -4
- package/lib/app/load_modules/load.js +1 -1
- package/lib/app/load_modules/loadChild.js +3 -3
- package/lib/app/message-api.d.ts +2 -8
- package/lib/app/message-api.js +7 -42
- package/lib/app/message-format.js +1 -1
- package/lib/cbp/connects/client.js +5 -4
- package/lib/cbp/connects/platform.js +3 -3
- package/lib/cbp/server/main.js +6 -6
- package/lib/client.js +3 -2
- package/lib/core/utils.js +5 -5
- package/lib/index.js +21 -4
- package/lib/process/module.js +3 -3
- package/lib/process/platform.js +3 -3
- package/lib/server/routers/router.js +59 -10
- package/lib/server/routers/utils.d.ts +2 -0
- package/lib/server/routers/utils.js +12 -1
- package/lib/types/event/base/platform.d.ts +1 -1
- package/lib/types/event/builder.d.ts +36 -0
- package/lib/types/event/builder.js +1 -0
- package/lib/types/event/channel/index.d.ts +4 -4
- package/lib/types/event/guild/index.d.ts +4 -4
- package/lib/types/event/interaction/index.d.ts +3 -3
- package/lib/types/event/member/index.d.ts +6 -6
- package/lib/types/event/message/message.d.ts +7 -7
- package/lib/types/event/message/private.message.d.ts +4 -4
- package/lib/types/event/notice/index.d.ts +3 -3
- package/lib/types/event/request/index.d.ts +4 -4
- package/lib/types/index.d.ts +1 -0
- package/lib/utils.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Events, EventKeys, EventBuilder, ReservedEventKeys, User, Guild, Channel, Message, MessageText, MessageMedia, MessageOpen, Platform } from '../types';
|
|
2
|
+
export declare class FormatEvent<T extends EventKeys = EventKeys> {
|
|
3
|
+
#private;
|
|
4
|
+
private constructor();
|
|
5
|
+
static create<T extends EventKeys>(name: T): EventBuilder<T>;
|
|
6
|
+
addPlatform(params: Platform): this;
|
|
7
|
+
addGuild(params: Guild): this;
|
|
8
|
+
addChannel(params: Channel): this;
|
|
9
|
+
addUser(params: User): this;
|
|
10
|
+
addMessage(params: Message): this;
|
|
11
|
+
addText(params: MessageText): this;
|
|
12
|
+
addMedia(params: MessageMedia): this;
|
|
13
|
+
addOpen(params: MessageOpen): this;
|
|
14
|
+
add<E extends Record<string, unknown>>(fields: {
|
|
15
|
+
[K in keyof E]: K extends ReservedEventKeys ? never : E[K];
|
|
16
|
+
}): this;
|
|
17
|
+
get value(): Events[T];
|
|
18
|
+
}
|
|
19
|
+
export declare function wrapEvent<E extends Record<string, unknown>>(event: object): Readonly<E>;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
class FormatEvent {
|
|
2
|
+
#data;
|
|
3
|
+
constructor(name) {
|
|
4
|
+
this.#data = { name, Timestamp: Date.now() };
|
|
5
|
+
}
|
|
6
|
+
static create(name) {
|
|
7
|
+
return new FormatEvent(name);
|
|
8
|
+
}
|
|
9
|
+
addPlatform(params) {
|
|
10
|
+
const { value, ...otherParams } = params;
|
|
11
|
+
Object.assign(this.#data, otherParams);
|
|
12
|
+
Object.defineProperty(this.#data, 'value', {
|
|
13
|
+
value,
|
|
14
|
+
enumerable: false
|
|
15
|
+
});
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
addGuild(params) {
|
|
19
|
+
Object.assign(this.#data, {
|
|
20
|
+
GuildId: params.GuildId,
|
|
21
|
+
SpaceId: params.SpaceId
|
|
22
|
+
});
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
addChannel(params) {
|
|
26
|
+
Object.assign(this.#data, {
|
|
27
|
+
ChannelId: params.ChannelId
|
|
28
|
+
});
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
addUser(params) {
|
|
32
|
+
Object.assign(this.#data, {
|
|
33
|
+
UserId: params.UserId,
|
|
34
|
+
UserKey: params.UserKey,
|
|
35
|
+
IsMaster: params.IsMaster,
|
|
36
|
+
IsBot: params.IsBot,
|
|
37
|
+
...(params.UserName !== undefined && { UserName: params.UserName }),
|
|
38
|
+
...(params.UserAvatar !== undefined && { UserAvatar: params.UserAvatar })
|
|
39
|
+
});
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
addMessage(params) {
|
|
43
|
+
const assign = { MessageId: params.MessageId };
|
|
44
|
+
if (params.ReplyId !== undefined) {
|
|
45
|
+
assign.ReplyId = params.ReplyId;
|
|
46
|
+
}
|
|
47
|
+
Object.assign(this.#data, assign);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
addText(params) {
|
|
51
|
+
Object.assign(this.#data, {
|
|
52
|
+
MessageText: params.MessageText
|
|
53
|
+
});
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
addMedia(params) {
|
|
57
|
+
Object.assign(this.#data, {
|
|
58
|
+
MessageMedia: params.MessageMedia
|
|
59
|
+
});
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
addOpen(params) {
|
|
63
|
+
Object.assign(this.#data, {
|
|
64
|
+
OpenId: params.OpenId
|
|
65
|
+
});
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
add(fields) {
|
|
69
|
+
for (const key of Object.keys(fields)) {
|
|
70
|
+
this.#data[`_${key}`] = fields[key];
|
|
71
|
+
}
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
get value() {
|
|
75
|
+
return this.#data;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function wrapEvent(event) {
|
|
79
|
+
return new Proxy(event, {
|
|
80
|
+
get(target, prop, receiver) {
|
|
81
|
+
if (typeof prop === 'string' && !prop.startsWith('_')) {
|
|
82
|
+
const privateKey = `_${prop}`;
|
|
83
|
+
if (privateKey in target) {
|
|
84
|
+
return Reflect.get(target, privateKey, receiver);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return Reflect.get(target, prop, receiver);
|
|
88
|
+
},
|
|
89
|
+
has(target, prop) {
|
|
90
|
+
if (typeof prop === 'string' && !prop.startsWith('_')) {
|
|
91
|
+
if (`_${prop}` in target) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return Reflect.has(target, prop);
|
|
96
|
+
},
|
|
97
|
+
set() {
|
|
98
|
+
return false;
|
|
99
|
+
},
|
|
100
|
+
deleteProperty() {
|
|
101
|
+
return false;
|
|
102
|
+
},
|
|
103
|
+
ownKeys(target) {
|
|
104
|
+
return Reflect.ownKeys(target).map(key => {
|
|
105
|
+
if (typeof key === 'string' && key.startsWith('_')) {
|
|
106
|
+
return key.slice(1);
|
|
107
|
+
}
|
|
108
|
+
return key;
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
112
|
+
if (typeof prop === 'string' && !prop.startsWith('_')) {
|
|
113
|
+
const privateKey = `_${prop}`;
|
|
114
|
+
const desc = Reflect.getOwnPropertyDescriptor(target, privateKey);
|
|
115
|
+
if (desc) {
|
|
116
|
+
return { ...desc, configurable: true, writable: false };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const desc = Reflect.getOwnPropertyDescriptor(target, prop);
|
|
120
|
+
if (desc) {
|
|
121
|
+
return { ...desc, writable: false };
|
|
122
|
+
}
|
|
123
|
+
return desc;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { FormatEvent, wrapEvent };
|
|
@@ -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';
|
|
@@ -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 { StateSubscribe, ChildrenApp, State } 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,53 @@
|
|
|
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
|
+
const createDeepProxy = (event, path = []) => {
|
|
14
|
+
return new Proxy((() => { }), {
|
|
15
|
+
get(_target, prop) {
|
|
16
|
+
if (typeof prop === 'symbol') {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
return createDeepProxy(event, [...path, String(prop)]);
|
|
20
|
+
},
|
|
21
|
+
apply(_target, _thisArg, args) {
|
|
22
|
+
return sendAPI({
|
|
23
|
+
action: 'client.api',
|
|
24
|
+
payload: {
|
|
25
|
+
event,
|
|
26
|
+
key: path.join('.'),
|
|
27
|
+
params: args
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
function useClient(eventOrClass, _ApiClass) {
|
|
34
|
+
let valueEvent;
|
|
35
|
+
if (eventOrClass !== undefined && typeof eventOrClass === 'function') {
|
|
36
|
+
valueEvent = getCurrentEvent();
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
valueEvent = eventOrClass ?? getCurrentEvent();
|
|
40
|
+
}
|
|
41
|
+
if (!valueEvent || typeof valueEvent !== 'object') {
|
|
42
|
+
logger.error({
|
|
43
|
+
code: ResultCode.FailParams,
|
|
44
|
+
message: 'Invalid event: event must be an object',
|
|
45
|
+
data: null
|
|
46
|
+
});
|
|
47
|
+
throw new Error('Invalid event: event must be an object');
|
|
48
|
+
}
|
|
49
|
+
const client = createDeepProxy(valueEvent);
|
|
50
|
+
return [client];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
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,20 @@
|
|
|
1
|
+
import { EventKeys, Events } from 'types';
|
|
2
|
+
type UseEventOptions<T extends EventKeys> = {
|
|
3
|
+
selects: T | T[];
|
|
4
|
+
regular?: RegExp;
|
|
5
|
+
prefix?: string;
|
|
6
|
+
exact?: string;
|
|
7
|
+
};
|
|
8
|
+
type UseEventResult<T extends EventKeys> = {
|
|
9
|
+
current: Events[T];
|
|
10
|
+
value: Events[T]['value'];
|
|
11
|
+
match: {
|
|
12
|
+
selects: boolean;
|
|
13
|
+
regular: boolean;
|
|
14
|
+
prefix: boolean;
|
|
15
|
+
exact: boolean;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export declare function useEvent<T extends EventKeys>(options?: UseEventOptions<T>): readonly [UseEventResult<T>];
|
|
19
|
+
export declare function useEvent<T extends EventKeys>(event?: Events[T], options?: UseEventOptions<T>): readonly [UseEventResult<T>];
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { getEventOrThrow } from './common.js';
|
|
2
|
+
|
|
3
|
+
const isOptions = (v) => {
|
|
4
|
+
return typeof v === 'object' && 'selects' in v;
|
|
5
|
+
};
|
|
6
|
+
function useEvent(eventOrOptions, options) {
|
|
7
|
+
let eventArg;
|
|
8
|
+
let opts;
|
|
9
|
+
if (eventOrOptions && isOptions(eventOrOptions)) {
|
|
10
|
+
opts = eventOrOptions;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
eventArg = eventOrOptions;
|
|
14
|
+
opts = options;
|
|
15
|
+
}
|
|
16
|
+
const { selects, regular, prefix, exact } = opts ?? {};
|
|
17
|
+
const eventValue = getEventOrThrow(eventArg);
|
|
18
|
+
const { name, MessageText } = eventValue;
|
|
19
|
+
const selectsArr = Array.isArray(selects) ? selects : [selects];
|
|
20
|
+
const match = {
|
|
21
|
+
selects: selectsArr.includes(name),
|
|
22
|
+
exact: !!(exact && MessageText === exact),
|
|
23
|
+
prefix: !!(prefix && MessageText?.startsWith(prefix)),
|
|
24
|
+
regular: !!(regular && MessageText && regular.test(MessageText))
|
|
25
|
+
};
|
|
26
|
+
const r = {
|
|
27
|
+
current: { ...eventValue },
|
|
28
|
+
match
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(r, 'value', {
|
|
31
|
+
get() {
|
|
32
|
+
return eventValue.value;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
return [r];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { useEvent };
|
|
@@ -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
|
+
}];
|