mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
refactor
This commit is contained in:
@@ -191,7 +191,9 @@ function onLoad() {
|
|||||||
ctx.plugin(SQLiteDriver, {
|
ctx.plugin(SQLiteDriver, {
|
||||||
path: path.join(dbDir, `${selfInfo.uin}.db`)
|
path: path.join(dbDir, `${selfInfo.uin}.db`)
|
||||||
})
|
})
|
||||||
ctx.plugin(Store)
|
ctx.plugin(Store, {
|
||||||
|
msgCacheExpire: config.msgCacheExpire! * 1000
|
||||||
|
})
|
||||||
ctx.start()
|
ctx.start()
|
||||||
ipcMain.on(CHANNEL_SET_CONFIG_CONFIRMED, (event, config: LLOBConfig) => {
|
ipcMain.on(CHANNEL_SET_CONFIG_CONFIRMED, (event, config: LLOBConfig) => {
|
||||||
ctx.parallel('llonebot/config-updated', config)
|
ctx.parallel('llonebot/config-updated', config)
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { Peer } from '@/ntqqapi/types'
|
import { Peer, RawMessage } from '@/ntqqapi/types'
|
||||||
import { createHash } from 'node:crypto'
|
import { createHash } from 'node:crypto'
|
||||||
import { LimitedHashTable } from '@/common/utils/table'
|
import { LimitedHashTable } from '@/common/utils/table'
|
||||||
import { FileCacheV2 } from '@/common/types'
|
import { FileCacheV2 } from '@/common/types'
|
||||||
@@ -24,13 +24,15 @@ interface MsgInfo {
|
|||||||
peer: Peer
|
peer: Peer
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Store extends Service {
|
class Store extends Service {
|
||||||
static inject = ['database', 'model']
|
static inject = ['database', 'model']
|
||||||
private cache: LimitedHashTable<string, number>
|
private cache: LimitedHashTable<string, number>
|
||||||
|
private messages: Map<string, RawMessage>
|
||||||
|
|
||||||
constructor(protected ctx: Context) {
|
constructor(protected ctx: Context, public config: Store.Config) {
|
||||||
super(ctx, 'store', true)
|
super(ctx, 'store', true)
|
||||||
this.cache = new LimitedHashTable(1000)
|
this.cache = new LimitedHashTable(1000)
|
||||||
|
this.messages = new Map()
|
||||||
this.initDatabase()
|
this.initDatabase()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,4 +125,29 @@ export default class Store extends Service {
|
|||||||
getFileCacheById(fileUuid: string) {
|
getFileCacheById(fileUuid: string) {
|
||||||
return this.ctx.database.get('file_v2', { fileUuid })
|
return this.ctx.database.get('file_v2', { fileUuid })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async addMsgCache(msg: RawMessage) {
|
||||||
|
const expire = this.config.msgCacheExpire
|
||||||
|
if (expire === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const id = msg.msgId
|
||||||
|
this.messages.set(id, msg)
|
||||||
|
setTimeout(() => {
|
||||||
|
this.messages.delete(id)
|
||||||
|
}, expire)
|
||||||
|
}
|
||||||
|
|
||||||
|
getMsgCache(msgId: string) {
|
||||||
|
return this.messages.get(msgId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace Store {
|
||||||
|
export interface Config {
|
||||||
|
/** 单位为毫秒 */
|
||||||
|
msgCacheExpire: number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Store
|
||||||
|
@@ -2,7 +2,6 @@ import { unlink } from 'node:fs/promises'
|
|||||||
import { Service, Context } from 'cordis'
|
import { Service, Context } from 'cordis'
|
||||||
import { registerCallHook, registerReceiveHook, ReceiveCmdS } from './hook'
|
import { registerCallHook, registerReceiveHook, ReceiveCmdS } from './hook'
|
||||||
import { Config as LLOBConfig } from '../common/types'
|
import { Config as LLOBConfig } from '../common/types'
|
||||||
import { llonebotError } from '../common/globalVars'
|
|
||||||
import { isNumeric } from '../common/utils/misc'
|
import { isNumeric } from '../common/utils/misc'
|
||||||
import { NTMethod } from './ntcall'
|
import { NTMethod } from './ntcall'
|
||||||
import {
|
import {
|
||||||
@@ -13,7 +12,8 @@ import {
|
|||||||
GroupMember,
|
GroupMember,
|
||||||
CategoryFriend,
|
CategoryFriend,
|
||||||
SimpleInfo,
|
SimpleInfo,
|
||||||
ChatType
|
ChatType,
|
||||||
|
BuddyReqType
|
||||||
} from './types'
|
} from './types'
|
||||||
import { selfInfo } from '../common/globalVars'
|
import { selfInfo } from '../common/globalVars'
|
||||||
import { version } from '../version'
|
import { version } from '../version'
|
||||||
@@ -24,25 +24,26 @@ declare module 'cordis' {
|
|||||||
app: Core
|
app: Core
|
||||||
}
|
}
|
||||||
interface Events {
|
interface Events {
|
||||||
'nt/message-created': (input: RawMessage[]) => void
|
'nt/message-created': (input: RawMessage) => void
|
||||||
'nt/message-deleted': (input: RawMessage) => void
|
'nt/message-deleted': (input: RawMessage) => void
|
||||||
'nt/message-sent': (input: RawMessage) => void
|
'nt/message-sent': (input: RawMessage) => void
|
||||||
'nt/group-notify': (input: GroupNotify[]) => void
|
'nt/group-notify': (input: GroupNotify) => void
|
||||||
'nt/friend-request': (input: FriendRequest[]) => void
|
'nt/friend-request': (input: FriendRequest) => void
|
||||||
'nt/group-member-info-updated': (input: { groupCode: string, members: GroupMember[] }) => void
|
'nt/group-member-info-updated': (input: { groupCode: string, members: GroupMember[] }) => void
|
||||||
'nt/system-message-created': (input: Uint8Array) => void
|
'nt/system-message-created': (input: Uint8Array) => void
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Core extends Service {
|
class Core extends Service {
|
||||||
static inject = ['ntMsgApi', 'ntFriendApi', 'ntGroupApi']
|
static inject = ['ntMsgApi', 'ntFriendApi', 'ntGroupApi', 'store']
|
||||||
|
public startTime = 0
|
||||||
|
|
||||||
constructor(protected ctx: Context, public config: Core.Config) {
|
constructor(protected ctx: Context, public config: Core.Config) {
|
||||||
super(ctx, 'app', true)
|
super(ctx, 'app', true)
|
||||||
}
|
}
|
||||||
|
|
||||||
public start() {
|
public start() {
|
||||||
llonebotError.otherError = ''
|
this.startTime = Date.now()
|
||||||
this.registerListener()
|
this.registerListener()
|
||||||
this.ctx.logger.info(`LLOneBot/${version}`)
|
this.ctx.logger.info(`LLOneBot/${version}`)
|
||||||
this.ctx.on('llonebot/config-updated', input => {
|
this.ctx.on('llonebot/config-updated', input => {
|
||||||
@@ -121,7 +122,7 @@ class Core extends Service {
|
|||||||
this.ctx.ntMsgApi.getMsgHistory(peer, '', 20).then(({ msgList }) => {
|
this.ctx.ntMsgApi.getMsgHistory(peer, '', 20).then(({ msgList }) => {
|
||||||
const lastTempMsg = msgList.at(-1)
|
const lastTempMsg = msgList.at(-1)
|
||||||
if (Date.now() / 1000 - Number(lastTempMsg?.msgTime) < 5) {
|
if (Date.now() / 1000 - Number(lastTempMsg?.msgTime) < 5) {
|
||||||
this.ctx.parallel('nt/message-created', [lastTempMsg!])
|
this.ctx.parallel('nt/message-created', lastTempMsg!)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -161,7 +162,16 @@ class Core extends Service {
|
|||||||
})
|
})
|
||||||
|
|
||||||
registerReceiveHook<{ msgList: RawMessage[] }>([ReceiveCmdS.NEW_MSG, ReceiveCmdS.NEW_ACTIVE_MSG], payload => {
|
registerReceiveHook<{ msgList: RawMessage[] }>([ReceiveCmdS.NEW_MSG, ReceiveCmdS.NEW_ACTIVE_MSG], payload => {
|
||||||
this.ctx.parallel('nt/message-created', payload.msgList)
|
for (const message of payload.msgList) {
|
||||||
|
// 过滤启动之前的消息
|
||||||
|
if (parseInt(message.msgTime) < this.startTime / 1000) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (message.senderUin && message.senderUin !== '0') {
|
||||||
|
this.ctx.store.addMsgCache(message)
|
||||||
|
}
|
||||||
|
this.ctx.parallel('nt/message-created', message)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const sentMsgIds = new Map<string, boolean>()
|
const sentMsgIds = new Map<string, boolean>()
|
||||||
@@ -199,20 +209,28 @@ class Core extends Service {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const list = notifies.filter(v => {
|
for (const notify of notifies) {
|
||||||
const flag = v.group.groupCode + '|' + v.seq + '|' + v.type
|
const flag = notify.group.groupCode + '|' + notify.seq + '|' + notify.type
|
||||||
if (groupNotifyFlags.includes(flag)) {
|
const notifyTime = parseInt(notify.seq) / 1000
|
||||||
return false
|
if (groupNotifyFlags.includes(flag) || notifyTime < this.startTime) {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
groupNotifyFlags.push(flag)
|
groupNotifyFlags.push(flag)
|
||||||
return true
|
this.ctx.parallel('nt/group-notify', notify)
|
||||||
})
|
}
|
||||||
this.ctx.parallel('nt/group-notify', list)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
registerReceiveHook<FriendRequestNotify>(ReceiveCmdS.FRIEND_REQUEST, payload => {
|
registerReceiveHook<FriendRequestNotify>(ReceiveCmdS.FRIEND_REQUEST, payload => {
|
||||||
this.ctx.parallel('nt/friend-request', payload.data.buddyReqs)
|
for (const req of payload.data.buddyReqs) {
|
||||||
|
if (!!req.isInitiator || (req.isDecide && req.reqType !== BuddyReqType.MeInitiatorWaitPeerConfirm)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (+req.reqTime < this.startTime / 1000) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
this.ctx.parallel('nt/friend-request', req)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
invoke('nodeIKernelMsgListener/onRecvSysMsg', [], { registerEvent: true })
|
invoke('nodeIKernelMsgListener/onRecvSysMsg', [], { registerEvent: true })
|
||||||
|
@@ -23,9 +23,7 @@ import { encodeSilk } from '../common/utils/audio'
|
|||||||
import { Context } from 'cordis'
|
import { Context } from 'cordis'
|
||||||
import { isNullable } from 'cosmokit'
|
import { isNullable } from 'cosmokit'
|
||||||
|
|
||||||
//export const mFaceCache = new Map<string, string>() // emojiId -> faceName
|
export namespace SendElement {
|
||||||
|
|
||||||
export namespace SendElementEntities {
|
|
||||||
export function text(content: string): SendTextElement {
|
export function text(content: string): SendTextElement {
|
||||||
return {
|
return {
|
||||||
elementType: ElementType.Text,
|
elementType: ElementType.Text,
|
||||||
|
@@ -56,20 +56,8 @@ export enum GroupRequestOperateTypes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export enum BuddyReqType {
|
export enum BuddyReqType {
|
||||||
KMEINITIATOR,
|
MsgInfo = 12,
|
||||||
KPEERINITIATOR,
|
MeInitiatorWaitPeerConfirm = 13,
|
||||||
KMEAGREED,
|
|
||||||
KMEAGREEDANDADDED,
|
|
||||||
KPEERAGREED,
|
|
||||||
KPEERAGREEDANDADDED,
|
|
||||||
KPEERREFUSED,
|
|
||||||
KMEREFUSED,
|
|
||||||
KMEIGNORED,
|
|
||||||
KMEAGREEANYONE,
|
|
||||||
KMESETQUESTION,
|
|
||||||
KMEAGREEANDADDFAILED,
|
|
||||||
KMSGINFO,
|
|
||||||
KMEINITIATORWAITPEERCONFIRM
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FriendRequest {
|
export interface FriendRequest {
|
||||||
@@ -128,4 +116,4 @@ export interface GroupExtParam {
|
|||||||
memberIcon: number
|
memberIcon: number
|
||||||
memberInfoSeq: number
|
memberInfoSeq: number
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { BaseAction, Schema } from '../BaseAction'
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
import { SendElementEntities } from '@/ntqqapi/entities'
|
import { SendElement } from '@/ntqqapi/entities'
|
||||||
import { uri2local } from '@/common/utils'
|
import { uri2local } from '@/common/utils'
|
||||||
import { sendMsg, createPeer, CreatePeerMode } from '../../helper/createMessage'
|
import { sendMsg, createPeer, CreatePeerMode } from '../../helper/createMessage'
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ export class UploadGroupFile extends BaseAction<Payload, null> {
|
|||||||
if (!success) {
|
if (!success) {
|
||||||
throw new Error(errMsg)
|
throw new Error(errMsg)
|
||||||
}
|
}
|
||||||
const file = await SendElementEntities.file(this.ctx, path, payload.name || fileName, payload.folder ?? payload.folder_id)
|
const file = await SendElement.file(this.ctx, path, payload.name || fileName, payload.folder ?? payload.folder_id)
|
||||||
const peer = await createPeer(this.ctx, payload, CreatePeerMode.Group)
|
const peer = await createPeer(this.ctx, payload, CreatePeerMode.Group)
|
||||||
await sendMsg(this.ctx, peer, [file], [])
|
await sendMsg(this.ctx, peer, [file], [])
|
||||||
return null
|
return null
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { BaseAction, Schema } from '../BaseAction'
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
import { SendElementEntities } from '@/ntqqapi/entities'
|
import { SendElement } from '@/ntqqapi/entities'
|
||||||
import { uri2local } from '@/common/utils'
|
import { uri2local } from '@/common/utils'
|
||||||
import { sendMsg, createPeer, CreatePeerMode } from '../../helper/createMessage'
|
import { sendMsg, createPeer, CreatePeerMode } from '../../helper/createMessage'
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ export class UploadPrivateFile extends BaseAction<UploadPrivateFilePayload, null
|
|||||||
if (!success) {
|
if (!success) {
|
||||||
throw new Error(errMsg)
|
throw new Error(errMsg)
|
||||||
}
|
}
|
||||||
const sendFileEle = await SendElementEntities.file(this.ctx, path, payload.name || fileName)
|
const sendFileEle = await SendElement.file(this.ctx, path, payload.name || fileName)
|
||||||
const peer = await createPeer(this.ctx, payload, CreatePeerMode.Private)
|
const peer = await createPeer(this.ctx, payload, CreatePeerMode.Private)
|
||||||
await sendMsg(this.ctx, peer, [sendFileEle], [])
|
await sendMsg(this.ctx, peer, [sendFileEle], [])
|
||||||
return null
|
return null
|
||||||
|
@@ -25,7 +25,7 @@ class GetMsg extends BaseAction<PayloadType, OB11Message> {
|
|||||||
peerUid: msgInfo.peer.peerUid,
|
peerUid: msgInfo.peer.peerUid,
|
||||||
chatType: msgInfo.peer.chatType
|
chatType: msgInfo.peer.chatType
|
||||||
}
|
}
|
||||||
const msg = this.adapter.getMsgCache(msgInfo.msgId) ?? (await this.ctx.ntMsgApi.getMsgsByMsgId(peer, [msgInfo.msgId])).msgList[0]
|
const msg = this.ctx.store.getMsgCache(msgInfo.msgId) ?? (await this.ctx.ntMsgApi.getMsgsByMsgId(peer, [msgInfo.msgId])).msgList[0]
|
||||||
const retMsg = await OB11Entities.message(this.ctx, msg)
|
const retMsg = await OB11Entities.message(this.ctx, msg)
|
||||||
if (!retMsg) {
|
if (!retMsg) {
|
||||||
throw new Error('消息为空')
|
throw new Error('消息为空')
|
||||||
|
@@ -58,7 +58,7 @@ export enum ActionName {
|
|||||||
GetCookies = 'get_cookies',
|
GetCookies = 'get_cookies',
|
||||||
ForwardFriendSingleMsg = 'forward_friend_single_msg',
|
ForwardFriendSingleMsg = 'forward_friend_single_msg',
|
||||||
ForwardGroupSingleMsg = 'forward_group_single_msg',
|
ForwardGroupSingleMsg = 'forward_group_single_msg',
|
||||||
// 以下为go-cqhttp api
|
// go-cqhttp
|
||||||
GoCQHTTP_SendGroupForwardMsg = 'send_group_forward_msg',
|
GoCQHTTP_SendGroupForwardMsg = 'send_group_forward_msg',
|
||||||
GoCQHTTP_SendPrivateForwardMsg = 'send_private_forward_msg',
|
GoCQHTTP_SendPrivateForwardMsg = 'send_private_forward_msg',
|
||||||
GoCQHTTP_GetStrangerInfo = 'get_stranger_info',
|
GoCQHTTP_GetStrangerInfo = 'get_stranger_info',
|
||||||
|
@@ -37,8 +37,6 @@ declare module 'cordis' {
|
|||||||
class OneBot11Adapter extends Service {
|
class OneBot11Adapter extends Service {
|
||||||
static inject = ['ntMsgApi', 'ntFileApi', 'ntFileCacheApi', 'ntFriendApi', 'ntGroupApi', 'ntUserApi', 'ntWindowApi', 'ntWebApi', 'store']
|
static inject = ['ntMsgApi', 'ntFileApi', 'ntFileCacheApi', 'ntFriendApi', 'ntGroupApi', 'ntUserApi', 'ntWindowApi', 'ntWebApi', 'store']
|
||||||
|
|
||||||
public messages: Map<string, RawMessage> = new Map()
|
|
||||||
public startTime = 0
|
|
||||||
private ob11WebSocket: OB11WebSocket
|
private ob11WebSocket: OB11WebSocket
|
||||||
private ob11WebSocketReverseManager: OB11WebSocketReverseManager
|
private ob11WebSocketReverseManager: OB11WebSocketReverseManager
|
||||||
private ob11Http: OB11Http
|
private ob11Http: OB11Http
|
||||||
@@ -74,24 +72,6 @@ class OneBot11Adapter extends Service {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 缓存近期消息内容 */
|
|
||||||
public async addMsgCache(msg: RawMessage) {
|
|
||||||
const expire = this.config.msgCacheExpire * 1000
|
|
||||||
if (expire === 0) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const id = msg.msgId
|
|
||||||
this.messages.set(id, msg)
|
|
||||||
setTimeout(() => {
|
|
||||||
this.messages.delete(id)
|
|
||||||
}, expire)
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 获取近期消息内容 */
|
|
||||||
public getMsgCache(msgId: string) {
|
|
||||||
return this.messages.get(msgId)
|
|
||||||
}
|
|
||||||
|
|
||||||
public dispatch(event: OB11BaseEvent | OB11Message) {
|
public dispatch(event: OB11BaseEvent | OB11Message) {
|
||||||
if (this.config.enableWs) {
|
if (this.config.enableWs) {
|
||||||
this.ob11WebSocket.emitEvent(event)
|
this.ob11WebSocket.emitEvent(event)
|
||||||
@@ -108,115 +88,99 @@ class OneBot11Adapter extends Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handleGroupNotify(notifies: GroupNotify[]) {
|
private async handleGroupNotify(notify: GroupNotify) {
|
||||||
for (const notify of notifies) {
|
try {
|
||||||
try {
|
const flag = notify.group.groupCode + '|' + notify.seq + '|' + notify.type
|
||||||
const notifyTime = parseInt(notify.seq) / 1000
|
if ([GroupNotifyType.MEMBER_LEAVE_NOTIFY_ADMIN, GroupNotifyType.KICK_MEMBER_NOTIFY_ADMIN].includes(notify.type)) {
|
||||||
if (notifyTime < this.startTime) {
|
this.ctx.logger.info('有成员退出通知', notify)
|
||||||
continue
|
const member1Uin = await this.ctx.ntUserApi.getUinByUid(notify.user1.uid)
|
||||||
}
|
let operatorId = member1Uin
|
||||||
const flag = notify.group.groupCode + '|' + notify.seq + '|' + notify.type
|
let subType: GroupDecreaseSubType = 'leave'
|
||||||
if ([GroupNotifyType.MEMBER_LEAVE_NOTIFY_ADMIN, GroupNotifyType.KICK_MEMBER_NOTIFY_ADMIN].includes(notify.type)) {
|
if (notify.user2.uid) {
|
||||||
this.ctx.logger.info('有成员退出通知', notify)
|
// 是被踢的
|
||||||
const member1Uin = await this.ctx.ntUserApi.getUinByUid(notify.user1.uid)
|
const member2Uin = await this.ctx.ntUserApi.getUinByUid(notify.user2.uid)
|
||||||
let operatorId = member1Uin
|
if (member2Uin) {
|
||||||
let subType: GroupDecreaseSubType = 'leave'
|
operatorId = member2Uin
|
||||||
if (notify.user2.uid) {
|
|
||||||
// 是被踢的
|
|
||||||
const member2Uin = await this.ctx.ntUserApi.getUinByUid(notify.user2.uid)
|
|
||||||
if (member2Uin) {
|
|
||||||
operatorId = member2Uin
|
|
||||||
}
|
|
||||||
subType = 'kick'
|
|
||||||
}
|
}
|
||||||
const event = new OB11GroupDecreaseEvent(
|
subType = 'kick'
|
||||||
parseInt(notify.group.groupCode),
|
|
||||||
parseInt(member1Uin),
|
|
||||||
parseInt(operatorId),
|
|
||||||
subType,
|
|
||||||
)
|
|
||||||
this.dispatch(event)
|
|
||||||
}
|
}
|
||||||
else if (notify.type === GroupNotifyType.REQUEST_JOIN_NEED_ADMINI_STRATOR_PASS && notify.status === GroupNotifyStatus.KUNHANDLE) {
|
const event = new OB11GroupDecreaseEvent(
|
||||||
this.ctx.logger.info('有加群请求')
|
parseInt(notify.group.groupCode),
|
||||||
const requestUin = await this.ctx.ntUserApi.getUinByUid(notify.user1.uid)
|
parseInt(member1Uin),
|
||||||
const event = new OB11GroupRequestEvent(
|
parseInt(operatorId),
|
||||||
parseInt(notify.group.groupCode),
|
subType,
|
||||||
parseInt(requestUin) || 0,
|
)
|
||||||
flag,
|
this.dispatch(event)
|
||||||
notify.postscript,
|
|
||||||
)
|
|
||||||
this.dispatch(event)
|
|
||||||
}
|
|
||||||
else if (notify.type === GroupNotifyType.INVITED_BY_MEMBER && notify.status === GroupNotifyStatus.KUNHANDLE) {
|
|
||||||
this.ctx.logger.info('收到邀请我加群通知')
|
|
||||||
const userId = await this.ctx.ntUserApi.getUinByUid(notify.user2.uid)
|
|
||||||
const event = new OB11GroupRequestEvent(
|
|
||||||
parseInt(notify.group.groupCode),
|
|
||||||
parseInt(userId) || 0,
|
|
||||||
flag,
|
|
||||||
notify.postscript,
|
|
||||||
undefined,
|
|
||||||
'invite'
|
|
||||||
)
|
|
||||||
this.dispatch(event)
|
|
||||||
}
|
|
||||||
else if (notify.type === GroupNotifyType.INVITED_NEED_ADMINI_STRATOR_PASS && notify.status === GroupNotifyStatus.KUNHANDLE) {
|
|
||||||
this.ctx.logger.info('收到群员邀请加群通知')
|
|
||||||
const userId = await this.ctx.ntUserApi.getUinByUid(notify.user1.uid)
|
|
||||||
const event = new OB11GroupRequestEvent(
|
|
||||||
parseInt(notify.group.groupCode),
|
|
||||||
parseInt(userId) || 0,
|
|
||||||
flag,
|
|
||||||
notify.postscript
|
|
||||||
)
|
|
||||||
this.dispatch(event)
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
this.ctx.logger.error('解析群通知失败', (e as Error).stack)
|
|
||||||
}
|
}
|
||||||
|
else if (notify.type === GroupNotifyType.REQUEST_JOIN_NEED_ADMINI_STRATOR_PASS && notify.status === GroupNotifyStatus.KUNHANDLE) {
|
||||||
|
this.ctx.logger.info('有加群请求')
|
||||||
|
const requestUin = await this.ctx.ntUserApi.getUinByUid(notify.user1.uid)
|
||||||
|
const event = new OB11GroupRequestEvent(
|
||||||
|
parseInt(notify.group.groupCode),
|
||||||
|
parseInt(requestUin) || 0,
|
||||||
|
flag,
|
||||||
|
notify.postscript,
|
||||||
|
)
|
||||||
|
this.dispatch(event)
|
||||||
|
}
|
||||||
|
else if (notify.type === GroupNotifyType.INVITED_BY_MEMBER && notify.status === GroupNotifyStatus.KUNHANDLE) {
|
||||||
|
this.ctx.logger.info('收到邀请我加群通知')
|
||||||
|
const userId = await this.ctx.ntUserApi.getUinByUid(notify.user2.uid)
|
||||||
|
const event = new OB11GroupRequestEvent(
|
||||||
|
parseInt(notify.group.groupCode),
|
||||||
|
parseInt(userId) || 0,
|
||||||
|
flag,
|
||||||
|
notify.postscript,
|
||||||
|
undefined,
|
||||||
|
'invite'
|
||||||
|
)
|
||||||
|
this.dispatch(event)
|
||||||
|
}
|
||||||
|
else if (notify.type === GroupNotifyType.INVITED_NEED_ADMINI_STRATOR_PASS && notify.status === GroupNotifyStatus.KUNHANDLE) {
|
||||||
|
this.ctx.logger.info('收到群员邀请加群通知')
|
||||||
|
const userId = await this.ctx.ntUserApi.getUinByUid(notify.user1.uid)
|
||||||
|
const event = new OB11GroupRequestEvent(
|
||||||
|
parseInt(notify.group.groupCode),
|
||||||
|
parseInt(userId) || 0,
|
||||||
|
flag,
|
||||||
|
notify.postscript
|
||||||
|
)
|
||||||
|
this.dispatch(event)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
this.ctx.logger.error('解析群通知失败', (e as Error).stack)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleMsg(msgList: RawMessage[]) {
|
private handleMsg(message: RawMessage) {
|
||||||
for (const message of msgList) {
|
OB11Entities.message(this.ctx, message).then(msg => {
|
||||||
// 过滤启动之前的消息
|
if (!msg) {
|
||||||
if (parseInt(message.msgTime) < this.startTime / 1000) {
|
return
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
this.addMsgCache(message)
|
if (!this.config.debug && msg.message.length === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const isSelfMsg = msg.user_id.toString() === selfInfo.uin
|
||||||
|
if (isSelfMsg && !this.config.reportSelfMessage) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (isSelfMsg) {
|
||||||
|
msg.target_id = parseInt(message.peerUin)
|
||||||
|
}
|
||||||
|
this.dispatch(msg)
|
||||||
|
}).catch(e => this.ctx.logger.error('constructMessage error: ', e.stack.toString()))
|
||||||
|
|
||||||
OB11Entities.message(this.ctx, message)
|
OB11Entities.groupEvent(this.ctx, message).then(groupEvent => {
|
||||||
.then((msg) => {
|
if (groupEvent) {
|
||||||
if (!msg) {
|
this.dispatch(groupEvent)
|
||||||
return
|
}
|
||||||
}
|
})
|
||||||
if (!this.config.debug && msg.message.length === 0) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const isSelfMsg = msg.user_id.toString() === selfInfo.uin
|
|
||||||
if (isSelfMsg && !this.config.reportSelfMessage) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (isSelfMsg) {
|
|
||||||
msg.target_id = parseInt(message.peerUin)
|
|
||||||
}
|
|
||||||
this.dispatch(msg)
|
|
||||||
})
|
|
||||||
.catch((e) => this.ctx.logger.error('constructMessage error: ', e.stack.toString()))
|
|
||||||
|
|
||||||
OB11Entities.groupEvent(this.ctx, message).then((groupEvent) => {
|
OB11Entities.privateEvent(this.ctx, message).then(privateEvent => {
|
||||||
if (groupEvent) {
|
if (privateEvent) {
|
||||||
this.dispatch(groupEvent)
|
this.dispatch(privateEvent)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
OB11Entities.privateEvent(this.ctx, message).then((privateEvent) => {
|
|
||||||
if (privateEvent) {
|
|
||||||
this.dispatch(privateEvent)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleRecallMsg(message: RawMessage) {
|
private handleRecallMsg(message: RawMessage) {
|
||||||
@@ -235,30 +199,22 @@ class OneBot11Adapter extends Service {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handleFriendRequest(buddyReqs: FriendRequest[]) {
|
private async handleFriendRequest(req: FriendRequest) {
|
||||||
for (const req of buddyReqs) {
|
let userId = 0
|
||||||
if (!!req.isInitiator || (req.isDecide && req.reqType !== BuddyReqType.KMEINITIATORWAITPEERCONFIRM)) {
|
try {
|
||||||
continue
|
const requesterUin = await this.ctx.ntUserApi.getUinByUid(req.friendUid)
|
||||||
}
|
userId = parseInt(requesterUin)
|
||||||
if (+req.reqTime < this.startTime / 1000) {
|
} catch (e) {
|
||||||
continue
|
this.ctx.logger.error('获取加好友者QQ号失败', e)
|
||||||
}
|
|
||||||
let userId = 0
|
|
||||||
try {
|
|
||||||
const requesterUin = await this.ctx.ntUserApi.getUinByUid(req.friendUid)
|
|
||||||
userId = parseInt(requesterUin)
|
|
||||||
} catch (e) {
|
|
||||||
this.ctx.logger.error('获取加好友者QQ号失败', e)
|
|
||||||
}
|
|
||||||
const flag = req.friendUid + '|' + req.reqTime
|
|
||||||
const comment = req.extWords
|
|
||||||
const friendRequestEvent = new OB11FriendRequestEvent(
|
|
||||||
userId,
|
|
||||||
comment,
|
|
||||||
flag
|
|
||||||
)
|
|
||||||
this.dispatch(friendRequestEvent)
|
|
||||||
}
|
}
|
||||||
|
const flag = req.friendUid + '|' + req.reqTime
|
||||||
|
const comment = req.extWords
|
||||||
|
const friendRequestEvent = new OB11FriendRequestEvent(
|
||||||
|
userId,
|
||||||
|
comment,
|
||||||
|
flag
|
||||||
|
)
|
||||||
|
this.dispatch(friendRequestEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handleConfigUpdated(config: LLOBConfig) {
|
private async handleConfigUpdated(config: LLOBConfig) {
|
||||||
@@ -374,7 +330,6 @@ class OneBot11Adapter extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public start() {
|
public start() {
|
||||||
this.startTime = Date.now()
|
|
||||||
if (this.config.enableWs) {
|
if (this.config.enableWs) {
|
||||||
this.ob11WebSocket.start()
|
this.ob11WebSocket.start()
|
||||||
}
|
}
|
||||||
@@ -397,7 +352,7 @@ class OneBot11Adapter extends Service {
|
|||||||
this.handleRecallMsg(input)
|
this.handleRecallMsg(input)
|
||||||
})
|
})
|
||||||
this.ctx.on('nt/message-sent', input => {
|
this.ctx.on('nt/message-sent', input => {
|
||||||
this.handleMsg([input])
|
this.handleMsg(input)
|
||||||
})
|
})
|
||||||
this.ctx.on('nt/group-notify', input => {
|
this.ctx.on('nt/group-notify', input => {
|
||||||
this.handleGroupNotify(input)
|
this.handleGroupNotify(input)
|
||||||
|
@@ -338,7 +338,6 @@ export namespace OB11Entities {
|
|||||||
key: marketFaceElement.key
|
key: marketFaceElement.key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//mFaceCache.set(emojiId, element.marketFaceElement.faceName!)
|
|
||||||
}
|
}
|
||||||
else if (element.markdownElement) {
|
else if (element.markdownElement) {
|
||||||
const { markdownElement } = element
|
const { markdownElement } = element
|
||||||
|
@@ -15,7 +15,7 @@ import {
|
|||||||
} from '../types'
|
} from '../types'
|
||||||
import { decodeCQCode } from '../cqcode'
|
import { decodeCQCode } from '../cqcode'
|
||||||
import { Peer } from '@/ntqqapi/types/msg'
|
import { Peer } from '@/ntqqapi/types/msg'
|
||||||
import { SendElementEntities } from '@/ntqqapi/entities'
|
import { SendElement } from '@/ntqqapi/entities'
|
||||||
import { selfInfo } from '@/common/globalVars'
|
import { selfInfo } from '@/common/globalVars'
|
||||||
import { uri2local } from '@/common/utils'
|
import { uri2local } from '@/common/utils'
|
||||||
import { Context } from 'cordis'
|
import { Context } from 'cordis'
|
||||||
@@ -36,7 +36,7 @@ export async function createSendElements(
|
|||||||
case OB11MessageDataType.text: {
|
case OB11MessageDataType.text: {
|
||||||
const text = sendMsg.data?.text
|
const text = sendMsg.data?.text
|
||||||
if (text) {
|
if (text) {
|
||||||
sendElements.push(SendElementEntities.text(sendMsg.data!.text))
|
sendElements.push(SendElement.text(sendMsg.data!.text))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@@ -62,7 +62,7 @@ export async function createSendElements(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isAdmin && remainAtAllCount > 0) {
|
if (isAdmin && remainAtAllCount > 0) {
|
||||||
sendElements.push(SendElementEntities.at(atQQ, atQQ, AtType.All, '@全体成员'))
|
sendElements.push(SendElement.at(atQQ, atQQ, AtType.All, '@全体成员'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (peer.chatType === ChatType.Group) {
|
else if (peer.chatType === ChatType.Group) {
|
||||||
@@ -70,14 +70,14 @@ export async function createSendElements(
|
|||||||
if (atMember) {
|
if (atMember) {
|
||||||
const display = `@${atMember.cardName || atMember.nick}`
|
const display = `@${atMember.cardName || atMember.nick}`
|
||||||
sendElements.push(
|
sendElements.push(
|
||||||
SendElementEntities.at(atQQ, atMember.uid, AtType.One, display),
|
SendElement.at(atQQ, atMember.uid, AtType.One, display),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
const atNmae = sendMsg.data?.name
|
const atNmae = sendMsg.data?.name
|
||||||
const uid = await ctx.ntUserApi.getUidByUin(atQQ) || ''
|
const uid = await ctx.ntUserApi.getUidByUin(atQQ) || ''
|
||||||
const display = atNmae ? `@${atNmae}` : ''
|
const display = atNmae ? `@${atNmae}` : ''
|
||||||
sendElements.push(
|
sendElements.push(
|
||||||
SendElementEntities.at(atQQ, uid, AtType.One, display),
|
SendElement.at(atQQ, uid, AtType.One, display),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -97,7 +97,7 @@ export async function createSendElements(
|
|||||||
)).msgList[0]
|
)).msgList[0]
|
||||||
if (replyMsg) {
|
if (replyMsg) {
|
||||||
sendElements.push(
|
sendElements.push(
|
||||||
SendElementEntities.reply(
|
SendElement.reply(
|
||||||
replyMsg.msgSeq,
|
replyMsg.msgSeq,
|
||||||
replyMsg.msgId,
|
replyMsg.msgId,
|
||||||
replyMsg.senderUin!,
|
replyMsg.senderUin!,
|
||||||
@@ -111,13 +111,13 @@ export async function createSendElements(
|
|||||||
case OB11MessageDataType.face: {
|
case OB11MessageDataType.face: {
|
||||||
const faceId = sendMsg.data?.id
|
const faceId = sendMsg.data?.id
|
||||||
if (faceId) {
|
if (faceId) {
|
||||||
sendElements.push(SendElementEntities.face(parseInt(faceId)))
|
sendElements.push(SendElement.face(parseInt(faceId)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case OB11MessageDataType.mface: {
|
case OB11MessageDataType.mface: {
|
||||||
sendElements.push(
|
sendElements.push(
|
||||||
SendElementEntities.mface(
|
SendElement.mface(
|
||||||
+sendMsg.data.emoji_package_id,
|
+sendMsg.data.emoji_package_id,
|
||||||
sendMsg.data.emoji_id,
|
sendMsg.data.emoji_id,
|
||||||
sendMsg.data.key,
|
sendMsg.data.key,
|
||||||
@@ -127,7 +127,7 @@ export async function createSendElements(
|
|||||||
}
|
}
|
||||||
break
|
break
|
||||||
case OB11MessageDataType.image: {
|
case OB11MessageDataType.image: {
|
||||||
const res = await SendElementEntities.pic(
|
const res = await SendElement.pic(
|
||||||
ctx,
|
ctx,
|
||||||
(await handleOb11FileLikeMessage(ctx, sendMsg, { deleteAfterSentFiles })).path,
|
(await handleOb11FileLikeMessage(ctx, sendMsg, { deleteAfterSentFiles })).path,
|
||||||
sendMsg.data.summary || '',
|
sendMsg.data.summary || '',
|
||||||
@@ -140,7 +140,7 @@ export async function createSendElements(
|
|||||||
break
|
break
|
||||||
case OB11MessageDataType.file: {
|
case OB11MessageDataType.file: {
|
||||||
const { path, fileName } = await handleOb11FileLikeMessage(ctx, sendMsg, { deleteAfterSentFiles })
|
const { path, fileName } = await handleOb11FileLikeMessage(ctx, sendMsg, { deleteAfterSentFiles })
|
||||||
sendElements.push(await SendElementEntities.file(ctx, path, fileName))
|
sendElements.push(await SendElement.file(ctx, path, fileName))
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case OB11MessageDataType.video: {
|
case OB11MessageDataType.video: {
|
||||||
@@ -150,38 +150,38 @@ export async function createSendElements(
|
|||||||
const uri2LocalRes = await uri2local(thumb)
|
const uri2LocalRes = await uri2local(thumb)
|
||||||
if (uri2LocalRes.success) thumb = uri2LocalRes.path
|
if (uri2LocalRes.success) thumb = uri2LocalRes.path
|
||||||
}
|
}
|
||||||
const res = await SendElementEntities.video(ctx, path, fileName, thumb)
|
const res = await SendElement.video(ctx, path, fileName, thumb)
|
||||||
deleteAfterSentFiles.push(res.videoElement.filePath)
|
deleteAfterSentFiles.push(res.videoElement.filePath)
|
||||||
sendElements.push(res)
|
sendElements.push(res)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case OB11MessageDataType.voice: {
|
case OB11MessageDataType.voice: {
|
||||||
const { path } = await handleOb11FileLikeMessage(ctx, sendMsg, { deleteAfterSentFiles })
|
const { path } = await handleOb11FileLikeMessage(ctx, sendMsg, { deleteAfterSentFiles })
|
||||||
sendElements.push(await SendElementEntities.ptt(ctx, path))
|
sendElements.push(await SendElement.ptt(ctx, path))
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case OB11MessageDataType.json: {
|
case OB11MessageDataType.json: {
|
||||||
sendElements.push(SendElementEntities.ark(sendMsg.data.data))
|
sendElements.push(SendElement.ark(sendMsg.data.data))
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case OB11MessageDataType.dice: {
|
case OB11MessageDataType.dice: {
|
||||||
const resultId = sendMsg.data?.result
|
const resultId = sendMsg.data?.result
|
||||||
sendElements.push(SendElementEntities.dice(resultId))
|
sendElements.push(SendElement.dice(resultId))
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case OB11MessageDataType.RPS: {
|
case OB11MessageDataType.RPS: {
|
||||||
const resultId = sendMsg.data?.result
|
const resultId = sendMsg.data?.result
|
||||||
sendElements.push(SendElementEntities.rps(resultId))
|
sendElements.push(SendElement.rps(resultId))
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case OB11MessageDataType.contact: {
|
case OB11MessageDataType.contact: {
|
||||||
const { type, id } = sendMsg.data
|
const { type, id } = sendMsg.data
|
||||||
const data = type === 'qq' ? ctx.ntFriendApi.getBuddyRecommendContact(id) : ctx.ntGroupApi.getGroupRecommendContact(id)
|
const data = type === 'qq' ? ctx.ntFriendApi.getBuddyRecommendContact(id) : ctx.ntGroupApi.getGroupRecommendContact(id)
|
||||||
sendElements.push(SendElementEntities.ark(await data))
|
sendElements.push(SendElement.ark(await data))
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case OB11MessageDataType.shake: {
|
case OB11MessageDataType.shake: {
|
||||||
sendElements.push(SendElementEntities.shake())
|
sendElements.push(SendElement.shake())
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user