Skip to content

Commit

Permalink
feat: 群成员信息支持使用QQ号获取
Browse files Browse the repository at this point in the history
  • Loading branch information
msojocs committed Apr 24, 2024
1 parent 6baf83d commit 022466b
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- refactor: 重构websocket的action处理
- feat: 基础信息获取,好友/群/群成员
- feat: at消息解析
- feat: uid <-> uin
- feat: 群成员信息支持使用QQ号获取

## v1.1.1 / 2024-02-20

Expand Down
56 changes: 28 additions & 28 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,34 @@ const options: RollupOptions[] = [
typescript(),
json(),
// 压缩
terser(),
// 混淆
obfuscator({
global: true,
}),
{
name: 'jsc',
writeBundle: (options, bundle) => {
console.log('generate jsc...')
if (bundle['core.js']) {
const core = bundle['core.js']
// console.log(options)
if (core.type === 'chunk' && options.dir) {
const fromPath = path.resolve(options.dir, './core.js')
console.log('compile file:', fromPath)
// 字节码生成
bytenode.compileFile({
filename: fromPath,
compileAsModule: true,
electron: true,
compress: true,
electronPath: process.env['PROGRAM_PATH'],
output: `${fromPath}c`,
})
}
}
}
},
// terser(),
// // 混淆
// obfuscator({
// global: true,
// }),
// {
// name: 'jsc',
// writeBundle: (options, bundle) => {
// console.log('generate jsc...')
// if (bundle['core.js']) {
// const core = bundle['core.js']
// // console.log(options)
// if (core.type === 'chunk' && options.dir) {
// const fromPath = path.resolve(options.dir, './core.js')
// console.log('compile file:', fromPath)
// // 字节码生成
// bytenode.compileFile({
// filename: fromPath,
// compileAsModule: true,
// electron: true,
// compress: true,
// electronPath: process.env['PROGRAM_PATH'],
// output: `${fromPath}c`,
// })
// }
// }
// }
// },
],
// 指出哪些模块应该视为外部模块
external: ['electron', 'module', 'ntwrapper', 'telecord-native'],
Expand Down
23 changes: 23 additions & 0 deletions src/ntqq/types/wrapper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,28 @@ declare namespace NTNativeWrapper {
constructor(options: NodeIKernelSessionListenerConstructorOptions)
}

// #region UixConvertService
class NodeIKernelUixConvertService extends NTBaseClass {
/**
* 根据QQ号获取用户id
*
* 已测试
* @param uin QQ号
*/
getUid(uin: `${number}`[]): Promise<UidInfo>
/**
* 根据用户id获取QQ号
*
* 未测试
* @param uin QQ号
*/
getUin(uid: `u_${string}`[]): Promise<Map<`u_${string}`, `${number}`>>
}
interface UidInfo {
uidInfo: Map<`${number}`, `u_${string}`>
}
// #endregion

interface NodeIKernelUnitedConfigListenerConstructorOptions {
onUnitedConfigUpdate: () => void
}
Expand Down Expand Up @@ -835,6 +857,7 @@ declare namespace NTNativeWrapper {
getSettingService(): NodeIKernelSettingService
getSkinService(): NodeIKernelSkinService
getStorageCleanService(): NodeIKernelStorageCleanService
getUixConvertService(): NodeIKernelUixConvertService
getUnitedConfigService(): NodeIKernelUnitedConfigService
offLine(info: NodeIQQNTWrapperSessionType.OfflineReq): Promise<SimpleResult>
}
Expand Down
1 change: 1 addition & 0 deletions src/onebot/actions/friend/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface GroupInfoResp {
*/
export interface GroupMemberInfoReq extends BotActionParams {
group_id: number
user_uin: number
user_uid: `u_${string}`
}

Expand Down
20 changes: 18 additions & 2 deletions src/onebot/actions/group/member.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useLogger } from "../../../common/log"
import { CustomError } from "../../../server/error/custom-error"
import { useStore } from "../../../store/store"
import { getGroupMemberInfoById } from "../../common/group"
import { getGroupMemberInfoByUid, getGroupMemberInfoByUin } from "../../common/group"
import { GroupMemberDetailInfoType } from "../../common/interfaces"
import { getUserInfoByUid } from "../../common/user"
import { GroupMemberInfoReq, GroupMemberInfoResp, UserInfoReq, UserInfoResp } from "../friend/interfaces"

