Skip to content

Commit

Permalink
feat(satori): make login.adapter required
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 23, 2025
1 parent ee7c018 commit 060df0b
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 21 deletions.
11 changes: 3 additions & 8 deletions adapters/satori/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot, camelCase, Context, h, HTTP, JsonForm, omit, pick, snakeCase, Universal } from '@satorijs/core'
import { Bot, camelCase, Context, h, HTTP, JsonForm, omit, snakeCase, Universal } from '@satorijs/core'
import { SatoriAdapter } from './ws'

function createInternal<C extends Context = Context>(bot: SatoriBot<C>, prefix = '') {
Expand Down Expand Up @@ -62,12 +62,11 @@ export class SatoriBot<C extends Context = Context> extends Bot<C, Universal.Log
declare adapter: SatoriAdapter<C, this>

public internal = createInternal(this)
public upstream: Pick<Universal.Login, 'sn' | 'adapter'>

constructor(ctx: C, config: Universal.Login) {
super(ctx, config, 'satori')
super(ctx, config, config.adapter)
this.logger = ctx.logger('satori')
Object.assign(this, omit(config, ['sn', 'adapter']))
this.upstream = pick(config, ['sn', 'adapter'])

this.defineInternalRoute('/*path', async ({ method, params, query, headers, body }) => {
const response = await this.request(`/v1/${this.getInternalUrl('/' + params.path, query, true)}`, {
Expand All @@ -85,10 +84,6 @@ export class SatoriBot<C extends Context = Context> extends Bot<C, Universal.Log
})
}

get adapterName() {
return this.upstream.adapter
}

request(url: string, config: HTTP.RequestConfig) {
return this.adapter.http(url, {
...config,
Expand Down
2 changes: 1 addition & 1 deletion adapters/satori/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class SatoriAdapter<C extends Context = Context, B extends SatoriBot<C> =

getBot(login: Universal.Login, action?: 'created' | 'updated' | 'removed') {
// FIXME Do not dispatch event from outside adapters.
let bot = this.bots.find(bot => bot.upstream.sn === login.sn)
let bot = this.bots.find(bot => bot.config.sn === login.sn)
if (bot) {
if (action === 'created') {
this.logger.warn('bot already exists when login created, sn = %s, adapter = %s', login.sn, login.adapter)
Expand Down
5 changes: 4 additions & 1 deletion adapters/whatsapp/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export class WhatsAppBot<C extends Context = Context> extends Bot<C> {

public internal: Internal
public http: HTTP
public platform = 'whatsapp'

constructor(ctx: C) {
super(ctx, {}, 'whatsapp')
}

async createReaction(channelId: string, messageId: string, emoji: string): Promise<void> {
await this.internal.messageReaction(this.selfId, channelId, messageId, emoji)
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Bot } from './bot'
export abstract class Adapter<C extends Context = Context, B extends Bot<C> = Bot<C>> {
static schema = false as const

public name?: string
public bots: B[] = []

constructor(protected ctx: C) {}
Expand Down
12 changes: 3 additions & 9 deletions packages/core/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,15 @@ export abstract class Bot<C extends Context = Context, T = any> {
protected context: Context
protected _status: Status = Status.OFFLINE

constructor(public ctx: C, public config: T, platform?: string) {
constructor(public ctx: C, public config: T, public adapterName: string) {
this.sn = ++ctx.satori._loginSeq
this.internal = null
this._internalRouter = new InternalRouter(ctx)
this.context = ctx
ctx.bots.push(this)
this.context.emit('bot-added', this)
if (platform) {
this.logger = ctx.logger(platform)
this.platform = platform
}
this.logger = ctx.logger(adapterName)
this.platform = adapterName

this.features = Object.entries(Methods)
.filter(([, value]) => this[value.name])
Expand All @@ -76,10 +74,6 @@ export abstract class Bot<C extends Context = Context, T = any> {
})
}

get adapterName() {
return this.adapter.name
}

getInternalUrl(path: string, init?: ConstructorParameters<typeof URLSearchParams>[0], slash?: boolean) {
let search = new URLSearchParams(init).toString()
if (search) search = '?' + search
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export interface GuildMember {

export interface Login {
sn: number
adapter?: string
adapter: string
user?: User
platform?: string
/** @deprecated use `login.user.id` instead */
Expand Down

0 comments on commit 060df0b

Please sign in to comment.