Skip to content

Commit 8699bac

Browse files
committed
feat: buttons, modals and select menus regroups in components dir
1 parent e7e3868 commit 8699bac

File tree

8 files changed

+73
-49
lines changed

8 files changed

+73
-49
lines changed

src/getters.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type {
2323

2424
export const getButton = (id: string) => {
2525
const harmonix = useHarmonix()
26-
const button = harmonix.buttons.get(id)
26+
const button = harmonix.components.buttons.get(id)
2727

2828
if (!button) return null
2929
const builder = new ButtonBuilder()
@@ -50,7 +50,7 @@ export const getButton = (id: string) => {
5050

5151
export const getModal = (id: string) => {
5252
const harmonix = useHarmonix()
53-
const modal = harmonix.modals.get(id)
53+
const modal = harmonix.components.modals.get(id)
5454

5555
if (!modal) return null
5656
const builder = new ModalBuilder()
@@ -91,7 +91,7 @@ export const getModal = (id: string) => {
9191

9292
export const getSelectMenu = (id: string) => {
9393
const harmonix = useHarmonix()
94-
const selectMenu = harmonix.selectMenus.get(id)
94+
const selectMenu = harmonix.components.selectMenus.get(id)
9595

9696
if (!selectMenu) return null
9797
const { placeholder, type, disabled, minValues, maxValues } =

src/harmonix.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ const initHarmonix = async (
6868
events: new Collection(),
6969
commands: new Collection(),
7070
contextMenus: new Collection(),
71-
buttons: new Collection(),
72-
modals: new Collection(),
73-
selectMenus: new Collection(),
71+
components: {
72+
buttons: new Collection(),
73+
modals: new Collection(),
74+
selectMenus: new Collection()
75+
},
7476
preconditions: new Collection()
7577
}
7678
}
@@ -84,9 +86,9 @@ const watchReload = (
8486
harmonix.options.dirs.commands,
8587
harmonix.options.dirs.events,
8688
harmonix.options.dirs.contextMenus,
87-
harmonix.options.dirs.buttons,
88-
harmonix.options.dirs.modals,
89-
harmonix.options.dirs.selectMenus,
89+
harmonix.options.dirs.components.buttons,
90+
harmonix.options.dirs.components.modals,
91+
harmonix.options.dirs.components.selectMenus,
9092
harmonix.options.dirs.preconditions
9193
].map((file) => resolve(harmonix.options.rootDir, file))
9294
const watcher = watch([...filesToWatch, harmonix.configFile], {
@@ -138,9 +140,9 @@ const clearHarmonix = async (harmonix: Harmonix) => {
138140
harmonix.events.clear()
139141
harmonix.commands.clear()
140142
harmonix.contextMenus.clear()
141-
harmonix.buttons.clear()
142-
harmonix.modals.clear()
143-
harmonix.selectMenus.clear()
143+
harmonix.components.buttons.clear()
144+
harmonix.components.modals.clear()
145+
harmonix.components.selectMenus.clear()
144146
harmonix.preconditions.clear()
145147
}
146148

@@ -181,14 +183,16 @@ const loadHarmonix = async (
181183
...(harmonix.options.contextMenus || []),
182184
...scannedContextMenus
183185
].map((ctm) => resolveContextMenu(ctm, harmonix.options))
184-
const buttons = [...(harmonix.options.buttons || []), ...scannedButtons].map(
185-
(btn) => resolveButton(btn, harmonix.options)
186-
)
187-
const modals = [...(harmonix.options.modals || []), ...scannedModals].map(
188-
(mdl) => resolveModal(mdl, harmonix.options)
189-
)
186+
const buttons = [
187+
...(harmonix.options.components?.buttons || []),
188+
...scannedButtons
189+
].map((btn) => resolveButton(btn, harmonix.options))
190+
const modals = [
191+
...(harmonix.options.components?.modals || []),
192+
...scannedModals
193+
].map((mdl) => resolveModal(mdl, harmonix.options))
190194
const selectMenus = [
191-
...(harmonix.options.selectMenus || []),
195+
...(harmonix.options.components?.selectMenus || []),
192196
...scannedSelectMenus
193197
].map((slm) => resolveSelectMenu(slm, harmonix.options))
194198
const preconditions = [

src/load.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ export const loadContextMenus = (
3939

4040
export const loadButtons = (harmonix: Harmonix, buttons: HarmonixButton[]) => {
4141
for (const btn of buttons) {
42-
harmonix.buttons.set(btn.config.id!, btn)
42+
harmonix.components.buttons.set(btn.config.id!, btn)
4343
}
4444
}
4545

4646
export const loadModals = (harmonix: Harmonix, modals: HarmonixModal[]) => {
4747
for (const mdl of modals) {
48-
harmonix.modals.set(mdl.config.id!, mdl)
48+
harmonix.components.modals.set(mdl.config.id!, mdl)
4949
}
5050
}
5151

@@ -54,7 +54,7 @@ export const loadSelectMenus = (
5454
selectMenus: HarmonixSelectMenu[]
5555
) => {
5656
for (const slm of selectMenus) {
57-
harmonix.selectMenus.set(slm.config.id!, slm)
57+
harmonix.components.selectMenus.set(slm.config.id!, slm)
5858
}
5959
}
6060

src/options.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ export const loadOptions = async (
4646
commands: 'commands',
4747
events: 'events',
4848
contextMenus: 'context-menus',
49-
buttons: 'buttons',
50-
modals: 'modals',
51-
selectMenus: 'select-menus',
49+
components: {
50+
dir: 'components',
51+
buttons: 'buttons',
52+
modals: 'modals',
53+
selectMenus: 'select-menus'
54+
},
5255
preconditions: 'preconditions'
5356
})
5457

src/register.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export const registerContextMenu = (harmonix: Harmonix) => {
108108
export const registerButtons = (harmonix: Harmonix) => {
109109
harmonix.client?.on(Events.InteractionCreate, (interaction) => {
110110
if (!interaction.isButton()) return
111-
const btn = harmonix.buttons.get(interaction.customId)
111+
const btn = harmonix.components.buttons.get(interaction.customId)
112112

113113
if (!btn) return
114114
btn.callback(interaction)
@@ -118,7 +118,7 @@ export const registerButtons = (harmonix: Harmonix) => {
118118
export const registerModals = (harmonix: Harmonix) => {
119119
harmonix.client?.on(Events.InteractionCreate, async (interaction) => {
120120
if (!interaction.isModalSubmit()) return
121-
const mdl = harmonix.modals.get(interaction.customId)
121+
const mdl = harmonix.components.modals.get(interaction.customId)
122122

123123
if (!mdl) return
124124
const inputs = Object.keys(mdl.config.inputs ?? {}).reduce<ParsedInputs>(
@@ -136,7 +136,7 @@ export const registerModals = (harmonix: Harmonix) => {
136136
export const registerSelectMenus = (harmonix: Harmonix) => {
137137
harmonix.client?.on(Events.InteractionCreate, (interaction) => {
138138
if (!interaction.isAnySelectMenu()) return
139-
const slm = harmonix.selectMenus.get(interaction.customId)
139+
const slm = harmonix.components.selectMenus.get(interaction.customId)
140140

141141
if (!slm) return
142142
slm.callback(interaction, interaction.values)

src/scan.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import type { Harmonix } from './types'
55
const GLOB_SCAN_PATTERN = '**/*.{js,ts}'
66

77
export const scanEvents = async (harmonix: Harmonix) => {
8-
const files = await Promise.all([
9-
scanFiles(harmonix, harmonix.options.dirs.events)
10-
]).then((r) => r.flat())
8+
const files = await scanFiles(harmonix, harmonix.options.dirs.events)
119

1210
return files.map((f) => f.fullPath)
1311
}
@@ -25,19 +23,31 @@ export const scanContextMenus = async (harmonix: Harmonix) => {
2523
}
2624

2725
export const scanButtons = async (harmonix: Harmonix) => {
28-
const files = await scanFiles(harmonix, harmonix.options.dirs.buttons)
26+
const buttonsDir = join(
27+
harmonix.options.dirs.components.dir,
28+
harmonix.options.dirs.components.buttons
29+
)
30+
const files = await scanFiles(harmonix, buttonsDir)
2931

3032
return files.map((f) => f.fullPath)
3133
}
3234

3335
export const scanModals = async (harmonix: Harmonix) => {
34-
const files = await scanFiles(harmonix, harmonix.options.dirs.modals)
36+
const modalsDir = join(
37+
harmonix.options.dirs.components.dir,
38+
harmonix.options.dirs.components.modals
39+
)
40+
const files = await scanFiles(harmonix, modalsDir)
3541

3642
return files.map((f) => f.fullPath)
3743
}
3844

3945
export const scanSelectMenus = async (harmonix: Harmonix) => {
40-
const files = await scanFiles(harmonix, harmonix.options.dirs.selectMenus)
46+
const selectMenusDir = join(
47+
harmonix.options.dirs.components.dir,
48+
harmonix.options.dirs.components.selectMenus
49+
)
50+
const files = await scanFiles(harmonix, selectMenusDir)
4151

4252
return files.map((f) => f.fullPath)
4353
}

src/types/harmonix.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ interface HarmonixDirs {
1818
events: string
1919
commands: string
2020
contextMenus: string
21-
buttons: string
22-
modals: string
23-
selectMenus: string
21+
components: {
22+
dir: string
23+
buttons: string
24+
modals: string
25+
selectMenus: string
26+
}
2427
preconditions: string
2528
}
2629

@@ -33,9 +36,11 @@ export interface HarmonixOptions {
3336
events: HarmonixEventInput[]
3437
commands: HarmonixCommandInput[]
3538
contextMenus: HarmonixContextMenuInput[]
36-
buttons: HarmonixButtonInput[]
37-
modals: HarmonixModalInput[]
38-
selectMenus: HarmonixSelectMenuInput[]
39+
components: {
40+
buttons: HarmonixButtonInput[]
41+
modals: HarmonixModalInput[]
42+
selectMenus: HarmonixSelectMenuInput[]
43+
}
3944
preconditions: HarmonixPreconditionInput[]
4045
client: ClientOptions
4146
clientId: string
@@ -58,9 +63,11 @@ export interface Harmonix {
5863
events: Collection<string, HarmonixEvent>
5964
commands: Collection<string, HarmonixCommand>
6065
contextMenus: Collection<string, HarmonixContextMenu>
61-
buttons: Collection<string, HarmonixButton>
62-
modals: Collection<string, HarmonixModal>
63-
selectMenus: Collection<string, HarmonixSelectMenu>
66+
components: {
67+
buttons: Collection<string, HarmonixButton>
68+
modals: Collection<string, HarmonixModal>
69+
selectMenus: Collection<string, HarmonixSelectMenu>
70+
}
6471
preconditions: Collection<string, HarmonixPrecondition>
6572
}
6673

src/uses.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import type { EmbedOptions } from './types'
1919
import { getButton, getModal, getSelectMenu } from './getters'
2020

2121
export const useButtons = () => {
22-
const { buttons } = useHarmonix()
22+
const { components } = useHarmonix()
2323

24-
return buttons.reduce(
24+
return components.buttons.reduce(
2525
(acc, button) => {
2626
if (button.config.id) {
2727
acc[button.config.id] = getButton(button.config.id) ?? undefined
@@ -33,9 +33,9 @@ export const useButtons = () => {
3333
}
3434

3535
export const useModals = () => {
36-
const { modals } = useHarmonix()
36+
const { components } = useHarmonix()
3737

38-
return modals.reduce(
38+
return components.modals.reduce(
3939
(acc, modal) => {
4040
if (modal.config.id) {
4141
acc[modal.config.id] = getModal(modal.config.id) ?? undefined
@@ -47,9 +47,9 @@ export const useModals = () => {
4747
}
4848

4949
export const useSelectMenus = () => {
50-
const { selectMenus } = useHarmonix()
50+
const { components } = useHarmonix()
5151

52-
return selectMenus.reduce(
52+
return components.selectMenus.reduce(
5353
(acc, selectMenu) => {
5454
if (selectMenu.config.id) {
5555
acc[selectMenu.config.id] =

0 commit comments

Comments
 (0)