diff --git a/packages/napcat-core/helper/profile-region.ts b/packages/napcat-core/helper/profile-region.ts new file mode 100644 index 00000000..97d37017 --- /dev/null +++ b/packages/napcat-core/helper/profile-region.ts @@ -0,0 +1,24 @@ +/** Self-reported profile region only; never infer a location from other fields. */ +export interface ProfileRegion { + country?: string; + province?: string; + city?: string; +} + +function text (value: unknown): string { + return typeof value === 'string' ? value.trim() : ''; +} + +/** Keep one source together: mixing old/new components can invent a region. */ +export function selectProfileRegion (fresh?: ProfileRegion, fallback?: ProfileRegion): Required { + for (const source of [fresh, fallback]) { + const region = { country: text(source?.country), province: text(source?.province), city: text(source?.city) }; + if (Object.values(region).some(Boolean)) return region; + } + return { country: '', province: '', city: '' }; +} + +export function profileRegionArea (source?: ProfileRegion): string { + const region = selectProfileRegion(source); + return [...new Set(Object.values(region).filter(Boolean))].join(' '); +} diff --git a/packages/napcat-onebot/action/go-cqhttp/GetStrangerInfo.ts b/packages/napcat-onebot/action/go-cqhttp/GetStrangerInfo.ts index 7610917e..c07340af 100644 --- a/packages/napcat-onebot/action/go-cqhttp/GetStrangerInfo.ts +++ b/packages/napcat-onebot/action/go-cqhttp/GetStrangerInfo.ts @@ -1,3 +1,4 @@ +import { selectProfileRegion } from '@/napcat-core/helper/profile-region'; import { OneBotAction } from '@/napcat-onebot/action/OneBotAction'; import { OB11UserSex } from '@/napcat-onebot/index'; import { OB11Construct } from '@/napcat-onebot/helper/data'; @@ -56,6 +57,7 @@ export default class GoCQHTTPGetStrangerInfo extends OneBotAction