Skip to content

Commit c64e65e

Browse files
committed
fix: replace precondition object by name
1 parent 20263e1 commit c64e65e

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

src/define.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
DefineEvent,
1919
DefineEventWithOptions,
2020
DefinePrecondition,
21+
DefinePreconditionWithName,
2122
EmbedOptions,
2223
EmbedSetters,
2324
EventCallback,
@@ -101,10 +102,25 @@ export const defineContextMenu: DefineContextMenu &
101102
}
102103
}
103104

104-
export const definePrecondition: DefinePrecondition = (
105-
callback: PreconditionCallback
105+
export const definePrecondition: DefinePrecondition &
106+
DefinePreconditionWithName = (
107+
...args: [PreconditionCallback | string, PreconditionCallback?]
106108
) => {
107-
return { options: {}, callback }
109+
let name = ''
110+
let callback: PreconditionCallback
111+
112+
if (args.length === 1) {
113+
const [cb] = args as [PreconditionCallback]
114+
115+
callback = cb
116+
} else {
117+
const [nm, cb] = args as [string, PreconditionCallback]
118+
119+
name = nm
120+
callback = cb
121+
}
122+
123+
return { name, callback }
108124
}
109125

110126
export const defineModal = (options: ModalOptions): ModalBuilder => {

src/load.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ export const loadPreconditions = (
3535
preconditions: HarmonixPrecondition[]
3636
) => {
3737
for (const prc of preconditions) {
38-
harmonix.preconditions.set(prc.options.name!, prc)
38+
harmonix.preconditions.set(prc.name!, prc)
3939
}
4040
}

src/resolve.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import type {
1313
HarmonixEvent,
1414
HarmonixEventInput,
1515
HarmonixPrecondition,
16-
HarmonixPreconditionInput,
17-
PreconditionOptions
16+
HarmonixPreconditionInput
1817
} from './types'
1918

2019
export const resolveEvent = (
@@ -96,11 +95,9 @@ export const resolvePrecondition = (
9695
})
9796
const _prcPath = _jiti.resolve(prc)
9897
const precondition = _jiti(_prcPath) as HarmonixPrecondition
99-
const options: PreconditionOptions = {
100-
name: precondition.options.name || filename(_prcPath).split('.')[0]
101-
}
98+
const name = precondition.name || filename(_prcPath).split('.')[0]
10299

103-
return { options, callback: precondition.callback }
100+
return { name, callback: precondition.callback }
104101
} else {
105102
return prc
106103
}

src/types/preconditions.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ export type PreconditionCallback = (
2020
entity: SlashEntity | ContextMenuEntity
2121
) => boolean
2222

23-
export interface PreconditionOptions {
24-
name?: string
25-
}
26-
2723
export type DefinePrecondition = (
2824
callback: PreconditionCallback
2925
) => HarmonixPrecondition
3026

27+
export type DefinePreconditionWithName = (
28+
name: string,
29+
callback: PreconditionCallback
30+
) => HarmonixPrecondition
31+
3132
export type HarmonixPreconditionInput = string | HarmonixPrecondition
3233

3334
export interface HarmonixPrecondition {
34-
options: PreconditionOptions
35+
name: string
3536
callback: PreconditionCallback
3637
}

0 commit comments

Comments
 (0)