Skip to content

Commit

Permalink
feat(satori): migrate core code to satori v3
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 30, 2023
1 parent 07f3e46 commit 9e7c534
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 64 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/command/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Awaitable, defineProperty } from 'cosmokit'
import { Bot, Context, h, Schema, Session } from '@satorijs/core'
import { Bot, Context, h, Schema, Session, Universal } from '@satorijs/core'
import { Command } from './command'
import { Argv } from './parser'
import validate from './validate'
Expand Down Expand Up @@ -61,8 +61,8 @@ export class Commander extends Map<string, Command> {
})

ctx.on('interaction/command', (session) => {
if (session.data?.argv) {
const { name, options, arguments: args } = session.data.argv
if (session.body?.argv) {
const { name, options, arguments: args } = session.body.argv
session.execute({ name, args, options })
} else {
defineProperty(session, 'argv', ctx.bail('before-parse', session.content, session))
Expand Down Expand Up @@ -124,12 +124,12 @@ export class Commander extends Map<string, Command> {
}), 1000)

ctx.on('ready', () => {
const bots = ctx.bots.filter(v => v.status === 'online' && v.updateCommands)
const bots = ctx.bots.filter(v => v.status === Universal.Status.ONLINE && v.updateCommands)
bots.forEach(bot => this.updateCommands(bot))
})

ctx.on('bot-status-updated', async (bot) => {
if (bot.status !== 'online' || !bot.updateCommands) return
if (bot.status !== Universal.Status.ONLINE || !bot.updateCommands) return
this.updateCommands(bot)
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class DatabaseService extends Database<Tables> {
primary: ['id', 'platform'],
})

app.on('bot-added', ({ platform }) => {
app.on('login-added', ({ platform }) => {
if (platform in this.tables.user.fields) return
this.migrate('user', { [platform]: 'string(255)' }, async (db) => {
const users = await db.get('user', { [platform]: { $exists: true } }, ['id', platform as never])
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ extend(Session.prototype as Session.Private, {
get username() {
const defaultName: string = this.user && this.user['name']
? this.user['name']
: this.author
? this.author.nickname || this.author.username
: this.userId
: this.author.name || this.userId
return this.app.chain('appellation', defaultName, this)
},

Expand Down
4 changes: 2 additions & 2 deletions packages/loader/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Dict, EffectScope, ForkScope, interpolate, isNullable, Logger, Plugin, resolveConfig, valueMap, version } from '@koishijs/core'
import { Context, Dict, EffectScope, ForkScope, interpolate, isNullable, Logger, Plugin, resolveConfig, Universal, valueMap, version } from '@koishijs/core'
import { constants, promises as fs } from 'fs'
import * as yaml from 'js-yaml'
import * as path from 'path'
Expand Down Expand Up @@ -376,7 +376,7 @@ export abstract class Loader {
const { sid, channelId, guildId, content } = app.envData.message
app.envData.message = null
const dispose = app.on('bot-status-updated', (bot) => {
if (bot.sid !== sid || bot.status !== 'online') return
if (bot.sid !== sid || bot.status !== Universal.Status.ONLINE) return
dispose()
bot.sendMessage(channelId, content, guildId)
})
Expand Down
12 changes: 6 additions & 6 deletions packages/loader/tests/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ describe('@koishijs/loader', () => {
expect(bar.mock.calls).to.have.length(0)
expect(baz.mock.calls).to.have.length(0)

let { meta } = app.mock.client('123', '456')
app.emit(app.mock.session(meta), 'test/bar' as any)
app.emit(app.mock.session(meta), 'test/baz' as any)
let { body } = app.mock.client('123', '456')
app.emit(app.mock.session(body), 'test/bar' as any)
app.emit(app.mock.session(body), 'test/baz' as any)
expect(bar.mock.calls).to.have.length(0)
expect(baz.mock.calls).to.have.length(1)

meta = app.mock.client('321', '456').meta
app.emit(app.mock.session(meta), 'test/bar' as any)
app.emit(app.mock.session(meta), 'test/baz' as any)
body = app.mock.client('321', '456').body
app.emit(app.mock.session(body), 'test/bar' as any)
app.emit(app.mock.session(body), 'test/baz' as any)
expect(bar.mock.calls).to.have.length(0)
expect(baz.mock.calls).to.have.length(1)
})
Expand Down
4 changes: 2 additions & 2 deletions plugins/adapter/satori/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SatoriBot } from '@satorijs/adapter-satori'
import { SatoriAdapter } from '@satorijs/adapter-satori'

export default SatoriBot
export default SatoriAdapter
export * from '@satorijs/adapter-satori'
37 changes: 18 additions & 19 deletions plugins/mock/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Adapter, Bot, Channel, Context, Session, User } from 'koishi'
import { MessageClient } from './client'
import { Adapter, Bot, Channel, Context, Session, Universal, User } from 'koishi'
import { MessageClient, MockMessenger } from './client'
import { Webhook } from './webhook'

declare module 'koishi' {
Expand All @@ -15,7 +15,7 @@ declare module 'koishi' {
export const DEFAULT_SELF_ID = '514'

export namespace MockBot {
export interface Config extends Bot.Config {
export interface Config {
selfId: string
}
}
Expand All @@ -25,35 +25,38 @@ export class MockBot extends Bot {
super(ctx, config)
this.platform = 'mock'
this.selfId = config.selfId ?? DEFAULT_SELF_ID
this.status = 'online'
this.status = Universal.Status.ONLINE
ctx.plugin(MockAdapter, this)
}

client(userId: string, channelId?: string) {
return new MessageClient(this, userId, channelId)
}

receive(meta: Partial<Session>) {
const session = this.session(meta)
receive(client: MessageClient, body: Partial<Universal.Event>) {
const session = this.session(body)
session.send = function (this: Session, fragment, options = {}) {
options.session = this
return new MockMessenger(client, options).send(fragment)
}
this.dispatch(session)
return session.id
}

async getMessage(channelId: string, messageId: string) {
const idDirect = channelId.startsWith('private:')
async getMessage(channelId: string, id: string) {
const isDirect = channelId.startsWith('private:')
return {
messageId,
channelId,
id,
messageId: id,
channel: { id: channelId, type: isDirect ? Universal.Channel.Type.DIRECT : Universal.Channel.Type.TEXT },
content: '',
time: 0,
idDirect,
subtype: idDirect ? 'private' : 'group',
author: { userId: this.selfId },
user: { id: this.selfId },
}
}
}

export class MockAdapter extends Adapter.Server<MockBot> {
export class MockAdapter extends Adapter<MockBot> {
public app: Context
public webhook: Webhook

Expand All @@ -76,13 +79,9 @@ export class MockAdapter extends Adapter.Server<MockBot> {
return new MessageClient(this.bots[0], userId, channelId)
}

session(meta: Partial<Session>) {
session(meta: Partial<Universal.Event>) {
return this.bots[0].session(meta)
}

receive(meta: Partial<Session>) {
return this.bots[0].receive(meta)
}
}

export namespace MockAdapter {
Expand Down
38 changes: 12 additions & 26 deletions plugins/mock/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert'
import { Context, h, hyphenate, isNullable, Messenger, SendOptions, Session, Universal } from 'koishi'
import { Context, h, hyphenate, isNullable, Messenger, Universal } from 'koishi'
import { format } from 'util'
import { MockBot } from './adapter'

Expand All @@ -12,8 +12,8 @@ const RECEIVED_NTH_OTHERWISE = 'expected "%s" to be replied with %s at index %s
export class MockMessenger extends Messenger {
private buffer = ''

constructor(private client: MessageClient, options?: SendOptions) {
super(client.bot, client.meta.channelId, client.meta.guildId, options)
constructor(private client: MessageClient, options?: Universal.SendOptions) {
super(client.bot, client.body.channel.id, client.body.guild?.id, options)
}

async flush() {
Expand Down Expand Up @@ -62,38 +62,24 @@ export class MockMessenger extends Messenger {

export class MessageClient {
public app: Context
public meta: Session.Payload & Partial<Session>
public resolve: (checkLength?: boolean) => void
public body: Partial<Universal.Event>
public resolve: (checkLength?: boolean) => void = () => {}
public replies: string[] = []

constructor(public bot: MockBot, public userId: string, public channelId?: string) {
this.app = bot.ctx.root
this.meta = {
this.body = {
platform: 'mock',
type: 'message',
selfId: bot.selfId,
userId,
author: {
userId,
username: '' + userId,
},
user: { id: userId, name: '' + userId },
}

if (channelId) {
this.meta.guildId = channelId
this.meta.channelId = channelId
this.meta.subtype = 'group'
this.body.guild = { id: channelId }
this.body.channel = { id: channelId, type: Universal.Channel.Type.TEXT }
} else {
this.meta.channelId = 'private:' + userId
this.meta.subtype = 'private'
this.meta.isDirect = true
}

const self = this
this.resolve = () => {}
this.meta.send = function (this: Session, fragment, options = {}) {
options.session = this
return new MockMessenger(self, options).send(fragment)
this.body.channel = { id: 'private:' + userId, type: Universal.Channel.Type.DIRECT }
}
}

Expand All @@ -115,10 +101,10 @@ export class MessageClient {
const elements = h.parse(content)
if (elements[0]?.type === 'quote') {
const { attrs, children } = elements.shift()
quote = { messageId: attrs.id, elements: children, content: children.join('') }
quote = { id: attrs.id, messageId: attrs.id, elements: children, content: children.join('') }
content = elements.join('')
}
const uuid = this.bot.receive({ ...this.meta, content, elements, quote })
const uuid = this.bot.receive(this, { ...this.body, message: { content, elements, quote } })
})
}

Expand Down

0 comments on commit 9e7c534

Please sign in to comment.