Skip to content

Commit

Permalink
feat: add useHamronix function to access to harmonix instance
Browse files Browse the repository at this point in the history
  • Loading branch information
nethriis committed May 16, 2024
1 parent cbbbcdd commit 1339153
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 94 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"dotenv": "^16.4.5",
"globby": "^14.0.1",
"jiti": "^1.21.0",
"pathe": "^1.1.2"
"pathe": "^1.1.2",
"unctx": "^2.3.1"
},
"devDependencies": {
"@types/node": "^20.12.8",
Expand Down
57 changes: 57 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions src/discord.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { Client, REST, Routes } from 'discord.js'
import consola from 'consola'
import type {
ArgsDef,
Harmonix,
HarmonixCommand,
HarmonixContextMenu
} from './types'
import type { Harmonix } from './types'
import 'dotenv/config'
import { createError } from './harmonix'
import { contextMenuToJSON, isHarmonixCommand, slashToJSON } from './utils'
Expand All @@ -18,10 +13,11 @@ export const initCient = (harmonixOptions: Harmonix['options']) => {
return client
}

export const refreshApplicationCommands = async (
harmonix: Harmonix,
commands: (HarmonixCommand<true, ArgsDef> | HarmonixContextMenu)[]
) => {
export const refreshApplicationCommands = async (harmonix: Harmonix) => {
const commands = [
...harmonix.commands.filter((cmd) => cmd.options.slash).map((cmd) => cmd),
...harmonix.contextMenus.map((cmd) => cmd)
]
const rest = new REST().setToken(process.env.HARMONIX_CLIENT_TOKEN!)

try {
Expand Down
46 changes: 28 additions & 18 deletions src/harmonix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Collection } from 'discord.js'
import type { LoadConfigOptions } from 'c12'
import { getContext } from 'unctx'
import consola from 'consola'
import { colors } from 'consola/utils'
import { loadOptions } from './options'
Expand All @@ -8,12 +10,17 @@ import {
scanEvents,
scanPreconditions
} from './scan'
import {
loadCommands,
loadContextMenus,
loadEvents,
loadPreconditions
} from './load'
import { initCient, refreshApplicationCommands } from './discord'
import {
registerMessageCommands,
registerContextMenu,
registerEvents,
registerPreconditions,
registerSlashCommands
} from './register'
import {
Expand All @@ -25,15 +32,21 @@ import {
import type { Harmonix, HarmonixConfig, HarmonixOptions } from './types'
import { version } from '../package.json'

export const ctx = getContext<Harmonix>('harmonix')

export const useHarmonix = ctx.use

export const createHarmonix = async (
config: HarmonixConfig = {},
opts: LoadConfigOptions = {}
) => {
const options = await loadOptions(config, opts)
const harmonix: Harmonix = {
options: options as HarmonixOptions,
preconditions: new Map(),
commands: new Map()
events: new Collection(),
commands: new Collection(),
contextMenus: new Collection(),
preconditions: new Collection()
}

const scannedCommands = await scanCommands(harmonix)
Expand Down Expand Up @@ -75,22 +88,19 @@ export const createHarmonix = async (
)
}
consola.log(colors.blue(`Harmonix ${colors.bold(version)}\n`))

loadEvents(harmonix, events)
loadCommands(harmonix, commands)
loadContextMenus(harmonix, contextMenus)
loadPreconditions(harmonix, preconditions)

harmonix.client = initCient(harmonix.options)
registerEvents(harmonix, events)
registerPreconditions(harmonix, preconditions)
await refreshApplicationCommands(harmonix, [
...commands.filter((cmd) => cmd.options.slash),
...contextMenus
])
registerMessageCommands(
harmonix,
commands.filter((cmd) => !cmd.options.slash)
)
registerSlashCommands(
harmonix,
commands.filter((cmd) => cmd.options.slash)
)
registerContextMenu(harmonix, contextMenus)

registerEvents(harmonix)
await refreshApplicationCommands(harmonix)
registerMessageCommands(harmonix)
registerSlashCommands(harmonix)
registerContextMenu(harmonix)

return harmonix
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './define'
export * from './harmonix'
export { createHarmonix, createError, useHarmonix } from './harmonix'
40 changes: 40 additions & 0 deletions src/load.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type {
Harmonix,
HarmonixCommand,
HarmonixContextMenu,
HarmonixEvent,
HarmonixPrecondition
} from './types'

export const loadEvents = (harmonix: Harmonix, events: HarmonixEvent[]) => {
for (const evt of events) {
harmonix.events.set(evt.options.name!, evt)
}
}

export const loadCommands = (
harmonix: Harmonix,
commands: HarmonixCommand[]
) => {
for (const cmd of commands) {
harmonix.commands.set(cmd.options.name!, cmd)
}
}

export const loadContextMenus = (
harmonix: Harmonix,
contextMenus: HarmonixContextMenu[]
) => {
for (const ctm of contextMenus) {
harmonix.contextMenus.set(ctm.options.name!, ctm)
}
}

export const loadPreconditions = (
harmonix: Harmonix,
preconditions: HarmonixPrecondition[]
) => {
for (const prc of preconditions) {
harmonix.preconditions.set(prc.options.name!, prc)
}
}
Loading

0 comments on commit 1339153

Please sign in to comment.