Skip to content

Commit

Permalink
refa: upgrade sandbox to new adapter / bot api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 11, 2022
1 parent b66b204 commit f607708
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 48 deletions.
2 changes: 1 addition & 1 deletion plugins/a11y/commands/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Config: Schema<string | Config, Config> = Schema.union([
Schema.transform(String, (name) => ({ name, alias: [] })),
])

export const schema = Schema.dict(Config)
export const schema: Schema<Dict<string | Config>, Dict<Config>> = Schema.dict(Config)

export const name = 'commands'

Expand Down
78 changes: 31 additions & 47 deletions plugins/frontend/sandbox/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Adapter, Bot, Context, Dict, observe, Random, Schema, segment, Session, User } from 'koishi'
import { Bot, Context, Dict, observe, Random, Schema, segment, User } from 'koishi'
import { DataService } from '@koishijs/plugin-console'
import { resolve } from 'path'

Expand All @@ -25,47 +25,16 @@ declare module '@koishijs/plugin-console' {
}
}

export interface Config {}

export const Config: Schema<Config> = Schema.object({})
class SandboxBot extends Bot {
static using = ['console'] as const

export class SandboxBot extends Bot {
username = 'koishi'
selfId = 'koishi'
hidden = true

constructor(public adapter: Sandbox, config: Bot.BaseConfig) {
super(adapter, config)
}

async sendMessage(channel: string, content: string) {
content = segment.transform(content, {
image(data) {
if (!data.url.startsWith('base64://')) return segment('image', data)
return segment.image('data:image/png;base64,' + data.url.slice(9))
},
constructor(public ctx: Context, config: SandboxBot.Config) {
super(ctx, {
platform: 'sandbox',
selfId: 'koishi',
})
this.adapter.broadcast({ content, user: 'Koishi', channel })
return [Random.id()]
}
}

export interface Message {
user: string
channel: string
content: string
}

export default class Sandbox extends Adapter {
static using = ['console'] as const
static schema: Schema<Config> = Schema.object({})

constructor(ctx: Context, config: Config) {
super(ctx, config)

this.platform = 'sandbox'
ctx.bots.adapters.sandbox = this
const bot = ctx.bots.create('sandbox', {}, SandboxBot)

ctx.plugin(UserProvider)

Expand All @@ -75,14 +44,12 @@ export default class Sandbox extends Adapter {
})

ctx.console.addListener('sandbox/message', async (user, channel, content) => {
this.broadcast({ content, user, channel })
this.dispatch(new Session(bot, {
platform: 'sandbox',
ctx.console.ws.broadcast('sandbox', { content, user, channel })
this.dispatch(this.session({
userId: user,
content,
channelId: channel,
guildId: channel,
selfId: 'koishi',
guildId: channel === '@' + user ? undefined : channel,
type: 'message',
subtype: channel === '@' + user ? 'private' : 'group',
author: {
Expand All @@ -93,13 +60,30 @@ export default class Sandbox extends Adapter {
}, { authority: 4 })
}

broadcast(body: Message) {
this.ctx.console.ws.broadcast('sandbox', body)
async sendMessage(channel: string, content: string) {
content = segment.transform(content, {
image(data) {
if (!data.url.startsWith('base64://')) return segment('image', data)
return segment.image('data:image/png;base64,' + data.url.slice(9))
},
})
this.ctx.console.ws.broadcast('sandbox', { content, user: 'Koishi', channel })
return [Random.id()]
}
}

namespace SandboxBot {
export interface Config {}

export const Config: Schema<Config> = Schema.object({})
}

async start() {}
export default SandboxBot

async stop() {}
export interface Message {
user: string
channel: string
content: string
}

export const words = [
Expand Down

0 comments on commit f607708

Please sign in to comment.