koishi-core
Advanced tools
Comparing version 2.0.0-alpha.3 to 2.0.0-alpha.4
@@ -7,3 +7,2 @@ /// <reference types="node" /> | ||
import { Meta } from './meta'; | ||
import { EventEmitter } from 'events'; | ||
export interface AppOptions { | ||
@@ -28,5 +27,5 @@ port?: number; | ||
export declare const appList: App[]; | ||
export declare const onStart: (callback: () => any) => EventEmitter; | ||
export declare const onStop: (callback: () => any) => EventEmitter; | ||
export declare const onApp: (callback: (app: App) => any) => EventEmitter; | ||
export declare const onStart: (callback: () => any) => import("events").EventEmitter; | ||
export declare const onStop: (callback: () => any) => import("events").EventEmitter; | ||
export declare const onApp: (callback: (app: App) => any) => import("events").EventEmitter; | ||
export declare function startAll(): Promise<void>; | ||
@@ -33,0 +32,0 @@ export declare function stopAll(): Promise<void>; |
@@ -14,11 +14,9 @@ "use strict"; | ||
const koishi_utils_1 = require("koishi-utils"); | ||
const messages_1 = require("./messages"); | ||
const events_1 = require("events"); | ||
const shared_1 = require("./shared"); | ||
const util_1 = require("util"); | ||
exports.appMap = {}; | ||
exports.appList = []; | ||
const emitter = new events_1.EventEmitter(); | ||
exports.onStart = (callback) => emitter.on('start', callback); | ||
exports.onStop = (callback) => emitter.on('stop', callback); | ||
exports.onApp = (callback) => emitter.on('app', callback); | ||
exports.onStart = (callback) => shared_1.emitter.on('start', callback); | ||
exports.onStop = (callback) => shared_1.emitter.on('stop', callback); | ||
exports.onApp = (callback) => shared_1.emitter.on('app', callback); | ||
async function startAll() { | ||
@@ -56,2 +54,3 @@ await Promise.all(exports.appList.map(async (app) => app.start())); | ||
retryInterval: 5000, | ||
quickOperationTimeout: 100, | ||
}; | ||
@@ -187,3 +186,3 @@ function defineProperty(object, key, value) { | ||
if (!this._middlewareSet.has(counter)) { | ||
throw new Error(messages_1.errors.ISOLATED_NEXT); | ||
throw new Error(shared_1.errors.ISOLATED_NEXT); | ||
} | ||
@@ -233,3 +232,3 @@ if (fallback) | ||
this.middleware(this._preprocess); | ||
emitter.emit('app', this); | ||
shared_1.emitter.emit('app', this); | ||
} | ||
@@ -343,3 +342,3 @@ get users() { | ||
if (exports.appList.every(app => app.status === Status.open)) { | ||
emitter.emit('start'); | ||
shared_1.emitter.emit('start'); | ||
} | ||
@@ -365,3 +364,3 @@ } | ||
if (exports.appList.every(app => app.status === Status.closed)) { | ||
emitter.emit('stop'); | ||
shared_1.emitter.emit('stop'); | ||
} | ||
@@ -368,0 +367,0 @@ } |
@@ -44,3 +44,3 @@ /// <reference types="node" /> | ||
} | ||
export declare type UserType<T> = T | ((user: UserData) => T); | ||
export declare type UserType<T> = T extends (...args: any) => any ? never : T | ((user: UserData) => T); | ||
export interface CommandConfig { | ||
@@ -51,2 +51,3 @@ /** description */ | ||
authority?: number; | ||
/** whether to disable */ | ||
disable?: UserType<boolean>; | ||
@@ -53,0 +54,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const messages_1 = require("./messages"); | ||
const shared_1 = require("./shared"); | ||
const koishi_utils_1 = require("koishi-utils"); | ||
@@ -87,3 +87,3 @@ const util_1 = require("util"); | ||
if (!name) | ||
throw new Error(messages_1.errors.EXPECT_COMMAND_NAME); | ||
throw new Error(shared_1.errors.EXPECT_COMMAND_NAME); | ||
this._argsDef = parseArguments(declaration); | ||
@@ -124,3 +124,3 @@ this.config = { ...Command.defaultConfig, ...config }; | ||
else if (previous !== this) { | ||
throw new Error(messages_1.errors.DUPLICATE_COMMAND); | ||
throw new Error(shared_1.errors.DUPLICATE_COMMAND); | ||
} | ||
@@ -203,3 +203,3 @@ } | ||
if (name in this._optsDef) { | ||
throw new Error(messages_1.errors.DUPLICATE_OPTION); | ||
throw new Error(shared_1.errors.DUPLICATE_OPTION); | ||
} | ||
@@ -206,0 +206,0 @@ this._optsDef[name] = option; |
@@ -6,3 +6,3 @@ "use strict"; | ||
const meta_1 = require("./meta"); | ||
const messages_1 = require("./messages"); | ||
const shared_1 = require("./shared"); | ||
const util_1 = require("util"); | ||
@@ -25,3 +25,3 @@ var ContextScope; | ||
if (!capture) | ||
throw new Error(messages_1.errors.INVALID_IDENTIFIER); | ||
throw new Error(shared_1.errors.INVALID_IDENTIFIER); | ||
const [_, type, sign = '-', list] = capture; | ||
@@ -110,3 +110,3 @@ const idList = list ? list.split(',').map(n => +n) : []; | ||
else { | ||
throw new Error(messages_1.errors.INVALID_PLUGIN); | ||
throw new Error(shared_1.errors.INVALID_PLUGIN); | ||
} | ||
@@ -222,7 +222,7 @@ return this; | ||
if (command === parent) { | ||
throw new Error(messages_1.errors.INVALID_SUBCOMMAND); | ||
throw new Error(shared_1.errors.INVALID_SUBCOMMAND); | ||
} | ||
if (command.parent) { | ||
if (command.parent !== parent) { | ||
throw new Error(messages_1.errors.INVALID_SUBCOMMAND); | ||
throw new Error(shared_1.errors.INVALID_SUBCOMMAND); | ||
} | ||
@@ -235,3 +235,3 @@ } | ||
else { | ||
throw new Error(messages_1.errors.INVALID_CONTEXT); | ||
throw new Error(shared_1.errors.INVALID_CONTEXT); | ||
} | ||
@@ -243,3 +243,3 @@ } | ||
if (context.identifier === noopIdentifier) { | ||
throw new Error(messages_1.errors.INVALID_CONTEXT); | ||
throw new Error(shared_1.errors.INVALID_CONTEXT); | ||
} | ||
@@ -269,3 +269,3 @@ command = new command_1.Command(name, declaration, context); | ||
if (!command || !command.context.match(meta) || command.getConfig('disable', meta)) { | ||
return meta.$send(messages_1.messages.COMMAND_NOT_FOUND); | ||
return meta.$send(shared_1.messages.COMMAND_NOT_FOUND); | ||
} | ||
@@ -272,0 +272,0 @@ const unknown = Object.keys(options).filter(key => !command._optsDef[key]); |
@@ -50,3 +50,2 @@ import { Observed } from 'koishi-utils'; | ||
observeUser<K extends UserField>(user: number | UserData, defaultAuthority?: number, fields?: readonly K[]): Promise<User<K>>; | ||
getUserCount(): Promise<number>; | ||
} | ||
@@ -61,3 +60,2 @@ export interface GroupMethods { | ||
observeGroup<K extends GroupField>(group: number | GroupData, selfId?: number, fields?: readonly K[]): Promise<Group<K>>; | ||
getGroupCount(): Promise<number>; | ||
} | ||
@@ -64,0 +62,0 @@ export interface TableMethods { |
@@ -7,3 +7,3 @@ "use strict"; | ||
const __1 = require(".."); | ||
const messages_1 = require("../messages"); | ||
const shared_1 = require("../shared"); | ||
const util_1 = require("util"); | ||
@@ -24,4 +24,4 @@ const leven_1 = __importDefault(require("leven")); | ||
items, | ||
prefix: messages_1.messages.COMMAND_SUGGESTION_PREFIX, | ||
suffix: messages_1.messages.COMMAND_SUGGESTION_SUFFIX, | ||
prefix: shared_1.messages.COMMAND_SUGGESTION_PREFIX, | ||
suffix: shared_1.messages.COMMAND_SUGGESTION_SUFFIX, | ||
coefficient: app.options.similarityCoefficient, | ||
@@ -44,3 +44,3 @@ command: suggestion => app._commandMap[suggestion], | ||
return next(() => { | ||
const message = prefix + util_1.format(messages_1.messages.SUGGESTION_TEXT, suggestions.map(name => `“${name}”`).join('或')); | ||
const message = prefix + util_1.format(shared_1.messages.SUGGESTION_TEXT, suggestions.map(name => `“${name}”`).join('或')); | ||
if (suggestions.length > 1) | ||
@@ -47,0 +47,0 @@ return meta.$send(message); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const __1 = require(".."); | ||
const messages_1 = require("../messages"); | ||
const shared_1 = require("../shared"); | ||
const util_1 = require("util"); | ||
@@ -23,4 +23,3 @@ const koishi_utils_1 = require("koishi-utils"); | ||
let shouldFetchAuthority = !fields.has('authority') && authority > 0; | ||
let shouldFetchUsage = !fields.has('usage') && (typeof maxUsage === 'number' && maxUsage < Infinity || | ||
typeof minInterval === 'number' && minInterval > 0); | ||
let shouldFetchUsage = !(fields.has('usage') || !maxUsage && !minInterval); | ||
for (const option of command._options) { | ||
@@ -52,7 +51,7 @@ if (option.camels[0] in options) { | ||
if (nextArg === null || nextArg === void 0 ? void 0 : nextArg.required) { | ||
return sendHint(meta, messages_1.messages.INSUFFICIENT_ARGUMENTS); | ||
return sendHint(meta, shared_1.messages.INSUFFICIENT_ARGUMENTS); | ||
} | ||
const finalArg = command._argsDef[command._argsDef.length - 1]; | ||
if (args.length > command._argsDef.length && !finalArg.noSegment && !finalArg.variadic) { | ||
return sendHint(meta, messages_1.messages.REDUNANT_ARGUMENTS); | ||
return sendHint(meta, shared_1.messages.REDUNANT_ARGUMENTS); | ||
} | ||
@@ -62,3 +61,3 @@ } | ||
if (command.config.checkUnknown && unknown.length) { | ||
return sendHint(meta, messages_1.messages.UNKNOWN_OPTIONS, unknown.join(', ')); | ||
return sendHint(meta, shared_1.messages.UNKNOWN_OPTIONS, unknown.join(', ')); | ||
} | ||
@@ -71,3 +70,3 @@ // check required options | ||
if (absent) { | ||
return sendHint(meta, messages_1.messages.REQUIRED_OPTIONS, absent.rawName); | ||
return sendHint(meta, shared_1.messages.REQUIRED_OPTIONS, absent.rawName); | ||
} | ||
@@ -80,3 +79,3 @@ } | ||
if (command.config.authority > meta.$user.authority) { | ||
return sendHint(meta, messages_1.messages.LOW_AUTHORITY); | ||
return sendHint(meta, shared_1.messages.LOW_AUTHORITY); | ||
} | ||
@@ -86,3 +85,3 @@ for (const option of command._options) { | ||
if (option.authority > meta.$user.authority) { | ||
return sendHint(meta, messages_1.messages.LOW_AUTHORITY); | ||
return sendHint(meta, shared_1.messages.LOW_AUTHORITY); | ||
} | ||
@@ -129,3 +128,3 @@ if (option.notUsage) | ||
if (now - usage.last < minInterval) { | ||
return messages_1.messages.TOO_FREQUENT; | ||
return shared_1.messages.TOO_FREQUENT; | ||
} | ||
@@ -136,3 +135,3 @@ else if (options.minInterval || options.timestamp) { | ||
if (usage.count >= maxUsage) { | ||
return messages_1.messages.USAGE_EXHAUSTED; | ||
return shared_1.messages.USAGE_EXHAUSTED; | ||
} | ||
@@ -139,0 +138,0 @@ else if (options.maxUsage) { |
@@ -7,2 +7,4 @@ /// <reference types="node" /> | ||
import { CQResponse } from './sender'; | ||
export declare const onHttpServer: (callback: (server: http.Server) => any) => import("events").EventEmitter; | ||
export declare const onWsClient: (callback: (socket: WebSocket) => any) => import("events").EventEmitter; | ||
export declare abstract class Server { | ||
@@ -9,0 +11,0 @@ appList: App[]; |
@@ -16,6 +16,8 @@ "use strict"; | ||
const http = __importStar(require("http")); | ||
const messages_1 = require("./messages"); | ||
const shared_1 = require("./shared"); | ||
const crypto_1 = require("crypto"); | ||
const koishi_utils_1 = require("koishi-utils"); | ||
const util_1 = require("util"); | ||
exports.onHttpServer = (callback) => shared_1.emitter.on('http-server', callback); | ||
exports.onWsClient = (callback) => shared_1.emitter.on('ws-client', callback); | ||
class Server { | ||
@@ -190,3 +192,3 @@ constructor(app) { | ||
if (this.versionLessThan(3)) { | ||
throw new Error(messages_1.errors.UNSUPPORTED_CQHTTP_VERSION); | ||
throw new Error(shared_1.errors.UNSUPPORTED_CQHTTP_VERSION); | ||
} | ||
@@ -196,3 +198,3 @@ else if (this.versionLessThan(3, 4)) { | ||
if (apps.length > 1) | ||
throw new Error(messages_1.errors.MULTIPLE_ANONYMOUS_BOTS); | ||
throw new Error(shared_1.errors.MULTIPLE_ANONYMOUS_BOTS); | ||
const info = await apps[0].sender.getLoginInfo(); | ||
@@ -219,2 +221,4 @@ apps[0].prepare(info.userId); | ||
this.server = http.createServer((req, res) => { | ||
if (req.url !== '/') | ||
return; | ||
let body = ''; | ||
@@ -246,3 +250,5 @@ req.on('data', chunk => body += chunk); | ||
// handle quick operations | ||
res.statusCode = 200; | ||
res.writeHead(200, { | ||
'Content-Type': 'application/json', | ||
}); | ||
const app = this.appMap[meta.selfId]; | ||
@@ -268,2 +274,3 @@ if (app.options.quickOperationTimeout > 0) { | ||
}); | ||
shared_1.emitter.emit('http-server', this.server); | ||
} | ||
@@ -337,3 +344,2 @@ async _listen() { | ||
}); | ||
let resolved = false; | ||
this.socket.on('message', (data) => { | ||
@@ -350,7 +356,2 @@ var _a, _b; | ||
} | ||
if (!resolved) { | ||
resolved = true; | ||
this.debug('connect to ws server:', this.app.options.server); | ||
resolve(); | ||
} | ||
if ('post_type' in parsed) { | ||
@@ -361,6 +362,9 @@ const meta = this.prepareMeta(parsed); | ||
} | ||
else if (parsed.echo === -1) { | ||
this.version = koishi_utils_1.camelCase(parsed.data); | ||
this.debug('connect to ws server:', this.app.options.server); | ||
shared_1.emitter.emit('ws-client', this.socket); | ||
resolve(); | ||
} | ||
else { | ||
if (parsed.echo === -1) { | ||
this.version = koishi_utils_1.camelCase(parsed.data); | ||
} | ||
(_b = (_a = this._listeners)[parsed.echo]) === null || _b === void 0 ? void 0 : _b.call(_a, parsed); | ||
@@ -383,3 +387,3 @@ } | ||
if (typeof app.options.type !== 'string') { | ||
throw new Error(messages_1.errors.UNSUPPORTED_SERVER_TYPE); | ||
throw new Error(shared_1.errors.UNSUPPORTED_SERVER_TYPE); | ||
} | ||
@@ -397,3 +401,3 @@ app.options.type = app.options.type.toLowerCase(); | ||
else { | ||
throw new Error(messages_1.errors.UNSUPPORTED_SERVER_TYPE); | ||
throw new Error(shared_1.errors.UNSUPPORTED_SERVER_TYPE); | ||
} | ||
@@ -403,3 +407,3 @@ const servers = exports.serverMap[app.options.type]; | ||
if (!value) { | ||
throw new Error(util_1.format(messages_1.errors.MISSING_CONFIGURATION, key)); | ||
throw new Error(util_1.format(shared_1.errors.MISSING_CONFIGURATION, key)); | ||
} | ||
@@ -406,0 +410,0 @@ if (value in servers) { |
{ | ||
"name": "koishi-core", | ||
"description": "Core features for Koishi", | ||
"version": "2.0.0-alpha.3", | ||
"version": "2.0.0-alpha.4", | ||
"main": "dist/index.js", | ||
@@ -36,6 +36,7 @@ "typings": "dist/index.d.ts", | ||
"@types/debug": "^4.1.5", | ||
"@types/ms": "^2.0.0", | ||
"@types/ws": "^7.2.2", | ||
"get-port": "^5.1.1", | ||
"koishi-database-memory": "^2.0.0-alpha.3", | ||
"koishi-test-utils": "^4.0.0-alpha.3" | ||
"koishi-database-memory": "^2.0.0-alpha.4", | ||
"koishi-test-utils": "^4.0.0-alpha.4" | ||
}, | ||
@@ -42,0 +43,0 @@ "dependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
284081
62
3486
6