Skip to content

Commit

Permalink
fix:OneBot v11数据类型错误(涉及字段:message_id、font、user_id、group_id、self_id、di…
Browse files Browse the repository at this point in the history
…scuss_id)
  • Loading branch information
lc-cn committed Jul 19, 2023
1 parent 2bcf62d commit 65d94ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/service/V11/action/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export class CommonAction {
* 获取消息
* @param message_id {string} 消息id
*/
getMsg(this: V11, message_id: string) {
return this.client.getMsg(message_id)
getMsg(this: V11, message_id: number) {
const messageId=this.db.get(`KVMap.${message_id}`)
return this.client.getMsg(messageId as string)
}

/**
Expand Down
30 changes: 23 additions & 7 deletions src/service/V11/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ import {WebSocket, WebSocketServer} from "ws";
import {Dispose} from "@/types";
import {Context} from "koa";
import {URL} from "url";
import {toBool, toHump, toLine, transformObj} from "@/utils";
import {toBool, toHump, toLine} from "@/utils";
import {fromCqcode, fromSegment, toCqcode, toSegment} from "icqq-cq-enable";
import {BOOLS, NotFoundError} from "@/onebot";
import http from "http";
import https from "https";
import {EventEmitter} from "events";
import {rmSync} from "fs";
import {Database} from "@/db";
import {join} from "path";
import {App} from "@/server/app";
import {V12} from "@/service/V12";

export class V11 extends EventEmitter implements OneBot.Base {
public action: Action
public version = 'V11'
protected timestamp = Date.now()
protected heartbeat?: NodeJS.Timeout
private path: string
db: Database<{ eventBuffer: V12.Payload<keyof Action>[],KVMap:Record<number, string>, files: Record<string, V12.FileInfo> }>
disposes: Dispose[]
protected _queue: Array<{
method: keyof Action,
Expand All @@ -34,6 +39,8 @@ export class V11 extends EventEmitter implements OneBot.Base {
constructor(public oneBot: OneBot<'V11'>, public client: Client, public config: V11.Config) {
super()
this.action = new Action()
this.db = new Database(join(App.configDir, 'data', this.oneBot.uin + '.json'))
this.db.sync({eventBuffer: [],KVMap:{}, files: {}})
this.logger = this.oneBot.app.getLogger(this.oneBot.uin, this.version)
}

Expand Down Expand Up @@ -65,7 +72,7 @@ export class V11 extends EventEmitter implements OneBot.Base {
if (this.config.heartbeat) {
this.heartbeat = setInterval(() => {
this.dispatch({
self_id: this.oneBot.uin + '',
self_id: this.oneBot.uin,
status: {
online: this.client.status === OnlineStatus.Online,
good: this.oneBot.status === OneBotStatus.Good
Expand Down Expand Up @@ -203,11 +210,20 @@ export class V11 extends EventEmitter implements OneBot.Base {
if (data.message && data.post_type === 'message') {
data.message = this.config.post_message_format === 'array' ? toSegment(data.message) : toCqcode(data)
}
if(data.message_id) {
this.db.set(`KVMap.${data.seq}`,data.message_id)
data.message_id = data.seq
}
if(data.font){
const fontNo=Buffer.from(data.font).readUInt32BE()
this.db.set(`KVMap.${data.fontNo}`,data.font)
data.font = fontNo
}
data.time = Math.floor(Date.now() / 1000)
data = transformObj(data, (key, value) => {
if (!['user_id', 'group_id', 'discuss_id', 'member_id', 'channel_id', 'guild_id'].includes(key)) return value
return value + ''
})
// data = transformObj(data, (key, value) => {
// if (!['user_id', 'group_id', 'discuss_id', 'member_id', 'channel_id', 'guild_id'].includes(key)) return value
// return value + ''
// })
this.emit('dispatch', JSON.stringify(data))
}

Expand Down Expand Up @@ -506,7 +522,7 @@ export namespace V11 {

export function genMetaEvent(uin: number, type: string) {
return {
self_id: uin + '',
self_id: uin,
time: Math.floor(Date.now() / 1000),
post_type: "meta_event",
meta_event_type: "lifecycle",
Expand Down
9 changes: 5 additions & 4 deletions src/service/V12/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export class V12 extends EventEmitter implements OneBot.Base {
private path: string
wss?: WebSocketServer
wsr: Set<WebSocket> = new Set<WebSocket>()
private db: Database<{ eventBuffer: V12.Payload<keyof Action>[], files: Record<string, V12.FileInfo> }>
private db: Database<{ eventBuffer: V12.Payload<keyof Action>[],msgIdMap:Record<string, number>, files: Record<string, V12.FileInfo> }>

constructor(public oneBot: OneBot<'V12'>, public client: Client, public config: V12.Config) {
super()
this.db = new Database(join(App.configDir, 'data', this.oneBot.uin + '.json'))
this.db.sync({eventBuffer: [], files: {}})
this.db.sync({eventBuffer: [],msgIdMap:{}, files: {}})
this.action = new Action()
this.logger = this.oneBot.app.getLogger(this.oneBot.uin, this.version)
}
Expand Down Expand Up @@ -833,14 +833,15 @@ export namespace V12 {

export function formatPayload<K extends keyof BotEventMap>(uin: number, type: K, data: Omit<BotEventMap[K], K>) {
return {
self_id: uin + '',
self_id: uin+'',
time: Math.floor(Date.now() / 1000),
detail_type: type,
type: 'meta',
sub_type: '',
...data,
group:data['group']?.info,
frind:data['friend']?.info,

friend:data['friend']?.info,
member:data['member']?.info,
}
}
Expand Down

0 comments on commit 65d94ce

Please sign in to comment.