ai/src/module.ts

195 lines
5.1 KiB
TypeScript
Raw Normal View History

2024-01-21 04:27:02 +00:00
import { bindThis } from '@/decorators.js';
2024-03-28 15:43:17 +00:00
import , { HandlerResult, InstallerResult, ModuleDataDoc } from '@/ai.js';
import Message from '@/message.js';
2019-01-14 15:14:22 +00:00
export default abstract class Module {
2019-01-15 01:26:48 +00:00
public abstract readonly name: string;
2019-01-14 15:14:22 +00:00
2024-03-25 14:45:20 +00:00
private maybeAi?: ;
2024-03-28 15:43:17 +00:00
/**
* @deprecated
*/
public installed?: InstalledModule;
2019-01-14 15:14:22 +00:00
2019-01-15 17:10:42 +00:00
public init(ai: ) {
2024-03-25 14:45:20 +00:00
this.maybeAi = ai;
2019-01-14 15:14:22 +00:00
}
2024-03-28 15:43:17 +00:00
public abstract install(ai: ): InstallerResult;
2019-01-14 15:14:22 +00:00
2024-03-28 15:43:17 +00:00
/**
* @deprecated {@link Module#install} 使
*/
2024-03-25 14:45:20 +00:00
protected get ai(): {
if (this.maybeAi == null) {
throw new TypeError('This module has not been initialized');
}
return this.maybeAi;
}
2024-03-28 15:43:17 +00:00
/**
* @deprecated {@link InstalledModule#log} 使
*/
2024-01-21 04:27:02 +00:00
@bindThis
2019-01-14 15:14:22 +00:00
protected log(msg: string) {
2019-01-24 08:47:13 +00:00
this.ai.log(`[${this.name}]: ${msg}`);
2019-01-14 15:14:22 +00:00
}
2019-02-01 16:53:06 +00:00
/**
*
* @param key
* @param id ID稿ID
* @param data
2024-03-28 15:43:17 +00:00
* @deprecated {@link InstalledModule#subscribeReply} 使
2019-02-01 16:53:06 +00:00
*/
2024-01-21 04:27:02 +00:00
@bindThis
protected subscribeReply(key: string | null, id: string, data?: any) {
this.ai.subscribeReply(this, key, id, data);
2019-01-14 15:14:22 +00:00
}
2019-02-01 16:53:06 +00:00
/**
*
* @param key
2024-03-28 15:43:17 +00:00
* @deprecated {@link InstalledModule#unsubscribeReply} 使
2019-02-01 16:53:06 +00:00
*/
2024-01-21 04:27:02 +00:00
@bindThis
2020-09-02 12:54:01 +00:00
protected unsubscribeReply(key: string | null) {
2019-01-14 15:14:22 +00:00
this.ai.unsubscribeReply(this, key);
}
2019-02-01 17:06:51 +00:00
/**
*
*
* @param delay
* @param data
2024-03-28 15:43:17 +00:00
* @deprecated {@link InstalledModule#setTimeoutWithPersistence} 使
2019-02-01 17:06:51 +00:00
*/
2024-01-21 04:27:02 +00:00
@bindThis
2019-02-01 17:06:51 +00:00
public setTimeoutWithPersistence(delay: number, data?: any) {
this.ai.setTimeoutWithPersistence(this, delay, data);
}
2019-05-10 02:55:07 +00:00
2024-03-28 15:43:17 +00:00
/**
* @deprecated {@link InstalledModule#getData} 使
*/
2024-01-21 04:27:02 +00:00
@bindThis
2019-05-10 02:55:07 +00:00
protected getData() {
2024-03-28 15:43:17 +00:00
const doc = this.ai.moduleData.findOne({
module: this.name
});
return doc?.data;
2019-05-10 02:55:07 +00:00
}
2024-03-28 15:43:17 +00:00
/**
* @deprecated {@link InstalledModule#setData} 使
*/
2024-01-21 04:27:02 +00:00
@bindThis
2019-05-10 02:55:07 +00:00
protected setData(data: any) {
2024-03-28 15:43:17 +00:00
const doc = this.ai.moduleData.findOne({
module: this.name
});
if (doc == null) {
return;
}
doc.data = data;
this.ai.moduleData.update(doc);
if (this.installed != null) {
this.installed.updateDoc();
}
}
}
export abstract class InstalledModule<M extends Module = Module, Data = any> implements InstallerResult {
protected readonly module: M;
protected readonly ai: ;
private doc: ModuleDataDoc<Data>;
constructor(module: M, ai: , initialData: any = {}) {
this.module = module;
this.ai = ai;
const doc = this.ai.moduleData.findOne({
module: module.name
});
if (doc == null) {
this.doc = this.ai.moduleData.insertOne({
module: module.name,
data: initialData
}) as ModuleDataDoc<Data>;
} else {
this.doc = doc;
}
ai.installedModules[module.name] = this;
}
@bindThis
protected log(msg: string) {
this.ai.log(`[${this.module.name}]: ${msg}`);
}
/**
*
* @param key
* @param id ID稿ID
* @param data
*/
@bindThis
protected subscribeReply(key: string | null, id: string, data?: any) {
this.ai.subscribeReply(this.module, key, id, data);
}
/**
*
* @param key
*/
@bindThis
protected unsubscribeReply(key: string | null) {
this.ai.unsubscribeReply(this.module, key);
}
/**
*
*
* @param delay
* @param data
*/
@bindThis
public setTimeoutWithPersistence(delay: number, data?: any) {
this.ai.setTimeoutWithPersistence(this.module, delay, data);
}
@bindThis
protected getData(): Data {
return this.doc.data;
}
@bindThis
protected setData(data: Data) {
2019-05-10 02:55:07 +00:00
this.doc.data = data;
this.ai.moduleData.update(this.doc);
}
2024-03-28 15:43:17 +00:00
/**
* @deprecated
*/
public updateDoc() {
const doc = this.ai.moduleData.findOne({
module: this.module.name
});
if (doc != null) {
this.doc = doc;
}
}
mentionHook?(msg: Message): Promise<boolean | HandlerResult>;
contextHook?(key: any, msg: Message, data?: any): Promise<void | boolean | HandlerResult>;
timeoutCallback?(data?: any): void;
2019-01-14 15:14:22 +00:00
}