From 99dc132c3684e4b233bf69ab6b157246c26888f0 Mon Sep 17 00:00:00 2001 From: Shigma Date: Tue, 3 Oct 2023 01:50:19 +0800 Subject: [PATCH] feat(sandbox): support satori v3 api --- plugins/sandbox/src/bot.ts | 27 ++++++++++++------- plugins/sandbox/src/index.ts | 49 +++++++++++++++------------------- plugins/sandbox/src/message.ts | 4 +-- 3 files changed, 40 insertions(+), 40 deletions(-) diff --git a/plugins/sandbox/src/bot.ts b/plugins/sandbox/src/bot.ts index 24485a9c..f82e8038 100644 --- a/plugins/sandbox/src/bot.ts +++ b/plugins/sandbox/src/bot.ts @@ -1,15 +1,26 @@ import { Client } from '@koishijs/plugin-console' -import { Bot, Context, Fragment, SendOptions, Time } from 'koishi' +import { Bot, Context, Time, Universal } from 'koishi' import { SandboxMessenger } from './message' -export class SandboxBot extends Bot { - username = 'koishi' +export namespace SandboxBot { + export interface Config { + selfId: string + platform: string + } +} + +export class SandboxBot extends Bot { + static MessageEncoder = SandboxMessenger + hidden = true internal = {} clients = new Set() - constructor(ctx: Context, config: Bot.Config) { + constructor(ctx: Context, config: SandboxBot.Config) { super(ctx, config) + this.selfId = config.selfId + this.platform = config.platform + this.user.name = 'koishi' } async request(method: string, data = {}) { @@ -34,12 +45,8 @@ export class SandboxBot extends Bot { }) } - async sendMessage(channelId: string, fragment: Fragment, guildId?: string, options?: SendOptions) { - return new SandboxMessenger(this, channelId, guildId, options).send(fragment) - } - - async sendPrivateMessage(userId: string, fragment: Fragment, options?: SendOptions) { - return new SandboxMessenger(this, '@' + userId, undefined, options).send(fragment) + async createDirectChannel(userId: string): Promise { + return { id: '@' + userId, type: Universal.Channel.Type.DIRECT } } async deleteMessage(channelId: string, messageId: string) { diff --git a/plugins/sandbox/src/index.ts b/plugins/sandbox/src/index.ts index 45f26aca..c27464c7 100644 --- a/plugins/sandbox/src/index.ts +++ b/plugins/sandbox/src/index.ts @@ -1,4 +1,4 @@ -import { $, Context, Dict, Random, Schema, User } from 'koishi' +import { $, Context, Dict, Random, Schema, Universal, User } from 'koishi' import { DataService } from '@koishijs/plugin-console' import { resolve } from 'path' import { SandboxBot } from './bot' @@ -77,18 +77,15 @@ export function apply(ctx: Context, config: Config) { const bots: Dict = {} - const templateSession = (userId: string, channelId: string) => ({ - userId, - channelId, - guildId: channelId === '@' + userId ? undefined : channelId, - subtype: channelId === '@' + userId ? 'private' : 'group', - isDirect: channelId === '@' + userId, - timestamp: Date.now(), - author: { - userId, - username: userId, - }, - }) + const createEvent = (userId: string, channelId: string) => { + const isDirect = channelId === '@' + userId + return { + user: { id: userId, name: userId }, + channel: { id: channelId, type: isDirect ? Universal.Channel.Type.DIRECT : Universal.Channel.Type.TEXT }, + guild: isDirect ? undefined : { id: channelId }, + timestamp: Date.now(), + } + } ctx.console.addListener('sandbox/send-message', async function (platform, userId, channel, content, quote) { const bot = bots[platform] ||= new SandboxBot(ctx, { @@ -101,16 +98,13 @@ export function apply(ctx: Context, config: Config) { type: 'sandbox/message', body: { id, content, user: userId, channel, platform, quote }, }) - const session = bot.session({ - ...templateSession(userId, channel), - messageId: id, - type: 'message', - quote: quote && { - ...templateSession(quote.user, quote.channel), - content: quote.content, - messageId: quote.id, - }, - }) + const session = bot.session(createEvent(userId, channel)) + session.type = 'message' + session.messageId = id + session.quote = quote && { + content: quote.content, + id: quote.id, + } session.content = content bot.dispatch(session) }, { authority: 4 }) @@ -121,11 +115,10 @@ export function apply(ctx: Context, config: Config) { selfId: 'koishi', }) bot.clients.add(this) - bot.dispatch(bot.session({ - ...templateSession(userId, channel), - messageId, - type: 'message-deleted', - })) + const session = bot.session(createEvent(userId, channel)) + session.type = 'message-deleted' + session.messageId = messageId + bot.dispatch(session) }, { authority: 4 }) ctx.console.addListener('sandbox/get-user', async function (platform, pid) { diff --git a/plugins/sandbox/src/message.ts b/plugins/sandbox/src/message.ts index 77dc5b5b..7d062e73 100644 --- a/plugins/sandbox/src/message.ts +++ b/plugins/sandbox/src/message.ts @@ -21,7 +21,7 @@ export class SandboxMessenger extends Messenger { async flush() { if (!this.buffer.trim()) return const content = await h.transformAsync(this.buffer.trim(), this.rules) - const session = this.bot.session(this.session) + const session = this.bot.session(this.session.event) session.messageId = Random.id() for (const client of this.bot.clients) { client.send({ @@ -35,7 +35,7 @@ export class SandboxMessenger extends Messenger { }, }) } - this.results.push(session) + this.results.push(session.event.message) this.buffer = '' }