Skip to content

Commit

Permalink
fix(core): add generic to support strict bot API
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 27, 2023
1 parent 78fa90d commit e1ca284
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
6 changes: 2 additions & 4 deletions packages/core/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import { Status, WebSocket } from '@satorijs/protocol'
import { Context } from '.'
import { Bot } from './bot'
import Schema from 'schemastery'
import Logger from 'reggol'

const logger = new Logger('adapter')

export abstract class Adapter<C extends Context = Context, B extends Bot<C> = Bot<C>> {
static schema = false
static schema = false as const

public bots: B[] = []

Expand Down Expand Up @@ -51,6 +48,7 @@ export namespace Adapter {

async start() {
let _retryCount = 0
const logger = this.ctx.logger('adapter')
const { retryTimes, retryInterval, retryLazy } = this.config

const reconnect = async (initial = false) => {
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ export interface Events<C extends Context = Context> extends cordis.Events<C> {
'before-send'(session: GetSession<C>, options: SendOptions): Awaitable<void | boolean>
'send'(session: GetSession<C>): void
/** @deprecated use `login-added` instead */
'bot-added'(client: Bot): void
'bot-added'(client: Bot<C>): void
/** @deprecated use `login-removed` instead */
'bot-removed'(client: Bot): void
'bot-removed'(client: Bot<C>): void
/** @deprecated use `login-updated` instead */
'bot-status-updated'(client: Bot): void
'bot-connect'(client: Bot): Awaitable<void>
'bot-disconnect'(client: Bot): Awaitable<void>
'bot-status-updated'(client: Bot<C>): void
'bot-connect'(client: Bot<C>): Awaitable<void>
'bot-disconnect'(client: Bot<C>): Awaitable<void>
}

export interface Context {
Expand All @@ -107,7 +107,7 @@ export class Context extends cordis.Context {
// remove generic type to loosen the constraint
static readonly Session = Session as new (bot: Bot, event: Partial<Event>) => Session

public bots = new Proxy([] as Bot[] & Dict<Bot>, {
public bots = new Proxy([] as Bot<this>[] & Dict<Bot<this>>, {
get(target, prop) {
if (prop in target || typeof prop === 'symbol') {
return Reflect.get(target, prop)
Expand All @@ -130,7 +130,7 @@ export class Context extends cordis.Context {

this.http = new Quester(config.request)

this.on('internal/warning', (format, ...args) => {
this.on('internal/warning', function (format, ...args) {
this.logger('app').warn(format, ...args)
})
}
Expand Down

0 comments on commit e1ca284

Please sign in to comment.