diff --git a/src/common/config.ts b/src/common/config.ts index 397c77c..16c854b 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -44,6 +44,7 @@ export class ConfigUtil { reportSelfMessage: false, autoDeleteFile: false, autoDeleteFileSecond: 60, + enablePoke: false }; if (!fs.existsSync(this.configPath)) { diff --git a/src/common/db.ts b/src/common/db.ts index de84003..3da7967 100644 --- a/src/common/db.ts +++ b/src/common/db.ts @@ -4,6 +4,7 @@ import {DATA_DIR, log} from "./utils"; import {selfInfo} from "./data"; import {FileCache} from "./types"; +type ReceiveTempUinMap = Record; class DBUtil { public readonly DB_KEY_PREFIX_MSG_ID = "msg_id_"; @@ -11,8 +12,9 @@ class DBUtil { public readonly DB_KEY_PREFIX_MSG_SEQ_ID = "msg_seq_id_"; public readonly DB_KEY_PREFIX_FILE = "file_"; public readonly DB_KEY_PREFIX_GROUP_NOTIFY = "group_notify_"; + private readonly DB_KEY_RECEIVED_TEMP_UIN_MAP = "received_temp_uin_map"; public db: Level; - public cache: Record = {} // : RawMessage + public cache: Record = {} // : RawMessage private currentShortId: number; /* @@ -67,6 +69,17 @@ class DBUtil { }, expiredMilliSecond) } + public async getReceivedTempUinMap(): Promise { + try{ + this.cache[this.DB_KEY_RECEIVED_TEMP_UIN_MAP] = JSON.parse(await this.db.get(this.DB_KEY_RECEIVED_TEMP_UIN_MAP)); + }catch (e) { + } + return (this.cache[this.DB_KEY_RECEIVED_TEMP_UIN_MAP] || {}) as ReceiveTempUinMap; + } + public setReceivedTempUinMap(data: ReceiveTempUinMap) { + this.cache[this.DB_KEY_RECEIVED_TEMP_UIN_MAP] = data; + this.db.put(this.DB_KEY_RECEIVED_TEMP_UIN_MAP, JSON.stringify(data)).then(); + } private addCache(msg: RawMessage) { const longIdKey = this.DB_KEY_PREFIX_MSG_ID + msg.msgId const shortIdKey = this.DB_KEY_PREFIX_MSG_SHORT_ID + msg.msgShortId diff --git a/src/common/types.ts b/src/common/types.ts index 98e495c..060a517 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -24,6 +24,7 @@ export interface Config { autoDeleteFile?: boolean autoDeleteFileSecond?: number ffmpeg?: string // ffmpeg路径 + enablePoke?: boolean } export interface LLOneBotError { diff --git a/src/main/main.ts b/src/main/main.ts index 406df68..c6f9b84 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -21,7 +21,7 @@ import { getGroupMember, llonebotError, refreshGroupMembers, - selfInfo + selfInfo, uidMaps } from "../common/data"; import {hookNTQQApiCall, hookNTQQApiReceive, ReceiveCmdS, registerReceiveHook} from "../ntqqapi/hook"; import {OB11Constructor} from "../onebot11/constructor"; @@ -115,11 +115,18 @@ function onLoad() { OB11Constructor.message(message).then((msg) => { if (debug) { msg.raw = message; + } else { + if (msg.message.length === 0) { + return + } } const isSelfMsg = msg.user_id.toString() == selfInfo.uin if (isSelfMsg && !reportSelfMessage) { return } + if (isSelfMsg) { + msg.target_id = parseInt(message.peerUin); + } postOB11Event(msg); // log("post msg", msg) }).catch(e => log("constructMessage error: ", e.stack.toString())); @@ -133,17 +140,21 @@ function onLoad() { } async function startReceiveHook() { - registerPokeHandler((id, isGroup) => { - log(`收到戳一戳消息了!是否群聊:${isGroup},id:${id}`) - let pokeEvent: OB11FriendPokeEvent | OB11GroupPokeEvent; - if (isGroup) { - pokeEvent = new OB11GroupPokeEvent(parseInt(id)); - }else{ - pokeEvent = new OB11FriendPokeEvent(parseInt(id)); - } - postOB11Event(pokeEvent); - }) - registerReceiveHook<{ msgList: Array }>([ReceiveCmdS.NEW_MSG, ReceiveCmdS.NEW_ACTIVE_MSG], async (payload) => { + if (getConfigUtil().getConfig().enablePoke) { + registerPokeHandler((id, isGroup) => { + log(`收到戳一戳消息了!是否群聊:${isGroup},id:${id}`) + let pokeEvent: OB11FriendPokeEvent | OB11GroupPokeEvent; + if (isGroup) { + pokeEvent = new OB11GroupPokeEvent(parseInt(id)); + } else { + pokeEvent = new OB11FriendPokeEvent(parseInt(id)); + } + postOB11Event(pokeEvent); + }) + } + registerReceiveHook<{ + msgList: Array + }>([ReceiveCmdS.NEW_MSG, ReceiveCmdS.NEW_ACTIVE_MSG], async (payload) => { try { await postReceiveMsg(payload.msgList); } catch (e) { @@ -316,7 +327,13 @@ function onLoad() { async function start() { log("llonebot pid", process.pid) + startTime = Date.now(); + dbUtil.getReceivedTempUinMap().then(m=>{ + for (const [key, value] of Object.entries(m)) { + uidMaps[value] = key; + } + }) startReceiveHook().then(); NTQQGroupApi.getGroups(true).then() const config = getConfigUtil().getConfig() diff --git a/src/ntqqapi/hook.ts b/src/ntqqapi/hook.ts index f2045f0..574adea 100644 --- a/src/ntqqapi/hook.ts +++ b/src/ntqqapi/hook.ts @@ -3,7 +3,7 @@ import {getConfigUtil, log, sleep} from "../common/utils"; import {NTQQApiClass} from "./ntcall"; import {NTQQMsgApi, sendMessagePool} from "./api/msg" import {ChatType, Group, RawMessage, User} from "./types"; -import {friends, groups, selfInfo, tempGroupCodeMap} from "../common/data"; +import {friends, groups, receivedTempUinMap, selfInfo, tempGroupCodeMap, uidMaps} from "../common/data"; import {OB11GroupDecreaseEvent} from "../onebot11/event/notice/OB11GroupDecreaseEvent"; import {v4 as uuidv4} from "uuid" import {postOB11Event} from "../onebot11/server/postOB11Event"; @@ -249,8 +249,26 @@ registerReceiveHook<{ } }) -// 新消息 registerReceiveHook<{ msgList: Array }>([ReceiveCmdS.NEW_MSG, ReceiveCmdS.NEW_ACTIVE_MSG], (payload) => { + // 保存一下uid + for (const message of payload.msgList) { + const uid = message.senderUid; + const uin = message.senderUin; + if (uid && uin) { + if (message.chatType === ChatType.temp){ + dbUtil.getReceivedTempUinMap().then(receivedTempUinMap=>{ + if (!receivedTempUinMap[uin]){ + receivedTempUinMap[uin] = uid; + dbUtil.setReceivedTempUinMap(receivedTempUinMap) + } + }) + } + uidMaps[uid] = uin; + } + } + + + // 自动清理新消息文件 const {autoDeleteFile} = getConfigUtil().getConfig(); if (!autoDeleteFile) { return diff --git a/src/onebot11/action/SendMsg.ts b/src/onebot11/action/SendMsg.ts index 5375d21..52145f3 100644 --- a/src/onebot11/action/SendMsg.ts +++ b/src/onebot11/action/SendMsg.ts @@ -7,7 +7,14 @@ import { SendArkElement, SendMessageElement } from "../../ntqqapi/types"; -import {friends, getFriend, getGroup, getGroupMember, getUidByUin, selfInfo,} from "../../common/data"; +import { + friends, + getFriend, + getGroup, + getGroupMember, + getUidByUin, + selfInfo, +} from "../../common/data"; import { OB11MessageCustomMusic, OB11MessageData, @@ -87,7 +94,7 @@ export class SendMsg extends BaseAction { } if (payload.user_id && payload.message_type !== "group") { if (!(await getFriend(payload.user_id))) { - if (!ALLOW_SEND_TEMP_MSG) { + if (!ALLOW_SEND_TEMP_MSG && !(await dbUtil.getReceivedTempUinMap())[payload.user_id.toString()]) { return { valid: false, message: `不能发送临时消息` diff --git a/src/onebot11/constructor.ts b/src/onebot11/constructor.ts index 8aff3b0..760381f 100644 --- a/src/onebot11/constructor.ts +++ b/src/onebot11/constructor.ts @@ -344,7 +344,8 @@ export class OB11Constructor { sex: OB11Constructor.sex(member.sex), age: 0, area: "", - level: member.qqLevel && calcQQLevel(member.qqLevel) || 0, + level: 0, + qq_level: member.qqLevel && calcQQLevel(member.qqLevel) || 0, join_time: 0, // 暂时没法获取 last_sent_time: 0, // 暂时没法获取 title_expire_time: 0, diff --git a/src/onebot11/types.ts b/src/onebot11/types.ts index 581c87a..b8db420 100644 --- a/src/onebot11/types.ts +++ b/src/onebot11/types.ts @@ -29,6 +29,7 @@ export interface OB11GroupMember { join_time?: number last_sent_time?: number level?: number + qq_level?: number role?: OB11GroupMemberRole title?: string area?: string @@ -64,6 +65,7 @@ export enum OB11MessageType { } export interface OB11Message { + target_id?: number; // 自己发送的消息才有此字段 self_id?: number, time: number, message_id: number, diff --git a/src/renderer/index.ts b/src/renderer/index.ts index 9399099..c16f12b 100644 --- a/src/renderer/index.ts +++ b/src/renderer/index.ts @@ -99,6 +99,11 @@ async function onSettingWindowCreated(view: Element) { ) ]), SettingList([ + SettingItem( + '接收戳一戳消息, 暂时只支持Windows版的LLOneBot', + `重启QQ后生效,如果导致QQ崩溃请勿开启此项`, + SettingSwitch('enablePoke', config.enablePoke), + ), SettingItem( '使用 Base64 编码获取文件', '开启后,调用 /get_image、/get_record 时,获取不到 url 时添加一个 Base64 字段', diff --git a/src/version.ts b/src/version.ts index 93bafb5..42fc3ff 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export const version = "3.16.0" \ No newline at end of file +export const version = "3.16.1" \ No newline at end of file