Skip to content

Commit

Permalink
feat(sandbox): support satori v3 api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 2, 2023
1 parent 0fbf9e8 commit 99dc132
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
27 changes: 17 additions & 10 deletions plugins/sandbox/src/bot.ts
Original file line number Diff line number Diff line change
@@ -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<SandboxBot.Config> {
static MessageEncoder = SandboxMessenger

hidden = true
internal = {}
clients = new Set<Client>()

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<T = any>(method: string, data = {}) {
Expand All @@ -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<Universal.Channel> {
return { id: '@' + userId, type: Universal.Channel.Type.DIRECT }
}

async deleteMessage(channelId: string, messageId: string) {
Expand Down
49 changes: 21 additions & 28 deletions plugins/sandbox/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -77,18 +77,15 @@ export function apply(ctx: Context, config: Config) {

const bots: Dict<SandboxBot> = {}

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, {
Expand All @@ -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 })
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/sandbox/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class SandboxMessenger extends Messenger<SandboxBot> {
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({
Expand All @@ -35,7 +35,7 @@ export class SandboxMessenger extends Messenger<SandboxBot> {
},
})
}
this.results.push(session)
this.results.push(session.event.message)
this.buffer = ''
}

Expand Down

0 comments on commit 99dc132

Please sign in to comment.