Skip to content

Commit

Permalink
feat(core): support bot.userId for auto selfId access
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 1, 2023
1 parent b274921 commit 953f1c6
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 18 deletions.
6 changes: 2 additions & 4 deletions adapters/discord/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ export class WsClient extends Adapter.WsClient<DiscordBot> {
if (parsed.t === 'READY') {
this._sessionId = parsed.d.session_id
this._resumeUrl = parsed.d.resume_gateway_url
const self: any = decodeUser(parsed.d.user)
self.selfId = self.userId
delete self.userId
Object.assign(this.bot, self)
const user = decodeUser(parsed.d.user)
Object.assign(this.bot, user)
logger.debug('session_id ' + this._sessionId)
return this.bot.online()
}
Expand Down
1 change: 0 additions & 1 deletion adapters/lark/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export function adaptMessage(bot: FeishuBot, data: Events['im.message.receive_v1

export function adaptSession(bot: FeishuBot, body: AllEvents): Session {
const session = bot.session()
session.selfId = bot.selfId
const internal = Object.create(bot.internal)
Object.assign(internal, body)
defineProperty(session, 'feishu', internal)
Expand Down
6 changes: 2 additions & 4 deletions adapters/line/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ export class LineBot extends Bot<LineBot.Config> {
}

async initialize(callback: (bot: this) => Promise<void>) {
const { userId, pictureUrl, displayName } = await this.internal.getBotInfo()
this.selfId = userId
this.username = displayName
if (pictureUrl) this.avatar = pictureUrl
const user = await this.getSelf()
Object.assign(this, user)
await callback(this)
this.online()
}
Expand Down
1 change: 0 additions & 1 deletion adapters/onebot/src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class OneBotBot<T extends OneBotBot.Config = OneBotBot.Config> extends Ba

constructor(ctx: Context, config: T) {
super(ctx, config)
this.selfId = config.selfId
this.internal = new OneBot.Internal()
this.avatar = `http://q.qlogo.cn/headimg_dl?dst_uin=${config.selfId}&spec=640`

Expand Down
2 changes: 1 addition & 1 deletion adapters/onebot/src/bot/qqguild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export namespace QQGuildBot {
}

export class QQGuildBot extends BaseBot {
parent: OneBotBot
declare parent: OneBotBot
hidden = true

constructor(ctx: Context, config: QQGuildBot.Config) {
Expand Down
7 changes: 2 additions & 5 deletions adapters/telegram/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ export class TelegramBot<T extends TelegramBot.Config = TelegramBot.Config> exte
}

async initialize(callback: (bot: this) => Promise<void>) {
const { username, userId, avatar, nickname } = await this.getLoginInfo()
this.username = username
this.avatar = avatar
this.selfId = userId
this.nickname = nickname
const user = await this.getLoginInfo()
Object.assign(this, user)
await callback(this)
logger.debug('connected to %c', 'telegram:' + this.selfId)
this.online()
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import WebSocket from 'ws'

export interface Bot extends Universal.Methods, Universal.User {
socket?: WebSocket
internal: any
}

export abstract class Bot<T extends Bot.Config = Bot.Config> {
Expand All @@ -19,7 +20,6 @@ export abstract class Bot<T extends Bot.Config = Bot.Config> {
public hidden = false
public platform: string
public selfId: string
public internal = null
public adapter?: Adapter<this>
public error?: Error

Expand All @@ -45,6 +45,14 @@ export abstract class Bot<T extends Bot.Config = Bot.Config> {
})
}

get userId() {
return this.selfId
}

set userId(value) {
this.selfId = value
}

get status() {
return this._status
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2019",
"target": "es2022",
"module": "commonjs",
"declaration": true,
"emitDeclarationOnly": true,
Expand Down

0 comments on commit 953f1c6

Please sign in to comment.