Skip to content

Commit

Permalink
feat(satori): support login.adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 4, 2024
1 parent 26064aa commit a88972e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
9 changes: 7 additions & 2 deletions packages/core/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Bot extends Methods {
internal: any
}

export abstract class Bot<C extends Context = Context, T = any> implements Login {
export abstract class Bot<C extends Context = Context, T = any> {
static reusable = true
static MessageEncoder?: new (bot: Bot, channelId: string, guildId?: string, options?: SendOptions) => MessageEncoder

Expand Down Expand Up @@ -233,7 +233,12 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
}

toJSON(): Login {
return clone(pick(this, ['platform', 'selfId', 'status', 'user', 'hidden', 'features', 'proxyUrls']))
return clone({
...pick(this, ['platform', 'selfId', 'status', 'hidden', 'features', 'proxyUrls']),
// make sure `user.id` is present
user: this.user.id ? this.user : undefined,
adapter: this.platform,
})
}

async getLogin() {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class Session<C extends Context = Context> {
return {
login: {
platform: this.platform,
user: { id: this.userId },
user: { id: this.selfId },
} as Login,
...this.event,
id: this.id,
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export interface GuildMember {
}

export interface Login {
adapter: string
user?: User
platform?: string
/** @deprecated use `login.user.id` instead */
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class SatoriServer extends Service<SatoriServer.Config> {

if (checkAuth(koa)) return

const selfId = koa.request.headers['satori-login-id'] ?? koa.request.headers['x-self-id']
const selfId = koa.request.headers['satori-user-id'] ?? koa.request.headers['x-self-id']
const platform = koa.request.headers['satori-platform'] ?? koa.request.headers['x-platform']
const bot = ctx.bots.find(bot => bot.selfId === selfId && bot.platform === platform)
if (!bot) {
Expand Down

0 comments on commit a88972e

Please sign in to comment.