feat(shell): uix: implement getUix2()

This commit is contained in:
Il Harper 2024-08-23 13:25:33 +08:00
parent ab26d18813
commit c3b9a4b82b
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
2 changed files with 45 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import { EventEmitter } from 'node:events' import { EventEmitter } from 'node:events'
import { api } from './api'
const uinRegex = /\d+/ const uinRegex = /\d+/
@ -9,11 +10,14 @@ const isUin = (uin: unknown) =>
const isUid = (uid: unknown) => const isUid = (uid: unknown) =>
typeof uid === 'string' && uid.length === 24 && uid.startsWith('u_') typeof uid === 'string' && uid.length === 24 && uid.startsWith('u_')
const isGroup = isUin
export class Uix extends EventEmitter { export class Uix extends EventEmitter {
map: Record<string, string> = {} map: Record<string, string> = {}
isUin = isUin isUin = isUin
isUid = isUid isUid = isUid
isGroup = isGroup
add = (uid: string, uin: string) => { add = (uid: string, uin: string) => {
if (!isUid(uid) || !isUin(uin)) return if (!isUid(uid) || !isUin(uin)) return
@ -23,6 +27,9 @@ export class Uix extends EventEmitter {
this.emit(uid, uin) this.emit(uid, uin)
} }
/**
* @deprecated
*/
getUin = (uid: string) => { getUin = (uid: string) => {
if (!isUid(uid)) return undefined if (!isUid(uid)) return undefined
const uin = this.map[uid] const uin = this.map[uid]
@ -30,12 +37,47 @@ export class Uix extends EventEmitter {
return uin return uin
} }
getUin2 = async (uid: string, group: string | number | undefined) => {
if (!isUid(uid)) return undefined
let uin: string | undefined
if (group) {
if (!isGroup(group)) return undefined
// const groupString = `${group}`
try {
uin = await api['chronocat.internal.uix.uin.get'](uid)
// uin = await api['chronocat.internal.uix.uin.get.group'](uid, groupString)
} catch (e) {
// TODO
}
} else {
try {
uin = await api['chronocat.internal.uix.uin.get'](uid)
} catch (e) {
// TODO
}
}
if (uin) this.add(uid, uin)
return this.getUin(uid)
}
/**
* @deprecated
*/
getUid = (uin: string) => { getUid = (uin: string) => {
if (!isUin(uin)) return undefined if (!isUin(uin)) return undefined
const uid = this.map[uin] const uid = this.map[uin]
if (!isUid(uid)) return undefined if (!isUid(uid)) return undefined
return uid return uid
} }
getUid2 = async (uin: string, _group: string | number | undefined) => {
if (!isUin(uin)) return undefined
// if (group) {
// if (!isGroup(group)) return undefined
// const groupString = `${group}`
// }
return this.getUid(uin)
}
} }
export const uix = new Uix() export const uix = new Uix()

View File

@ -192,6 +192,9 @@ export interface CCInternalMethods {
'chronocat.internal.qface.get': [[string], QFace | undefined] 'chronocat.internal.qface.get': [[string], QFace | undefined]
'chronocat.internal.qface.list': [[], QFace[] | undefined] 'chronocat.internal.qface.list': [[], QFace[] | undefined]
'chronocat.internal.uix.uin.get': [[string], string | undefined]
'chronocat.internal.uix.uin.get.group': [[string, string], string | undefined]
} }
export type Methods = SatoriMethods & CCInternalMethods export type Methods = SatoriMethods & CCInternalMethods