diff --git a/core/src/adapter.ts b/core/src/adapter.ts index 33679c9a9..7d2cd1e3c 100644 --- a/core/src/adapter.ts +++ b/core/src/adapter.ts @@ -14,24 +14,13 @@ export type Element = { export class Adapter

extends EventEmitter { elements: Element[] = []; private [adapterKey] = true; + bots: Adapter.Bot

[] = []; #is_started: boolean = false; app: App | null = null; #configs: Adapter.BotConfig

[] = []; static isAdapter(obj: any): obj is Adapter { return typeof obj === 'object' && !!obj[adapterKey]; } - get bots(): Adapter.Bot

[] { - return (this.app?.bots.filter(bot => bot.adapter === this) || []) as unknown as Adapter.Bot

[]; - } - set bots(bots: Adapter.Bot

[]) { - for (const bot of bots) { - if (bot.adapter !== this) throw new Error(`bot ${bot.unique_id} not belongs to adapter ${this.name}`); - const hasBot = (bot: Adapter.Bot

) => { - return this.app?.bots.some(b => b.unique_id === bot.unique_id); - }; - if (!hasBot(bot)) this.app?.bots.push(bot as any); - } - } start(app: App, config: Adapter.BotConfig

[]) { this.app = app; this.emit('start', config); diff --git a/core/src/app.ts b/core/src/app.ts index fa15143bd..6770d0c48 100644 --- a/core/src/app.ts +++ b/core/src/app.ts @@ -29,10 +29,15 @@ export class App extends EventEmitter { middlewares: Middleware[] = []; plugins: PluginMap = new PluginMap(); renders: Message.Render[] = []; - bots: Adapter.Bot[] = []; get adapters() { return App.adapters; } + get bots() { + return Array.from(App.adapters.values()).reduce((result, adapter) => { + result.push(...adapter.bots); + return result; + }, [] as Adapter.Bot[]); + } constructor() { super(); this.handleMessage = this.handleMessage.bind(this); diff --git a/core/src/message.ts b/core/src/message.ts index ef1b9f69b..a50c4f614 100644 --- a/core/src/message.ts +++ b/core/src/message.ts @@ -2,6 +2,8 @@ import { Dict, escape, unescape } from '@zhinjs/shared'; import { Prompt } from './prompt'; import { Adapter } from './adapter'; import { Adapters, App } from './app'; +import path from 'path'; +import * as fs from 'node:fs'; export interface MessageBase { message_id?: string; channel: Message.Channel; @@ -65,7 +67,7 @@ export class Message

{ export function parseFromTemplate(template: string | MessageElem): MessageElem[] { if (typeof template !== 'string') return [template]; const result: MessageElem[] = []; - const closingReg = /^<(\S+)(\s[^>]+)?\/>/; + const closingReg = /^<(\S+)(\s[^>]+)?>/; const twinningReg = /^<(\S+)(\s[^>]+)?>([\s\S]*?)<\/\1>/; while (template.length) { const [_, type, attrStr = '', child = ''] = template.match(twinningReg) || template.match(closingReg) || []; @@ -123,9 +125,18 @@ export namespace Message { (type: string, data: Dict): string; text(text?: string): string; face(id: number): string; + image(path: string, type?: string): string; + image(url: string, type?: string): string; + image(data: Uint8Array, type?: string): string; image(base64: string, type?: string): string; - video(file: string, type?: string): string; - audio(file: string, type?: string): string; + video(path: string, type?: string): string; + video(url: string, type?: string): string; + video(data: Uint8Array, type?: string): string; + video(base64: string, type?: string): string; + audio(path: string, type?: string): string; + audio(url: string, type?: string): string; + audio(data: Uint8Array, type?: string): string; + audio(base64: string, type?: string): string; at(user_id: string | number): string; }; export type Type = 'private' | 'group' | 'guild' | 'direct'; @@ -149,9 +160,23 @@ export const segment: Message.DefineSegment = function (type, data) { }) .join(' ')}/>`; } as Message.DefineSegment; +function formatSourceDataTostring(data: T): string { + const result: string = typeof data === 'string' ? data : `base64://${Buffer.from(data).toString('base64')}`; + if (fs.existsSync(result)) return `base64://${fs.readFileSync(result).toString('base64')}`; + return result; +} segment.text = text => escape(text || ''); segment.face = (id: number) => ``; -segment.image = (file: string, type = 'png') => ``; -segment.video = (file: string, type = 'mp4') => `