Skip to content

Commit

Permalink
fix: release
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-cn committed Jul 3, 2024
1 parent 6fbc50a commit d3981b6
Show file tree
Hide file tree
Showing 45 changed files with 825 additions and 516 deletions.
15 changes: 9 additions & 6 deletions core/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class Adapter<I extends object = object, M = {}> extends EventEmitter {
constructor(public name: string) {
super();
}
botConfig(bot: Adapter.Bot<I>) {
return this.app!.config.bots.find(config => config.unique_id === bot.unique_id);
}
async sendMsg(
bot_id: string,
target_id: string,
Expand Down Expand Up @@ -70,14 +73,14 @@ export class Adapter<I extends object = object, M = {}> extends EventEmitter {
this.elements.push(element);
return this;
}
mount(app: App, bots: App.BotConfig[]) {
mount(app: App) {
this.emit('before-mount');
this.logger.level = app.config.log_level;
this.app = app;
this.emit('mounted', bots);
this.emit('mounted', app);
}
unmount() {
this.emit('before-unmount');
this.emit('before-unmount', this.app!);
this.app = null;
this.emit('unmounted');
}
Expand Down Expand Up @@ -138,10 +141,10 @@ export namespace Adapter {
export interface EventMap {
'bot-ready'(bot: Bot<any>): void;
'before-mount'(): void;
'before-unmount'(): void;
'mounted'(): void;
'before-unmount'(app: App): void;
'mounted'(app: App): void;
'unmounted'(): void;
'start'(): void;
'start'(configs: App.BotConfig[]): void;
}
export interface Config<T extends keyof App.Adapters = keyof App.Adapters> {
name: T;
Expand Down
13 changes: 6 additions & 7 deletions core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { EventEmitter } from 'events';
import { Logger, getLogger } from 'log4js';
import { Middleware } from './middleware';
import { Plugin, PluginMap } from './plugin';
import { Bot, Dict, LogLevel } from './types';
import { Bot, LogLevel } from './types';
import { loadModule, remove } from './utils';
import { APP_KEY, REQUIRED_KEY, WORK_DIR } from './constans';
import path from 'path';
import { Adapter, AdapterBot, AdapterReceive } from './adapter';
import { Message } from './message';
import process from 'process';
import { Config } from './config';
import { Level } from 'level';
import { LevelDb } from './levelDb';

export function defineConfig(config: Partial<App.Config>): Partial<App.Config>;
Expand Down Expand Up @@ -294,7 +293,10 @@ export class App extends EventEmitter {
this.initPlugins();
this.initAdapter();
for (const [name, adapter] of this.adapters) {
adapter.emit('start');
const bots = this.config.bots.filter(
bot => bot?.adapter === adapter.name && !this.config.disable_bots.includes(bot.unique_id),
);
adapter.emit('start', bots);
this.logger.mark(`adapter: ${name} started`);
}
this.emit('start');
Expand All @@ -314,10 +316,7 @@ export class App extends EventEmitter {
if (!(adapter instanceof Adapter)) throw new Error(`${loadPath} is not a valid adapter`);
this.adapters.set(adapter.name, adapter);
loadName = adapter.name;
const bots = this.config.bots.filter(
bot => bot?.adapter === adapter.name && !this.config.disable_bots.includes(bot.unique_id),
);
adapter.mount(this, bots);
adapter.mount(this);
this.logger.debug(`adapter: ${adapter.name} loaded`);
loaded = true;
} catch (e) {
Expand Down
23 changes: 6 additions & 17 deletions core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,19 @@ import { WORK_DIR } from './constans';
import { App } from './app';
export class Config {
public static exts: string[] = ['.json', '.yaml', '.yml'];
#filename: string = '';
filename: string = '';
#type: Config.Type = Config.Type.YAML;
#saving: boolean = false;
#data: App.Config;
get #dir() {
return path.dirname(this.#filename);
}
constructor(name: string, defaultValue?: App.Config) {
try {
this.#filename = this.#resolveByName(name);
this.filename = this.#resolveByName(name);
} catch (e) {
if (!defaultValue) throw e;
const ext = path.extname(name);
if (!Config.exts.includes(ext)) this.#filename = path.join(WORK_DIR, `${name}${this.#resolveExt()}`);
if (!Config.exts.includes(ext)) this.filename = path.join(WORK_DIR, `${name}${this.#resolveExt()}`);
this.#saveConfig(defaultValue);
}
this.#data = this.#loadConfig();
const watcher = fs.watch(this.#filename, 'buffer', () => {
if (process.env.init !== '1') {
console.log(`config changed, restarting...`);
watcher.close();
process.exit(51);
}
});
return new Proxy<App.Config>(this.#data, {
get: (target, p, receiver) => {
if (Reflect.has(this, p)) return Reflect.get(this, p, receiver);
Expand Down Expand Up @@ -76,7 +65,7 @@ export class Config {
}
}
#loadConfig() {
const content = fs.readFileSync(this.#filename, 'utf8');
const content = fs.readFileSync(this.filename, 'utf8');
switch (this.#type) {
case Config.Type.JSON:
return JSON.parse(content);
Expand All @@ -89,9 +78,9 @@ export class Config {
#saveConfig(data: App.Config = this.#data) {
switch (this.#type) {
case Config.Type.JSON:
return fs.writeFileSync(this.#filename, JSON.stringify(data, null, 2));
return fs.writeFileSync(this.filename, JSON.stringify(data, null, 2));
case Config.Type.YAML:
return fs.writeFileSync(this.#filename, yaml.stringify(data));
return fs.writeFileSync(this.filename, yaml.stringify(data));
default:
throw new Error(`不支持的配置文件类型${this.#type}`);
}
Expand Down
8 changes: 6 additions & 2 deletions core/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ export namespace Message {
(type: string, data: Dict): string;
text(text?: string): string;
face(id: number): string;
image(url: string): string;
image(base64: string, type?: string): string;
video(file: string, type?: string): string;
audio(file: string, type?: string): string;
at(user_id: string | number): string;
};
export type Type = 'private' | 'group' | 'guild' | 'direct';
Expand Down Expand Up @@ -124,7 +126,9 @@ export const segment: Message.DefineSegment = function (type, data) {
} as Message.DefineSegment;
segment.text = text => (text ? encodeURIComponent(text) : '');
segment.face = (id: number) => `<face id='${encodeURIComponent(id)}'/>`;
segment.image = (file: string) => `<image file='${encodeURIComponent(file)}'/>`;
segment.image = (file: string, type = 'png') => `<image file='${encodeURIComponent(file)}' type='${type}'/>`;
segment.video = (file: string, type = 'mp4') => `<video file='${encodeURIComponent(file)}' type='${type}'>`;
segment.audio = (file: string, type = 'mp3') => `<audio file='${encodeURIComponent(file)}' type='${type}'>`;
segment.at = user_id => `<at user_id='${encodeURIComponent(user_id)}'/>`;
type MessageSender = {
user_id?: string | number;
Expand Down
4 changes: 2 additions & 2 deletions packages/adapters/com-wechat/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@zhinjs/adapter-onebot-12",
"name": "@zhinjs/adapter-com-wechat",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"version": "0.0.1",
Expand All @@ -12,7 +12,7 @@
"author": "凉菜",
"repository": {
"url": "https://github.com/zhinjs/zhin",
"directory": "packages/adapters/onebot-v12"
"directory": "packages/adapters/com-wechat"
},
"dependencies": {
"zhin": "workspace:^",
Expand Down
Loading

0 comments on commit d3981b6

Please sign in to comment.