Skip to content

Commit

Permalink
各自小修改,测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceale committed Oct 13, 2024
1 parent b15776c commit 9c0afda
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 59 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,6 @@ dist

# Finder (MacOS) folder config
.DS_Store


lib/
Binary file modified bun.lockb
Binary file not shown.
41 changes: 33 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
{
"name": "core",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
"name": "shirosaki-onebot",
"version": "0.1.0",
"description": "极简OneBot v11框架",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"exports": {
".": {
"import": "./lib/index.js",
"require": "./lib/index.cjs",
"types": "./lib/index.d.ts"
},
"./package.json": "./package.json"
},
"files": [
"lib"
],
"scripts": {
"build": "tsc"
},
"peerDependencies": {
"typescript": "^5.0.0"
"author": "筱莱Ceale",
"license": "Apache-2.0",
"keywords": [
"typescript",
"onebot",
"onebot-sdk",
"onebot11"
],
"homepage": "https://github.com/Ceale/shirosaki-onebot",
"repository": {
"url": "git+https://github.com/Ceale/shirosaki-onebot.git"
},
"devDependencies": {
"typescript": "^4.9.4"
}
}
}
69 changes: 36 additions & 33 deletions src/Bot.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { WebSocket } from "ws"
import Logger from "./Log.ts"
import { generateRandomHex, wait } from "./Util.ts"
import { Message } from "./Message.ts"
import { At, MessageSegmentType, Text } from "./MessageSegment.ts"
import Logger from "./Log"
import { generateRandomHex, wait } from "./Util"
import { Message } from "./Message"
import { At, MessageSegmentType, Text } from "./MessageSegment"
import { error, log } from "node:console"

declare global {
Expand Down Expand Up @@ -30,7 +30,7 @@ export class Sender {
level: string | undefined
title: string | undefined
role: "owner"|"admin"|"member" | undefined
constructor(id, sender) {
constructor(id: number, sender: any) {
this.id = id
this.name = sender.nickname
this.nickname = sender.card || sender.nickname
Expand Down Expand Up @@ -84,7 +84,7 @@ export class Source {
groupId?: number
userId: number

constructor(type, userId, groupId?) {
constructor(type: SouceType, userId: number, groupId?: number) {
this.type = type
this.userId = userId
this.groupId = groupId
Expand All @@ -101,8 +101,6 @@ export class Source {
}

export class MessageEvent {
private bot: Bot

public timestamp: number

public source: Source
Expand All @@ -114,7 +112,7 @@ export class MessageEvent {

public session: Session

constructor(event, bot) {
constructor(event: any, bot: Bot) {
this.timestamp = event.timestamp
this.source = new Source(event.message_type, event.user_id, event.group_id)

Expand Down Expand Up @@ -144,7 +142,7 @@ export class Session {
waitAnswer(timeout: number): Promise<{message: Message, messageId: number}|null> {
return new Promise((resolve) => {
this.bot.sessionList.add(this.source)
const handle = (event) => {
const handle = (event: any) => {
const messageEvent = new MessageEvent(event, this.bot)
if (this.source.equals(messageEvent.source)) {
this.bot.sessionList.delete(this.source)
Expand All @@ -170,41 +168,42 @@ export class Session {
}
}

export class NoticeEventData {
constructor() {
// export class NoticeEventData {
// constructor() {

}
timestamp: number
selfid: number
// }
// timestamp: number
// selfid: number

}
// }

export class RequestEventData {
constructor() {
// export class RequestEventData {
// constructor() {

}
timestamp: number
selfid: number
// }
// timestamp: number
// selfid: number

}
// }

export class MetaEventData {
timestamp: number
selfid: number
// export class MetaEventData {
// timestamp: number
// selfid: number

}
// }

export default class Bot {
private options: {
url: string,
debug: boolean
retryInterval: number
}
private connection: WebSocket
private connection: WebSocket | null = null

private id: number
private name: string
private id: number | undefined
private name: string | undefined
public getBotInfo() {
if (this.connection === null) throw new Error("当前未连接")
return {
id: this.id,
name: this.name
Expand Down Expand Up @@ -232,6 +231,7 @@ export default class Bot {
}

public connect(): Promise<void> {
if (this.connection !== null) throw new Error("当前已连接")
this.initializeConnection()
return new Promise(resolve => {
this.onEvent(EventType.Meta, (event) => {
Expand All @@ -249,7 +249,9 @@ export default class Bot {
}

public disconnect(): Promise<void> {
if (this.connection === null) throw new Error("当前未连接")
return new Promise(resolve => {
if (this.connection === null) return
const connection = this.connection
this.connection = null
connection.on("close", () => {
Expand Down Expand Up @@ -317,19 +319,20 @@ export default class Bot {

private requestsList = new Map()

public callApi(action, params?): Promise<any> {
public callApi(action: string, params?: object): Promise<any> {
if (this.connection === null) throw new Error("当前未连接")
return new Promise((resolve, reject) => {
const requestId = generateRandomHex(8)
this.requestsList.set(requestId, { resolve, reject })
this.connection.send(Buffer.from(JSON.stringify({
this.connection!.send(Buffer.from(JSON.stringify({
action: action,
params: params || {},
echo: requestId
})))
})
}

private handleResponse(response) {
private handleResponse(response: any) {
const requestId = response.echo
if (this.requestsList.has(requestId)) {
const { resolve, reject } = this.requestsList.get(requestId)
Expand Down Expand Up @@ -374,7 +377,7 @@ export default class Bot {
userId?: number[]
}
}) {
const handle = (eventData) => {
const handle = (eventData: any) => {
const messageEvent = new MessageEvent(eventData, this)

for (const souce of this.sessionList) {
Expand Down
14 changes: 7 additions & 7 deletions src/Log.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
export default class Logger {
private name
constructor(name) {
private name: string
constructor(name: string) {
this.name = name
}

debug(str) {
debug(str: string) {
console.debug(this.#format(str, "debug"))
}

info(str) {
info(str: string) {
console.info(this.#format(str, "info"))
}

warn(str) {
warn(str: string) {
console.warn(this.#format(str, "warn"))
}

error(str) {
error(str: string) {
console.error(this.#format(str, "error"))
}

#format(str, mode) {
#format(str: string, mode: "debug"|"info"|"warn"|"error") {
const date = new Date()
const dateText = `${date.getUTCFullYear()}/${date.getMonth()+1}/${date.getDate()} ${date.getHours().toString().padStart(2, "0")}:${date.getMinutes().toString().padStart(2, "0")}:${date.getSeconds().toString().padStart(2, "0")}`
return `[${dateText}] [${this.name}/${mode}] ${str}`
Expand Down
2 changes: 1 addition & 1 deletion src/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ export class Message {
}
return null
}).filter(segment => segment !== null)
return new Message(...segments)
return new Message(...(segments as MessageSegment[]))
}
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./Bot"
export * from "./Message"
export * from "./MessageSegment"
export * from "./ModuleLoader"
export * as Util from "./Util"
40 changes: 30 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
}
}
"moduleResolution": "node",
"outDir": "lib",
"rootDir": "src",
"sourceMap": true,
"declaration": true,
"declarationMap": true,
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmitOnError": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts",
"**/*.test.ts"
]
}

0 comments on commit 9c0afda

Please sign in to comment.