Expand All @@ -18,7 +20,21 @@ const getGroupMemberInfo = async (p: GroupMemberInfoReq): Promise<GroupMemberInf
card_name: "",
avatar_url: ''
}
const ret = await getGroupMemberInfoById(p.group_id, p.user_uid)
// 检查uid是否可用
let ret: GroupMemberDetailInfoType | undefined = undefined
if (p.user_uid === undefined || p.user_uid === null || p.user_uid.length === 0) {
if (p.user_uin <= 0) {
throw new CustomError(1, 'uid or uin is error!');
}
ret = await getGroupMemberInfoByUin(p.group_id, p.user_uin)
}
else
{
ret = await getGroupMemberInfoByUid(p.group_id, p.user_uid)
}
if (ret === undefined) {
throw new CustomError(1, 'Failed to get member info!');
}
log.info('getGroupMemberInfoById:', ret)
resp.user_uid = ret.uid
resp.user_uin = parseInt(ret.uin)
Expand Down
34 changes: 27 additions & 7 deletions src/onebot/common/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,38 @@ export const getGroupInfoById = (gid: number): Promise<GroupDetailInfoType> => {

/**
*
* @param gid NodeIKernelGroupService[undefined]/getMemberInfo/eca95e2a-f434-436e-a8da-cf2fcbb3bcbf
arguments: 933286835 [ 'u_EbxBsO-JLi3oxEYabJ0umg' ] true
JSON arguments: ["933286835",["u_EbxBsO-JLi3oxEYabJ0umg"],true]
* @param gid 群号
* @param uid 用户id
* @returns
*/
export const getGroupMemberInfoById = (gid: number, uid: `u_${string}`): Promise<GroupMemberDetailInfoType> => {
export const getGroupMemberInfoByUin = async (gid: number, uin: number): Promise<GroupMemberDetailInfoType> => {
log.info('getGroupMemberInfoByUin', gid, uin)
const { getWrapperSession } = useNTCore()
const session = getWrapperSession()
const convert = session.getUixConvertService()
const info = await convert.getUid([`${uin}`])
log.info('get uid:', info)
const uid = info?.uidInfo?.get(`${uin}`)
if (!uid) {
throw new CustomError(1, 'Failed to get uid by uin!')
}
return await getGroupMemberInfoByUid(gid, uid)
}
/**
*
* @param gid 群号
* @param uid 用户id
* @returns
*/
export const getGroupMemberInfoByUid = (gid: number, uid: `u_${string}`): Promise<GroupMemberDetailInfoType> => {
log.info('getGroupMemberInfoByUid', gid, uid)
return new Promise(async (resolve, reject) => {
let remover: { remove: () => void } | null = null
// 超时拒绝
let time = setTimeout(() => {
if (remover)
remover?.remove()
reject('getGroupMemberInfoById timeout')
reject('getGroupMemberInfoByUid timeout')
}, 30000)
remover = registerEventListener(`KernelGroupListener/onMemberInfoChange`, 'always', (groupId: `${number}`, b: number, payload: Map<`u_${string}`, GroupMemberDetailInfoType>) => {
if (groupId !== `${gid}`) return
Expand All @@ -57,14 +76,15 @@ export const getGroupMemberInfoById = (gid: number, uid: `u_${string}`): Promise
clearTimeout(time)
const result = payload.get(uid)
if (!result) {
throw new CustomError(1, 'failed to get member info!')
log.error('info:', result, 'payload:', payload)
throw new CustomError(1, 'Failed to get member info!')
}
resolve(result)
})
const { getWrapperSession } = useNTCore()
const session = getWrapperSession()
const service = session.getGroupService()
const result = await service.getMemberInfo(`${gid}`, [uid], true)
log.info(`getGroupMemberInfoById[${gid}]:`, result)
log.info(`getGroupMemberInfoByUid[${gid}]:`, result)
})
}
4 changes: 4 additions & 0 deletions src/onebot/common/message.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ declare namespace BotMessageReceiveElements {
*
*/
uid: `u_${string}` | 'all'
/**
* QQ号
*/
uin: number
name?: string
}

Expand Down
10 changes: 10 additions & 0 deletions src/onebot/event/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useStore } from "../../store/store"
import { getBotAccount, getUserInfoByUid } from "../common/user"
import { EventDataType, MessageData, RecallMessageData } from "./interfaces"
import { useLogger } from "../../common/log"
import { getGroupMemberInfoByUid } from "../common/group"

const { registerEventListener } = useStore()
const { sendMessage } = useServer()
Expand Down Expand Up @@ -66,6 +67,15 @@ const onRecvMsg = () => {
break
}
ret.data.elements = convertNTMessage2BotMessage(msg.elements)
for (const ele of ret.data.elements) {
if (ele.type === 'mention') {
if(false === ele.data.at?.isAll) {
// at特定成员,获取QQ号
const info = await getGroupMemberInfoByUid(ret.data.group_id, ele.data.at.uid as `u_${string}`)
ele.data.at.uin = parseInt(info.uin)
}
}
}
ret.data.records = msg.records.map(e => ({
message_id: e.msgId,
message_seq: e.msgSeq,
Expand Down
3 changes: 3 additions & 0 deletions src/transfer/message/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getRichMediaFilePathForGuild } from "../../ntqq/common/nt-api";
import { copyFile, getFileType } from "../../ntqq/common/fs-api";
import { useNTCore } from "../../ntqq/core/core";
import { useStore } from "../../store/store";
import { getGroupMemberInfoByUid } from "../../onebot/common/group";

const log = useLogger('Convert')

Expand All @@ -34,6 +35,7 @@ export const convertNTMessage2BotMessage = (elems: NTReceiveMessageType.NTMessag
at: {
isAll: false,
uid: cur.atNtUid as `u_${string}`,
uin: parseInt(cur.atTinyId),
name: cur.content,
}
}
Expand All @@ -48,6 +50,7 @@ export const convertNTMessage2BotMessage = (elems: NTReceiveMessageType.NTMessag
at: {
isAll: true,
uid: 'all',
uin: 0,
name: cur.content,
}
}
Expand Down
2 changes: 2 additions & 0 deletions tools/dev.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const ActionHandle = {
...process.env,
YUKIHANA_LOG: true,
ELECTRON_RUN_AS_NODE: true,
YUKIHANA_ACTION: 'dev',
YUKIHANA_NATIVE: "D:/GitHub/Telecord-native/build/nt_native.node",
}
})
Expand All @@ -161,6 +162,7 @@ const ActionHandle = {
...process.env,
YUKIHANA_LOG: true,
ELECTRON_RUN_AS_NODE: true,
YUKIHANA_ACTION: 'dev',
YUKIHANA_NATIVE: "/home/msojocs/github/nt-native/build/nt_native.node",
}
})
Expand Down

0 comments on commit 022466b

Please sign in to comment.