diff --git a/packages/core/src/adapter.ts b/packages/core/src/adapter.ts index a017f132..4104daa2 100644 --- a/packages/core/src/adapter.ts +++ b/packages/core/src/adapter.ts @@ -23,9 +23,9 @@ export abstract class Adapter = Bo export namespace Adapter { export interface WsClientConfig { - retryLazy?: number - retryTimes?: number - retryInterval?: number + retryLazy: number + retryTimes: number + retryInterval: number } export const WsClientConfig: z = z.object({ @@ -35,7 +35,7 @@ export namespace Adapter { }).description('连接设置') export abstract class WsClientBase> extends Adapter { - protected socket: WebSocket + protected socket?: WebSocket protected connectionId = 0 protected abstract prepare(): Awaitable @@ -79,7 +79,7 @@ export namespace Adapter { let socket: WebSocket try { socket = await this.prepare() - } catch (error) { + } catch (error: any) { reconnect(initial, error.toString() || `failed to prepare websocket`) return } @@ -92,7 +92,7 @@ export namespace Adapter { }) socket.addEventListener('close', ({ code, reason }) => { - if (this.socket === socket) this.socket = null + if (this.socket === socket) this.socket = undefined logger.debug(`websocket closed with ${code}`) reconnect(initial, reason.toString() || `failed to connect to ${url}, code: ${code}`) }) @@ -125,7 +125,7 @@ export namespace Adapter { return this.bot.isActive } - setStatus(status: Status, error: Error = null) { + setStatus(status: Status, error?: Error) { this.bot.status = status this.bot.error = error } diff --git a/packages/core/src/bot.ts b/packages/core/src/bot.ts index 6da49832..907c07a8 100644 --- a/packages/core/src/bot.ts +++ b/packages/core/src/bot.ts @@ -37,7 +37,7 @@ export abstract class Bot { public platform: string public features: string[] public adapter?: Adapter - public error?: Error + public error: any public callbacks: Dict = {} public logger: Logger @@ -131,7 +131,7 @@ export abstract class Bot { online() { this.status = Status.ONLINE - this.error = null + this.error = undefined } offline(error?: Error) { @@ -145,7 +145,7 @@ export abstract class Bot { try { await this.context.parallel('bot-connect', this) await this.adapter?.connect(this) - } catch (error) { + } catch (error: any) { this.offline(error) } } @@ -193,7 +193,7 @@ export abstract class Bot { async createMessage(channelId: string, content: h.Fragment, referrer?: any, options?: SendOptions) { const { MessageEncoder } = this.constructor as typeof Bot - return new MessageEncoder(this, channelId, referrer, options).send(content) + return new MessageEncoder!(this, channelId, referrer, options).send(content) } async sendMessage(channelId: string, content: h.Fragment, referrer?: any, options?: SendOptions) { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index c6fadf94..7723d676 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -3,7 +3,7 @@ import { Awaitable, defineProperty, Dict } from 'cosmokit' import { Bot } from './bot' import { ExtractParams, InternalRequest, InternalRouter, JsonForm } from './internal' import { Session } from './session' -import { HTTP } from '@cordisjs/plugin-http' +import { FileResponse, HTTP } from '@cordisjs/plugin-http' import { Meta, Response, SendOptions } from '@satorijs/protocol' import h from '@satorijs/element' @@ -88,7 +88,7 @@ declare module '@cordisjs/plugin-http' { HTTP.createConfig = function createConfig(this, endpoint) { return z.object({ endpoint: z.string().role('link').description('要连接的服务器地址。') - .default(typeof endpoint === 'string' ? endpoint : null) + .default(typeof endpoint === 'string' ? endpoint : undefined!) .required(typeof endpoint === 'boolean' ? endpoint : false), headers: z.dict(String).role('table').description('要附加的额外请求头。'), ...this.Config.dict, @@ -172,7 +172,7 @@ export class Satori extends Service { public _loginSeq = 0 public _sessionSeq = 0 - constructor(ctx?: C) { + constructor(ctx: C) { super(ctx) ctx.mixin('satori', ['bots', 'component']) @@ -185,12 +185,12 @@ export class Satori extends Service { const { status, body, headers } = await self.handleInternalRoute('GET', url) if (status >= 400) throw new Error(`Failed to fetch ${_url}, status code: ${status}`) if (status >= 300) { - const location = headers?.get('location') + const location = headers?.get('location')! return this.file(location, options) } const type = headers?.get('content-type') const filename = headers?.get('content-disposition')?.split('filename=')[1] - return { data: body, filename, type, mime: type } + return { data: body, filename, type, mime: type } as FileResponse }) this._internalRouter = new InternalRouter(ctx) @@ -223,7 +223,7 @@ export class Satori extends Service { }) } - public bots = new Proxy([], { + public bots = new Proxy([] as Bot[], { get(target, prop) { if (prop in target || typeof prop === 'symbol') { return Reflect.get(target, prop) @@ -270,7 +270,7 @@ export class Satori extends Service { toJSON(meta = false): Meta { return { - logins: meta ? undefined : this.bots.map(bot => bot.toJSON()), + logins: meta ? undefined! : this.bots.map(bot => bot.toJSON()), proxyUrls: [...this.proxyUrls], } } diff --git a/packages/core/src/message.ts b/packages/core/src/message.ts index 28861e20..a9fc6ebd 100644 --- a/packages/core/src/message.ts +++ b/packages/core/src/message.ts @@ -12,7 +12,7 @@ class AggregateError extends Error { export abstract class MessageEncoder = Bot> { public errors: Error[] = [] public results: Message[] = [] - public session: C[typeof Context.session] + public session!: C[typeof Context.session] constructor(public bot: B, public channelId: string, public referrer?: any, public options: SendOptions = {}) {} @@ -38,7 +38,7 @@ export abstract class MessageEncoder { public id: number // for backward compatibility public sn: number - public bot: Bot - public app: C['root'] + public bot!: Bot + public app!: C['root'] public event: Event public locales: string[] = [] @@ -59,21 +59,21 @@ export class Session { } get isDirect() { - return this.event.channel.type === Channel.Type.DIRECT + return this.event.channel?.type === Channel.Type.DIRECT } set isDirect(value) { (this.event.channel ??= {} as Channel).type = value ? Channel.Type.DIRECT : Channel.Type.TEXT } - get author(): GuildMember & User { + get author() { return { ...this.event.user, ...this.event.member, userId: this.event.user?.id, username: this.event.user?.name, nickname: this.event.member?.name, - } + } as GuildMember & User } get uid() { @@ -114,7 +114,7 @@ export class Session { this.event.message.quote = undefined this.event.message.elements = isNullable(value) ? value : h.parse(value) if (this.event.message.elements?.[0]?.type === 'quote') { - const el = this.event.message.elements.shift() + const el = this.event.message.elements.shift()! this.event.message.quote = Resource.decode(el) } } @@ -146,7 +146,7 @@ export class Session { event.message.content = this.content delete event.message.elements if (event.message.quote) { - event.message.content = Resource.encode('quote', event.message.quote) + event.message.content + event.message.content = Resource.encode('quote', event.message.quote) + event.message.content! } } return event @@ -163,7 +163,7 @@ export function defineAccessor(prototype: {}, name: string, keys: string[]) { // See https://github.com/satorijs/satori/issues/166 if (value === undefined) return const _keys = keys.slice() - const last = _keys.pop() + const last = _keys.pop()! const data = _keys.reduce((data, key) => data[key] ??= {}, this) data[last] = value }, diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index 0b6e1c44..3378ee05 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -3,6 +3,8 @@ "compilerOptions": { "rootDir": "src", "outFile": "lib/index.d.ts", + "strict": true, + "noImplicitAny": false, }, "include": [ "src" diff --git a/packages/protocol/src/index.ts b/packages/protocol/src/index.ts index db08928f..07eaa011 100644 --- a/packages/protocol/src/index.ts +++ b/packages/protocol/src/index.ts @@ -492,7 +492,7 @@ export namespace WebSocket { } export interface WebSocket { - readonly url?: string + readonly url: string readonly protocol?: string readonly readyState?: number close(code?: number, reason?: string): void