diff --git a/packages/core/src/command/index.ts b/packages/core/src/command/index.ts index a0749ea0a0..5db07a2e41 100644 --- a/packages/core/src/command/index.ts +++ b/packages/core/src/command/index.ts @@ -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' @@ -61,8 +61,8 @@ export class Commander extends Map { }) 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)) @@ -124,12 +124,12 @@ export class Commander extends Map { }), 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) }) } diff --git a/packages/core/src/database.ts b/packages/core/src/database.ts index 438e176af2..e6f01e8bdc 100644 --- a/packages/core/src/database.ts +++ b/packages/core/src/database.ts @@ -112,7 +112,7 @@ export class DatabaseService extends Database { 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]) diff --git a/packages/core/src/session.ts b/packages/core/src/session.ts index 8368f6ac18..67498a3d55 100644 --- a/packages/core/src/session.ts +++ b/packages/core/src/session.ts @@ -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) }, diff --git a/packages/loader/src/shared.ts b/packages/loader/src/shared.ts index 4b4c4df905..fc181fb5f2 100644 --- a/packages/loader/src/shared.ts +++ b/packages/loader/src/shared.ts @@ -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' @@ -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) }) diff --git a/packages/loader/tests/loader.spec.ts b/packages/loader/tests/loader.spec.ts index 27c15fba9f..c9725e58c2 100644 --- a/packages/loader/tests/loader.spec.ts +++ b/packages/loader/tests/loader.spec.ts @@ -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) }) diff --git a/plugins/adapter/satori/src/index.ts b/plugins/adapter/satori/src/index.ts index 7c2fbd74cf..b19e88b6a4 100644 --- a/plugins/adapter/satori/src/index.ts +++ b/plugins/adapter/satori/src/index.ts @@ -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' diff --git a/plugins/mock/src/adapter.ts b/plugins/mock/src/adapter.ts index 2fa6dd9b3a..a46c9f63fc 100644 --- a/plugins/mock/src/adapter.ts +++ b/plugins/mock/src/adapter.ts @@ -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' { @@ -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 } } @@ -25,7 +25,7 @@ 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) } @@ -33,27 +33,30 @@ export class MockBot extends Bot { return new MessageClient(this, userId, channelId) } - receive(meta: Partial) { - const session = this.session(meta) + receive(client: MessageClient, body: Partial) { + 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 { +export class MockAdapter extends Adapter { public app: Context public webhook: Webhook @@ -76,13 +79,9 @@ export class MockAdapter extends Adapter.Server { return new MessageClient(this.bots[0], userId, channelId) } - session(meta: Partial) { + session(meta: Partial) { return this.bots[0].session(meta) } - - receive(meta: Partial) { - return this.bots[0].receive(meta) - } } export namespace MockAdapter { diff --git a/plugins/mock/src/client.ts b/plugins/mock/src/client.ts index 5bb3349215..5cf3b68500 100644 --- a/plugins/mock/src/client.ts +++ b/plugins/mock/src/client.ts @@ -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' @@ -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() { @@ -62,38 +62,24 @@ export class MockMessenger extends Messenger { export class MessageClient { public app: Context - public meta: Session.Payload & Partial - public resolve: (checkLength?: boolean) => void + public body: Partial + 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 } } } @@ -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 } }) }) }