import { Adapter, Bot, Channel, Context, Fragment, Universal, User } from 'koishi' import { MessageClient, MockMessenger } from './client' import { Webhook } from './webhook' declare module 'koishi' { interface Context { mock: MockAdapter } interface User { mock: string } } export const DEFAULT_SELF_ID = '514' export namespace MockBot { export interface Config { selfId: string } } export class MockBot extends Bot { constructor(ctx: C, config: MockBot.Config) { super(ctx, config) this.platform = 'mock' this.selfId = config.selfId ?? DEFAULT_SELF_ID this.status = Universal.Status.ONLINE ctx.plugin(MockAdapter, this) } client(userId: string, channelId?: string) { return new MessageClient(this, userId, channelId) } receive(event: Partial, client?: MessageClient) { const session = this.session(event) session['client'] = client this.dispatch(session) return session.id } async getMessage(channelId: string, id: string) { const isDirect = channelId.startsWith('private:') return { id, messageId: id, channel: { id: channelId, type: isDirect ? Universal.Channel.Type.DIRECT : Universal.Channel.Type.TEXT }, content: '', time: 0, user: { id: this.selfId }, } } async sendMessage(channelId: string, fragment: Fragment, guildId?: string, options?: Universal.SendOptions) { const messages = await new MockMessenger(options!.session['client'], options).send(fragment) return messages.map(messages => messages.id) } } export class MockAdapter extends Adapter> { public webhook: Webhook constructor(ctx: C, bot: MockBot) { super(ctx) this.webhook = new Webhook(ctx.root) ctx.provide('mock', this, true) } async initUser(id: string, authority = 1, data?: Partial) { await this.ctx.root.database.createUser('mock', id, { authority, ...data }) } async initChannel(id: string, assignee = this.bots[0].selfId, data?: Partial) { await this.ctx.root.database.createChannel('mock', id, { assignee, ...data }) } client(userId: string, channelId?: string) { return this.bots[0].client(userId, channelId) } receive(event: Partial, client?: MessageClient) { return this.bots[0].receive(event, client) } session(event: Partial) { return this.bots[0].session(event) } } export namespace MockAdapter { export interface Config {} }