Skip to content

Commit 1339153

Browse files
committed
feat: add useHamronix function to access to harmonix instance
1 parent cbbbcdd commit 1339153

File tree

9 files changed

+199
-94
lines changed

9 files changed

+199
-94
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"dotenv": "^16.4.5",
2929
"globby": "^14.0.1",
3030
"jiti": "^1.21.0",
31-
"pathe": "^1.1.2"
31+
"pathe": "^1.1.2",
32+
"unctx": "^2.3.1"
3233
},
3334
"devDependencies": {
3435
"@types/node": "^20.12.8",

pnpm-lock.yaml

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/discord.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { Client, REST, Routes } from 'discord.js'
22
import consola from 'consola'
3-
import type {
4-
ArgsDef,
5-
Harmonix,
6-
HarmonixCommand,
7-
HarmonixContextMenu
8-
} from './types'
3+
import type { Harmonix } from './types'
94
import 'dotenv/config'
105
import { createError } from './harmonix'
116
import { contextMenuToJSON, isHarmonixCommand, slashToJSON } from './utils'
@@ -18,10 +13,11 @@ export const initCient = (harmonixOptions: Harmonix['options']) => {
1813
return client
1914
}
2015

21-
export const refreshApplicationCommands = async (
22-
harmonix: Harmonix,
23-
commands: (HarmonixCommand<true, ArgsDef> | HarmonixContextMenu)[]
24-
) => {
16+
export const refreshApplicationCommands = async (harmonix: Harmonix) => {
17+
const commands = [
18+
...harmonix.commands.filter((cmd) => cmd.options.slash).map((cmd) => cmd),
19+
...harmonix.contextMenus.map((cmd) => cmd)
20+
]
2521
const rest = new REST().setToken(process.env.HARMONIX_CLIENT_TOKEN!)
2622

2723
try {

src/harmonix.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { Collection } from 'discord.js'
12
import type { LoadConfigOptions } from 'c12'
3+
import { getContext } from 'unctx'
24
import consola from 'consola'
35
import { colors } from 'consola/utils'
46
import { loadOptions } from './options'
@@ -8,12 +10,17 @@ import {
810
scanEvents,
911
scanPreconditions
1012
} from './scan'
13+
import {
14+
loadCommands,
15+
loadContextMenus,
16+
loadEvents,
17+
loadPreconditions
18+
} from './load'
1119
import { initCient, refreshApplicationCommands } from './discord'
1220
import {
1321
registerMessageCommands,
1422
registerContextMenu,
1523
registerEvents,
16-
registerPreconditions,
1724
registerSlashCommands
1825
} from './register'
1926
import {
@@ -25,15 +32,21 @@ import {
2532
import type { Harmonix, HarmonixConfig, HarmonixOptions } from './types'
2633
import { version } from '../package.json'
2734

35+
export const ctx = getContext<Harmonix>('harmonix')
36+
37+
export const useHarmonix = ctx.use
38+
2839
export const createHarmonix = async (
2940
config: HarmonixConfig = {},
3041
opts: LoadConfigOptions = {}
3142
) => {
3243
const options = await loadOptions(config, opts)
3344
const harmonix: Harmonix = {
3445
options: options as HarmonixOptions,
35-
preconditions: new Map(),
36-
commands: new Map()
46+
events: new Collection(),
47+
commands: new Collection(),
48+
contextMenus: new Collection(),
49+
preconditions: new Collection()
3750
}
3851

3952
const scannedCommands = await scanCommands(harmonix)
@@ -75,22 +88,19 @@ export const createHarmonix = async (
7588
)
7689
}
7790
consola.log(colors.blue(`Harmonix ${colors.bold(version)}\n`))
91+
92+
loadEvents(harmonix, events)
93+
loadCommands(harmonix, commands)
94+
loadContextMenus(harmonix, contextMenus)
95+
loadPreconditions(harmonix, preconditions)
96+
7897
harmonix.client = initCient(harmonix.options)
79-
registerEvents(harmonix, events)
80-
registerPreconditions(harmonix, preconditions)
81-
await refreshApplicationCommands(harmonix, [
82-
...commands.filter((cmd) => cmd.options.slash),
83-
...contextMenus
84-
])
85-
registerMessageCommands(
86-
harmonix,
87-
commands.filter((cmd) => !cmd.options.slash)
88-
)
89-
registerSlashCommands(
90-
harmonix,
91-
commands.filter((cmd) => cmd.options.slash)
92-
)
93-
registerContextMenu(harmonix, contextMenus)
98+
99+
registerEvents(harmonix)
100+
await refreshApplicationCommands(harmonix)
101+
registerMessageCommands(harmonix)
102+
registerSlashCommands(harmonix)
103+
registerContextMenu(harmonix)
94104

95105
return harmonix
96106
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export * from './define'
2-
export * from './harmonix'
2+
export { createHarmonix, createError, useHarmonix } from './harmonix'

src/load.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type {
2+
Harmonix,
3+
HarmonixCommand,
4+
HarmonixContextMenu,
5+
HarmonixEvent,
6+
HarmonixPrecondition
7+
} from './types'
8+
9+
export const loadEvents = (harmonix: Harmonix, events: HarmonixEvent[]) => {
10+
for (const evt of events) {
11+
harmonix.events.set(evt.options.name!, evt)
12+
}
13+
}
14+
15+
export const loadCommands = (
16+
harmonix: Harmonix,
17+
commands: HarmonixCommand[]
18+
) => {
19+
for (const cmd of commands) {
20+
harmonix.commands.set(cmd.options.name!, cmd)
21+
}
22+
}
23+
24+
export const loadContextMenus = (
25+
harmonix: Harmonix,
26+
contextMenus: HarmonixContextMenu[]
27+
) => {
28+
for (const ctm of contextMenus) {
29+
harmonix.contextMenus.set(ctm.options.name!, ctm)
30+
}
31+
}
32+
33+
export const loadPreconditions = (
34+
harmonix: Harmonix,
35+
preconditions: HarmonixPrecondition[]
36+
) => {
37+
for (const prc of preconditions) {
38+
harmonix.preconditions.set(prc.options.name!, prc)
39+
}
40+
}

0 commit comments

Comments
 (0)