Skip to content

Commit

Permalink
feat(satori): support association for bot
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 12, 2024
1 parent 5172e2e commit 3de44dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
23 changes: 13 additions & 10 deletions packages/core/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import h from '@satorijs/element'
import { Context } from '.'
import { Adapter } from './adapter'
import { MessageEncoder } from './message'
import { defineAccessor } from './session'
import { defineAccessor, Session } from './session'
import { Event, List, Login, Methods, SendOptions, Status, User } from '@satorijs/protocol'

const eventAliases = [
Expand Down Expand Up @@ -37,25 +37,29 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
constructor(public ctx: C, public config: T, platform?: string) {
this.internal = null
this.context = ctx
ctx.bots.push(this)
this.context.emit('bot-added', this)
this[Context.current] = ctx
const self = Context.associate(this, 'bot')
ctx.bots.push(self)
self.context.emit('bot-added', self)
if (platform) {
this.logger = ctx.logger(platform)
this.platform = platform
self.logger = ctx.logger(platform)
self.platform = platform
}

ctx.on('ready', async () => {
await Promise.resolve()
this.dispatchLoginEvent('login-added')
return this.start()
self.dispatchLoginEvent('login-added')
return self.start()
})

ctx.on('dispose', () => this.dispose())
ctx.on('dispose', () => self.dispose())

ctx.on('interaction/button', (session: C[typeof Context.session]) => {
ctx.on('interaction/button', (session) => {
const cb = this.callbacks[session.event.button.id]
if (cb) cb(session)
})

return self
}

update(login: Login) {
Expand Down Expand Up @@ -136,7 +140,6 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
}

session(event: Partial<Event> = {}): C[typeof Context.session] {
const { Session } = this.ctx.constructor as typeof Context
return new Session(this, event)
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,12 @@ export class Context extends cordis.Context {

constructor(config?: any) {
super(config)
this.provide('http', undefined, true)
this.provide('satori', undefined, true)
this.plugin(HTTP, config.request)
this.plugin(Satori)
}
}

export default class Satori<C extends Context> extends cordis.Service<unknown, C> {
export class Satori<C extends Context = Context> extends cordis.Service<unknown, C> {
static [cordis.Service.provide] = 'satori'
static [cordis.Service.immediate] = true

Expand Down Expand Up @@ -157,3 +155,5 @@ export default class Satori<C extends Context> extends cordis.Service<unknown, C
return this.ctx.set('component:' + name, render)
}
}

export default Satori

0 comments on commit 3de44dc

Please sign in to comment.