@kotori-bot/core 1.1.0 → 1.3.0
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/README.md +17 -0
- package/lib/components/config.d.ts +16 -0
- package/lib/components/config.js +39 -0
- package/lib/components/core.d.ts +31 -0
- package/lib/components/core.js +30 -0
- package/lib/components/index.d.ts +1 -0
- package/lib/components/index.js +17 -0
- package/lib/components/message.d.ts +25 -0
- package/lib/components/message.js +135 -0
- package/lib/components/modules.d.ts +12 -0
- package/lib/components/modules.js +127 -0
- package/lib/consts.d.ts +12 -8
- package/lib/consts.js +14 -8
- package/lib/context/context.d.ts +35 -0
- package/lib/context/context.js +111 -0
- package/lib/context/events.d.ts +18 -0
- package/lib/context/index.d.ts +5 -0
- package/lib/context/index.js +21 -0
- package/lib/context/modules.d.ts +39 -0
- package/lib/context/modules.js +65 -0
- package/lib/context/service.d.ts +16 -0
- package/lib/context/service.js +22 -0
- package/lib/context/symbols.d.ts +12 -0
- package/lib/context/symbols.js +18 -0
- package/lib/context/test.d.ts +6 -0
- package/lib/context/test.js +12 -0
- package/lib/index.d.ts +6 -11
- package/lib/index.js +13 -21
- package/lib/service/adapter.d.ts +44 -0
- package/lib/service/adapter.js +165 -0
- package/lib/service/api.d.ts +32 -0
- package/lib/{components → service}/api.js +12 -14
- package/lib/service/cache.d.ts +13 -0
- package/lib/service/cache.js +34 -0
- package/lib/service/elements.d.ts +12 -0
- package/lib/service/elements.js +39 -0
- package/lib/service/index.d.ts +4 -0
- package/lib/service/index.js +20 -0
- package/lib/service/service.d.ts +16 -0
- package/lib/service/service.js +22 -0
- package/lib/types/adapter.d.ts +22 -0
- package/lib/types/adapter.js +2 -0
- package/lib/types/config.d.ts +22 -0
- package/lib/types/config.js +2 -0
- package/lib/types/core.d.ts +7 -0
- package/lib/types/core.js +2 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/index.js +19 -0
- package/lib/types/message.d.ts +228 -0
- package/lib/types/message.js +17 -0
- package/lib/types/modules.d.ts +39 -0
- package/lib/types/modules.js +2 -0
- package/lib/types/service.d.ts +23 -0
- package/lib/types/service.js +2 -0
- package/lib/utils/command.d.ts +43 -0
- package/lib/utils/command.js +212 -0
- package/lib/utils/commandError.d.ts +7 -0
- package/lib/utils/commandError.js +13 -0
- package/lib/utils/container.d.ts +9 -0
- package/lib/utils/container.js +21 -0
- package/lib/utils/errror.d.ts +5 -21
- package/lib/utils/errror.js +9 -14
- package/lib/utils/factory.d.ts +7 -0
- package/lib/utils/factory.js +25 -0
- package/package.json +10 -7
- package/lib/base/command.d.ts +0 -20
- package/lib/base/command.js +0 -217
- package/lib/base/core.d.ts +0 -30
- package/lib/base/core.js +0 -52
- package/lib/base/events.d.ts +0 -13
- package/lib/base/events.js +0 -41
- package/lib/base/filter.d.ts +0 -1
- package/lib/base/internal.d.ts +0 -34
- package/lib/base/internal.js +0 -89
- package/lib/base/message.d.ts +0 -18
- package/lib/base/message.js +0 -177
- package/lib/base/modules.d.ts +0 -15
- package/lib/base/modules.js +0 -205
- package/lib/components/adapter.d.ts +0 -38
- package/lib/components/adapter.js +0 -128
- package/lib/components/api.d.ts +0 -35
- package/lib/components/elements.d.ts +0 -12
- package/lib/components/elements.js +0 -28
- package/lib/components/service.d.ts +0 -13
- package/lib/components/service.js +0 -7
- package/lib/context.d.ts +0 -13
- package/lib/context.js +0 -35
- package/lib/types.d.ts +0 -417
- package/lib/types.js +0 -81
- package/lib/utils/commandExtra.d.ts +0 -6
- package/lib/utils/commandExtra.js +0 -11
- package/lib/utils/i18n.d.ts +0 -13
- package/lib/utils/i18n.js +0 -65
- /package/lib/{base/filter.js → context/events.js} +0 -0
package/lib/base/core.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { obj } from '@kotori-bot/tools';
|
|
2
|
-
import { Parser } from 'tsukiko';
|
|
3
|
-
import { type AdapterConstructor, type BaseDir, type GlobalConfig, type GlobalOptions, type KotoriConfig, type PackageInfo } from '../types';
|
|
4
|
-
import type Api from '../components/api';
|
|
5
|
-
export declare const defaultConfig: {
|
|
6
|
-
baseDir: {
|
|
7
|
-
root: string;
|
|
8
|
-
modules: string;
|
|
9
|
-
};
|
|
10
|
-
config: {
|
|
11
|
-
global: {
|
|
12
|
-
lang: "ja_JP" | "en_US" | "zh_TW" | "zh_CN";
|
|
13
|
-
'command-prefix': string;
|
|
14
|
-
};
|
|
15
|
-
adapter: {};
|
|
16
|
-
};
|
|
17
|
-
options: {
|
|
18
|
-
env: string;
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
export declare class Core {
|
|
22
|
-
protected readonly adapterStack: obj<[AdapterConstructor, Parser<unknown>?]>;
|
|
23
|
-
protected readonly botStack: obj<Api[]>;
|
|
24
|
-
readonly baseDir: BaseDir;
|
|
25
|
-
readonly config: GlobalConfig;
|
|
26
|
-
readonly options: GlobalOptions;
|
|
27
|
-
readonly package: PackageInfo;
|
|
28
|
-
constructor(config?: KotoriConfig);
|
|
29
|
-
}
|
|
30
|
-
export default Core;
|
package/lib/base/core.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Core = exports.defaultConfig = void 0;
|
|
7
|
-
const tools_1 = require("@kotori-bot/tools");
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const types_1 = require("../types");
|
|
10
|
-
const errror_1 = require("../utils/errror");
|
|
11
|
-
const consts_1 = require("../consts");
|
|
12
|
-
exports.defaultConfig = {
|
|
13
|
-
baseDir: {
|
|
14
|
-
root: path_1.default.resolve(consts_1.DEFAULT_ROOT_DIR),
|
|
15
|
-
modules: path_1.default.resolve(consts_1.DEFAULT_MODULES_DIR),
|
|
16
|
-
},
|
|
17
|
-
config: {
|
|
18
|
-
global: {
|
|
19
|
-
lang: consts_1.DEFAULT_LANG,
|
|
20
|
-
'command-prefix': consts_1.DEFAULT_COMMAND_PREFIX,
|
|
21
|
-
},
|
|
22
|
-
adapter: {},
|
|
23
|
-
},
|
|
24
|
-
options: {
|
|
25
|
-
env: consts_1.DEFAULT_ENV,
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
class Core {
|
|
29
|
-
adapterStack = {};
|
|
30
|
-
botStack = {};
|
|
31
|
-
baseDir;
|
|
32
|
-
config;
|
|
33
|
-
options;
|
|
34
|
-
package;
|
|
35
|
-
constructor(config) {
|
|
36
|
-
const info = (0, tools_1.loadConfig)(path_1.default.join(__dirname, '../../package.json'));
|
|
37
|
-
if (!info || Object.values(info).length === 0)
|
|
38
|
-
throw new errror_1.CoreError('Cannot find kotori-bot package.json');
|
|
39
|
-
const result = types_1.packageInfoSchema.parseSafe(info);
|
|
40
|
-
if (!result.value)
|
|
41
|
-
throw new errror_1.CoreError(`File package.json format error: ${result.error.message}`);
|
|
42
|
-
this.package = result.data;
|
|
43
|
-
const handle = types_1.kotoriConfigSchema.parseSafe(config);
|
|
44
|
-
if (!handle.value)
|
|
45
|
-
throw new errror_1.CoreError('Unexpected error in parsing config (basedir,kotori,options)');
|
|
46
|
-
this.baseDir = handle.data.baseDir;
|
|
47
|
-
this.config = handle.data.config;
|
|
48
|
-
this.options = handle.data.options;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
exports.Core = Core;
|
|
52
|
-
exports.default = Core;
|
package/lib/base/events.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { EventType, EventCallback } from '../types';
|
|
2
|
-
import Core from './core';
|
|
3
|
-
type EventBeforeKeys<T> = T extends `before_${infer U}` ? U : never;
|
|
4
|
-
export declare class Events extends Core {
|
|
5
|
-
private eventStack;
|
|
6
|
-
emit<T extends keyof EventType>(type: T, data: Omit<EventType[T], 'type'>): void;
|
|
7
|
-
on<T extends keyof EventType>(type: T, callback: EventCallback<T>): void;
|
|
8
|
-
before<T extends EventBeforeKeys<keyof EventType>>(type: T, callback: EventCallback<`before_${T}`>): void;
|
|
9
|
-
once<T extends keyof EventType>(type: T, callback: EventCallback<T>): void;
|
|
10
|
-
off<T extends keyof EventType>(type: T, callback: EventCallback<T>): void;
|
|
11
|
-
offAll<T extends keyof EventType>(type: T): void;
|
|
12
|
-
}
|
|
13
|
-
export default Events;
|
package/lib/base/events.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Events = void 0;
|
|
7
|
-
const core_1 = __importDefault(require("./core"));
|
|
8
|
-
class Events extends core_1.default {
|
|
9
|
-
eventStack = [];
|
|
10
|
-
emit(type, data) {
|
|
11
|
-
const session = Object.assign(data, { type });
|
|
12
|
-
this.eventStack.filter(el => el.type === type).forEach(el => el.callback(session));
|
|
13
|
-
}
|
|
14
|
-
on(type, callback) {
|
|
15
|
-
// if (this.eventStack.filter(el => el.callback === callback && el.type === type).length > 0) return false;
|
|
16
|
-
this.eventStack.push({ type, callback: callback });
|
|
17
|
-
}
|
|
18
|
-
before(type, callback) {
|
|
19
|
-
this.on(`before_${type}`, callback);
|
|
20
|
-
}
|
|
21
|
-
once(type, callback) {
|
|
22
|
-
const removeSelf = data => {
|
|
23
|
-
const handleArr = this.eventStack.filter(el => el.type !== type && el.callback !== removeSelf);
|
|
24
|
-
this.eventStack = handleArr;
|
|
25
|
-
callback(data);
|
|
26
|
-
};
|
|
27
|
-
this.on(type, removeSelf);
|
|
28
|
-
}
|
|
29
|
-
off(type, callback) {
|
|
30
|
-
const handleArr = this.eventStack.filter(el => el.callback !== callback && el.type !== type);
|
|
31
|
-
// if (this.eventStack.length === handleArr.length) return false;
|
|
32
|
-
this.eventStack = handleArr;
|
|
33
|
-
}
|
|
34
|
-
offAll(type) {
|
|
35
|
-
const handleArr = this.eventStack.filter(el => el.type !== type);
|
|
36
|
-
// if (this.eventStack.length === handleArr.length) return false;
|
|
37
|
-
this.eventStack = handleArr;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.Events = Events;
|
|
41
|
-
exports.default = Events;
|
package/lib/base/filter.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/base/internal.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import Message from './message';
|
|
2
|
-
export declare class Internal extends Message {
|
|
3
|
-
private getModules;
|
|
4
|
-
private setModules;
|
|
5
|
-
private getAdapters;
|
|
6
|
-
private setAdapters;
|
|
7
|
-
private getBots;
|
|
8
|
-
private setBots;
|
|
9
|
-
private getCommands;
|
|
10
|
-
private setCommands;
|
|
11
|
-
private getCommandData;
|
|
12
|
-
private setCommandData;
|
|
13
|
-
private getMidwares;
|
|
14
|
-
private setMidwares;
|
|
15
|
-
private getRegexps;
|
|
16
|
-
private setRegexps;
|
|
17
|
-
get internal(): {
|
|
18
|
-
getModules: () => import("..").ModuleData[];
|
|
19
|
-
setModules: (key: number, value: import("..").ModuleData) => void;
|
|
20
|
-
getAdapters: () => import("@kotori-bot/tools").obj<[import("..").AdapterConstructor, (import("tsukiko").Parser<unknown> | undefined)?]>;
|
|
21
|
-
setAdapters: (key: string, value: [import("..").AdapterConstructor, (import("tsukiko").Parser<unknown> | undefined)?]) => void;
|
|
22
|
-
getBots: () => import("@kotori-bot/tools").obj<import("..").Api[]>;
|
|
23
|
-
setBots: (key: string, value: import("..").Api[]) => void;
|
|
24
|
-
getCommands: () => import("..").CommandStack[];
|
|
25
|
-
setCommands: (key: number, value: import("..").CommandStack) => void;
|
|
26
|
-
getCommandData: () => import("..").CommandData[];
|
|
27
|
-
setCommandData: (key: number, value: import("..").CommandData) => void;
|
|
28
|
-
getMidwares: () => import("..").MidwareStack[];
|
|
29
|
-
setMidwares: (key: number, value: import("..").MidwareStack) => void;
|
|
30
|
-
getRegexps: () => import("..").RegexpStack[];
|
|
31
|
-
setRegexps: (key: number, value: import("..").RegexpStack) => void;
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
export default Internal;
|
package/lib/base/internal.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Internal = void 0;
|
|
7
|
-
const tools_1 = require("@kotori-bot/tools");
|
|
8
|
-
const message_1 = __importDefault(require("./message"));
|
|
9
|
-
const command_1 = __importDefault(require("./command"));
|
|
10
|
-
class Internal extends message_1.default {
|
|
11
|
-
getModules() {
|
|
12
|
-
return this.moduleStack;
|
|
13
|
-
}
|
|
14
|
-
setModules(key, value) {
|
|
15
|
-
this.moduleStack[key] = value;
|
|
16
|
-
}
|
|
17
|
-
getAdapters() {
|
|
18
|
-
return this.adapterStack;
|
|
19
|
-
}
|
|
20
|
-
setAdapters(key, value) {
|
|
21
|
-
this.adapterStack[key] = value;
|
|
22
|
-
}
|
|
23
|
-
getBots() {
|
|
24
|
-
return this.botStack;
|
|
25
|
-
}
|
|
26
|
-
setBots(key, value) {
|
|
27
|
-
this.botStack[key] = value;
|
|
28
|
-
}
|
|
29
|
-
getCommands() {
|
|
30
|
-
return this.commandStack;
|
|
31
|
-
}
|
|
32
|
-
setCommands(key, value) {
|
|
33
|
-
this.commandStack[key] = value;
|
|
34
|
-
}
|
|
35
|
-
getCommandData() {
|
|
36
|
-
(0, tools_1.none)(this);
|
|
37
|
-
return command_1.default.dataList;
|
|
38
|
-
}
|
|
39
|
-
setCommandData(key, value) {
|
|
40
|
-
(0, tools_1.none)(this);
|
|
41
|
-
command_1.default.dataList[key] = value;
|
|
42
|
-
}
|
|
43
|
-
getMidwares() {
|
|
44
|
-
return this.midwareStack;
|
|
45
|
-
}
|
|
46
|
-
setMidwares(key, value) {
|
|
47
|
-
this.midwareStack[key] = value;
|
|
48
|
-
}
|
|
49
|
-
getRegexps() {
|
|
50
|
-
return this.regexpStack;
|
|
51
|
-
}
|
|
52
|
-
setRegexps(key, value) {
|
|
53
|
-
this.regexpStack[key] = value;
|
|
54
|
-
}
|
|
55
|
-
get internal() {
|
|
56
|
-
const getModules = () => this.getModules();
|
|
57
|
-
const setModules = (key, value) => this.setModules(key, value);
|
|
58
|
-
const getAdapters = () => this.getAdapters();
|
|
59
|
-
const setAdapters = (key, value) => this.setAdapters(key, value);
|
|
60
|
-
const getBots = () => this.getBots();
|
|
61
|
-
const setBots = (key, value) => this.setBots(key, value);
|
|
62
|
-
const getCommands = () => this.getCommands();
|
|
63
|
-
const setCommands = (key, value) => this.setCommands(key, value);
|
|
64
|
-
const getCommandData = () => this.getCommandData();
|
|
65
|
-
const setCommandData = (key, value) => this.setCommandData(key, value);
|
|
66
|
-
const getMidwares = () => this.getMidwares();
|
|
67
|
-
const setMidwares = (key, value) => this.setMidwares(key, value);
|
|
68
|
-
const getRegexps = () => this.getRegexps();
|
|
69
|
-
const setRegexps = (key, value) => this.setRegexps(key, value);
|
|
70
|
-
return {
|
|
71
|
-
getModules,
|
|
72
|
-
setModules,
|
|
73
|
-
getAdapters,
|
|
74
|
-
setAdapters,
|
|
75
|
-
getBots,
|
|
76
|
-
setBots,
|
|
77
|
-
getCommands,
|
|
78
|
-
setCommands,
|
|
79
|
-
getCommandData,
|
|
80
|
-
setCommandData,
|
|
81
|
-
getMidwares,
|
|
82
|
-
setMidwares,
|
|
83
|
-
getRegexps,
|
|
84
|
-
setRegexps,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
exports.Internal = Internal;
|
|
89
|
-
exports.default = Internal;
|
package/lib/base/message.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { CommandConfig, MessageRaw, MessageScope, MidwareStack, CommandStack, RegexpStack, MidwareCallback, RegexpCallback } from '../types';
|
|
2
|
-
import Modules from './modules';
|
|
3
|
-
import Command from './command';
|
|
4
|
-
export declare class Message extends Modules {
|
|
5
|
-
protected readonly midwareStack: MidwareStack[];
|
|
6
|
-
protected readonly commandStack: CommandStack[];
|
|
7
|
-
protected readonly regexpStack: RegexpStack[];
|
|
8
|
-
private handleMessageEvent;
|
|
9
|
-
private handleMidwaresEvent;
|
|
10
|
-
private handleUnloadModuleEvent;
|
|
11
|
-
protected registeMessageEvent(): void;
|
|
12
|
-
midware(callback: MidwareCallback, priority?: number): boolean;
|
|
13
|
-
command(template: string, config?: CommandConfig): Command;
|
|
14
|
-
regexp(match: RegExp, callback: RegexpCallback): boolean;
|
|
15
|
-
boardcast(type: MessageScope, message: MessageRaw): void;
|
|
16
|
-
notify(message: MessageRaw): void;
|
|
17
|
-
}
|
|
18
|
-
export default Message;
|
package/lib/base/message.js
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Message = void 0;
|
|
7
|
-
const tsukiko_1 = __importDefault(require("tsukiko"));
|
|
8
|
-
const tools_1 = require("@kotori-bot/tools");
|
|
9
|
-
const modules_1 = __importDefault(require("./modules"));
|
|
10
|
-
const command_1 = __importDefault(require("./command"));
|
|
11
|
-
const errror_1 = require("../utils/errror");
|
|
12
|
-
const commandExtra_1 = __importDefault(require("../utils/commandExtra"));
|
|
13
|
-
class Message extends modules_1.default {
|
|
14
|
-
midwareStack = [];
|
|
15
|
-
/* two commands data array kill them! */
|
|
16
|
-
commandStack = [];
|
|
17
|
-
regexpStack = [];
|
|
18
|
-
handleMessageEvent(session) {
|
|
19
|
-
/* Handle middle wares */
|
|
20
|
-
let isPass = true;
|
|
21
|
-
const midwareStack = Object.create(this.midwareStack);
|
|
22
|
-
let lastMidwareNum = -1;
|
|
23
|
-
while (midwareStack.length > 0) {
|
|
24
|
-
if (lastMidwareNum === midwareStack.length) {
|
|
25
|
-
isPass = false;
|
|
26
|
-
break;
|
|
27
|
-
}
|
|
28
|
-
lastMidwareNum = midwareStack.length;
|
|
29
|
-
session.quick(midwareStack[0].callback(() => midwareStack.shift(), session));
|
|
30
|
-
}
|
|
31
|
-
this.emit('midwares', { isPass, event: session });
|
|
32
|
-
}
|
|
33
|
-
async handleMidwaresEvent(session) {
|
|
34
|
-
const { isPass, event } = session;
|
|
35
|
-
event.api.adapter.status.receivedMsg += 1;
|
|
36
|
-
if (!isPass)
|
|
37
|
-
return;
|
|
38
|
-
/* Handle regexp */
|
|
39
|
-
this.regexpStack.forEach(element => {
|
|
40
|
-
const match = event.message.match(element.match);
|
|
41
|
-
if (!match)
|
|
42
|
-
return;
|
|
43
|
-
event.quick(element.callback(match, event));
|
|
44
|
-
});
|
|
45
|
-
/* Handle command */
|
|
46
|
-
const params = [event.message, event.api.adapter.config['command-prefix']];
|
|
47
|
-
if (!params[0].startsWith(params[1]))
|
|
48
|
-
return;
|
|
49
|
-
const commonParams = {
|
|
50
|
-
event,
|
|
51
|
-
command: (0, tools_1.stringRightSplit)(params[0], params[1]),
|
|
52
|
-
};
|
|
53
|
-
this.emit('before_parse', commonParams);
|
|
54
|
-
let isCancel = false;
|
|
55
|
-
const cancel = () => {
|
|
56
|
-
isCancel = true;
|
|
57
|
-
};
|
|
58
|
-
const commandParams = {
|
|
59
|
-
scope: event.type === 'group_msg' ? 'group' : 'private',
|
|
60
|
-
access: event.userId === event.api.adapter.config.master ? 2 /* CommandAccess.ADMIN */ : 0 /* CommandAccess.MEMBER */,
|
|
61
|
-
};
|
|
62
|
-
this.emit('before_command', { cancel, ...commonParams, ...commandParams });
|
|
63
|
-
if (isCancel)
|
|
64
|
-
return;
|
|
65
|
-
try {
|
|
66
|
-
command_1.default.parse(commonParams.command);
|
|
67
|
-
}
|
|
68
|
-
catch (err) {
|
|
69
|
-
if (!(err instanceof errror_1.CommandError) || !(err.extra instanceof commandExtra_1.default))
|
|
70
|
-
throw err;
|
|
71
|
-
const parseResult = err.extra.value;
|
|
72
|
-
const isSuccessParsed = parseResult.type === 'parsed';
|
|
73
|
-
this.emit('parse', { result: parseResult, ...commonParams, cancel });
|
|
74
|
-
if (isCancel)
|
|
75
|
-
return;
|
|
76
|
-
if (!isSuccessParsed)
|
|
77
|
-
throw err;
|
|
78
|
-
try {
|
|
79
|
-
const executedResult = await parseResult.action({ args: parseResult.args, options: parseResult.options }, event);
|
|
80
|
-
if (tsukiko_1.default.Object({}).index(tsukiko_1.default.Unknown()).check(executedResult)) {
|
|
81
|
-
this.emit('command', { result: executedResult, ...commonParams, ...commandParams });
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
const objectTemp = (obj) => {
|
|
85
|
-
const result = obj;
|
|
86
|
-
Object.keys(result).forEach(key => {
|
|
87
|
-
if (!result[key] || typeof result[key] !== 'string')
|
|
88
|
-
return;
|
|
89
|
-
result[key] = event.locale(result[key]);
|
|
90
|
-
});
|
|
91
|
-
return result;
|
|
92
|
-
};
|
|
93
|
-
const returnHandle = Array.isArray(executedResult)
|
|
94
|
-
? (0, tools_1.stringTemp)(event.locale(executedResult[0]), objectTemp(executedResult[1]))
|
|
95
|
-
: event.locale(executedResult ?? '');
|
|
96
|
-
this.emit('command', {
|
|
97
|
-
result: { type: 'success', return: returnHandle ?? undefined },
|
|
98
|
-
...commonParams,
|
|
99
|
-
...commandParams,
|
|
100
|
-
});
|
|
101
|
-
if (returnHandle)
|
|
102
|
-
event.send(returnHandle);
|
|
103
|
-
}
|
|
104
|
-
catch (executeErr) {
|
|
105
|
-
this.emit('command', {
|
|
106
|
-
result: { type: 'error', error: executeErr },
|
|
107
|
-
...commonParams,
|
|
108
|
-
...commandParams,
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
handleUnloadModuleEvent(session) {
|
|
114
|
-
if (!session.module)
|
|
115
|
-
return;
|
|
116
|
-
const superArr = [...this.midwareStack, ...this.commandStack, ...this.regexpStack];
|
|
117
|
-
Object.keys(superArr).forEach(indexTemp => {
|
|
118
|
-
if (!session.module)
|
|
119
|
-
return;
|
|
120
|
-
const index = parseInt(indexTemp, 10);
|
|
121
|
-
const element = superArr[index];
|
|
122
|
-
if (element.extend === session.module.mainPath)
|
|
123
|
-
delete superArr[index];
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
registeMessageEvent() {
|
|
127
|
-
this.on('midwares', session => this.handleMidwaresEvent(session));
|
|
128
|
-
this.on('group_msg', session => this.handleMessageEvent(session));
|
|
129
|
-
this.on('private_msg', session => this.handleMessageEvent(session));
|
|
130
|
-
this.before('send', session => {
|
|
131
|
-
const { api } = session;
|
|
132
|
-
api.adapter.status.sentMsg += 1;
|
|
133
|
-
});
|
|
134
|
-
this.on('unload_module', session => this.handleUnloadModuleEvent(session));
|
|
135
|
-
}
|
|
136
|
-
midware(callback, priority = 100) {
|
|
137
|
-
if (this.midwareStack.filter(Element => Element.callback === callback).length)
|
|
138
|
-
return false;
|
|
139
|
-
this.midwareStack.push({ callback, priority, extend: this.getCurrent() });
|
|
140
|
-
this.midwareStack.sort((first, second) => first.priority - second.priority);
|
|
141
|
-
return true;
|
|
142
|
-
}
|
|
143
|
-
command(template, config) {
|
|
144
|
-
const result = new command_1.default(template, config);
|
|
145
|
-
this.commandStack.push({
|
|
146
|
-
extend: this.getCurrent(),
|
|
147
|
-
data: command_1.default.dataList[command_1.default.dataList.length - 1],
|
|
148
|
-
});
|
|
149
|
-
return result;
|
|
150
|
-
}
|
|
151
|
-
regexp(match, callback) {
|
|
152
|
-
if (this.regexpStack.filter(Element => Element.match === match).length)
|
|
153
|
-
return false;
|
|
154
|
-
this.regexpStack.push({ extend: this.getCurrent(), match, callback });
|
|
155
|
-
return true;
|
|
156
|
-
}
|
|
157
|
-
boardcast(type, message) {
|
|
158
|
-
const send = type === 'private'
|
|
159
|
-
? (api) => api.send_private_msg(message, 1)
|
|
160
|
-
: (api) => api.send_group_msg(message, 1);
|
|
161
|
-
/* this need support of database... */
|
|
162
|
-
Object.values(this.botStack).forEach(apis => {
|
|
163
|
-
/* feating... */
|
|
164
|
-
apis.forEach(api => send(api));
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
notify(message) {
|
|
168
|
-
const mainAdapterIdentity = Object.keys(this.config.adapter)[0];
|
|
169
|
-
Object.values(this.botStack).forEach(apis => apis.forEach(api => {
|
|
170
|
-
if (api.adapter.identity !== mainAdapterIdentity)
|
|
171
|
-
return;
|
|
172
|
-
api.send_private_msg(message, api.adapter.config.master);
|
|
173
|
-
}));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
exports.Message = Message;
|
|
177
|
-
exports.default = Message;
|
package/lib/base/modules.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import Events from './events';
|
|
2
|
-
import Context from '../context';
|
|
3
|
-
import { type ModuleData, type ModuleInstanceConstructor, type ModuleInstanceFunction } from '../types';
|
|
4
|
-
export declare class Modules extends Events {
|
|
5
|
-
private current;
|
|
6
|
-
private failedLoadCount;
|
|
7
|
-
protected getCurrent(): string;
|
|
8
|
-
private setCureent;
|
|
9
|
-
private runInstance;
|
|
10
|
-
private moduleAllHandle;
|
|
11
|
-
protected readonly moduleStack: ModuleData[];
|
|
12
|
-
use(summary: string | ModuleData | ModuleInstanceFunction | ModuleInstanceConstructor, ctx: Context, config: object): Promise<void>;
|
|
13
|
-
dispose(module: string | ModuleData): void;
|
|
14
|
-
}
|
|
15
|
-
export default Modules;
|