Skip to content

Commit

Permalink
fix(core): check getTargetId arg type
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 16, 2020
1 parent e4eea74 commit 00ab2ce
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/koishi-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export function getSenderName (meta: MessageMeta) {
: meta.sender ? meta.sender.card || meta.sender.nickname : userId
}

export function getTargetId (target: string) {
if (!target) return
export function getTargetId (target: string | number) {
if (typeof target !== 'string' && typeof target !== 'number') return
let qq = +target
if (!qq) {
const capture = /\[CQ:at,qq=(\d+)\]/.exec(target)
const capture = /\[CQ:at,qq=(\d+)\]/.exec(target as any)
if (capture) qq = +capture[1]
}
if (!isInteger(qq)) return
Expand Down
1 change: 1 addition & 0 deletions packages/koishi-core/tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('getTargetId', () => {

test('wrong syntax', () => {
expect(getTargetId('')).toBeFalsy()
expect(getTargetId(true as any)).toBeFalsy()
expect(getTargetId('[CQ:at,qq=]')).toBeFalsy()
expect(getTargetId('foo123')).toBeFalsy()
})
Expand Down

0 comments on commit 00ab2ce

Please sign in to comment.