Skip to content

Commit

Permalink
fix: rename harmony to harmonix
Browse files Browse the repository at this point in the history
  • Loading branch information
nethriis committed May 9, 2024
1 parent f329d3c commit 76f4957
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 138 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ packages/*/LICENSE

# Generated dirs
dist
.harmony
.harmony-*
.harmonix
.harmonix-*
.output
.output-*
.gen
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Harmony
Copyright (c) 2024 Harmonix

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Harmony
# Harmonix

Create Discord Bot with ease and simplicity.

## License

Published under the [MIT](https://github.com/harmony-ts/harmony/blob/main/LICENSE) license.
Published under the [MIT](https://github.com/harmonix-js/core/blob/main/LICENSE) license.
Made by [@nethriis](https://github.com/nethriis) 💙
8 changes: 4 additions & 4 deletions bin/index.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node

import { createHarmony } from '../dist/index.mjs'
import { createHarmonix } from '../dist/index.mjs'
import 'dotenv/config'

const initHarmony = async () => {
await createHarmony({ rootDir: './playground' }, { cwd: './playground' })
const initHarmonix = async () => {
await createHarmonix({ rootDir: './playground' }, { cwd: './playground' })
}

initHarmony()
initHarmonix()
6 changes: 6 additions & 0 deletions playground/harmonix.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineHarmonixConfig } from '../src'

export default defineHarmonixConfig({
defaultPrefix: 'h!',
clientId: '929023549656662047'
})
6 changes: 0 additions & 6 deletions playground/harmony.config.ts

This file was deleted.

26 changes: 13 additions & 13 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ import { dirname } from 'pathe'
import { filename } from 'pathe/utils'
import {
CommandArgType,
type HarmonyCommandArgType,
type HarmonixCommandArgType,
type CommandExecute,
type CommandOptions,
type Harmony,
type HarmonyCommand,
type HarmonyCommandInput,
type Harmonix,
type HarmonixCommand,
type HarmonixCommandInput,
type CommandArg,
type CommandExecuteOptions,
ArgumentResolver
} from './types'
import { SlashCommandBuilder } from 'discord.js'

export const resolveHarmonyCommand = (
cmd: HarmonyCommandInput,
harmonyOptions: Harmony['options']
): HarmonyCommand<boolean> => {
export const resolveHarmonixCommand = (
cmd: HarmonixCommandInput,
harmonixOptions: Harmonix['options']
): HarmonixCommand<boolean> => {
if (typeof cmd === 'string') {
const _jiti = jiti(harmonyOptions.rootDir, {
const _jiti = jiti(harmonixOptions.rootDir, {
interopDefault: true
})
const _cmdPath = _jiti.resolve(cmd)
const command = _jiti(_cmdPath) as HarmonyCommand<boolean>
const command = _jiti(_cmdPath) as HarmonixCommand<boolean>
const options: CommandOptions = {
name: command.options.name || filename(_cmdPath).split('.')[0],
category: command.options.category || filename(dirname(_cmdPath)),
Expand All @@ -41,11 +41,11 @@ export const resolveHarmonyCommand = (
export const defineCommand = <Slash extends boolean>(
options: CommandOptions & { slash?: Slash },
execute: CommandExecute<Slash>
): HarmonyCommand<Slash> => {
): HarmonixCommand<Slash> => {
return { options, execute }
}

export const toJSON = (cmd: HarmonyCommand<true>) => {
export const toJSON = (cmd: HarmonixCommand<true>) => {
const builder = new SlashCommandBuilder()
.setName(cmd.options.name!)
.setDescription(cmd.options.description || 'No description provided')
Expand Down Expand Up @@ -133,7 +133,7 @@ export const toJSON = (cmd: HarmonyCommand<true>) => {
}

export const defineArgument = <
Type extends keyof HarmonyCommandArgType
Type extends keyof HarmonixCommandArgType
>(options: {
type: Type
name: string
Expand Down
36 changes: 18 additions & 18 deletions src/discord.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Client, REST, Routes, Events } from 'discord.js'
import type { Harmony, HarmonyCommand, HarmonyEvent } from './types'
import type { Harmonix, HarmonixCommand, HarmonixEvent } from './types'
import 'dotenv/config'
import { toJSON } from './commands'

export const initCient = (harmonyOptions: Harmony['options']) => {
const client = new Client({ intents: harmonyOptions.intents })
export const initCient = (harmonixOptions: Harmonix['options']) => {
const client = new Client({ intents: harmonixOptions.intents })

client.login(process.env.HARMONY_CLIENT_TOKEN)
client.login(process.env.HARMONIX_CLIENT_TOKEN)

return client
}

export const registerCommands = (
harmony: Harmony,
commands: HarmonyCommand<false>[]
harmonix: Harmonix,
commands: HarmonixCommand<false>[]
) => {
harmony.client?.on(Events.MessageCreate, (message) => {
harmonix.client?.on(Events.MessageCreate, (message) => {
if (message.author.bot) return
const prefix = harmony.options.defaultPrefix
const prefix = harmonix.options.defaultPrefix
const args = message.content.slice(prefix.length).trim().split(/ +/)
const command = args.shift()?.toLowerCase()

Expand All @@ -28,43 +28,43 @@ export const registerCommands = (
cmd?.options.args?.map((arg, i) => [arg.name, args[i]]) || []
)
if (!cmd || cmd.options.slash) return
cmd.execute(harmony.client!, message, { slash: false, params: params })
cmd.execute(harmonix.client!, message, { slash: false, params: params })
})
}

export const registerSlashCommands = async (
harmony: Harmony,
commands: HarmonyCommand<true>[]
harmonix: Harmonix,
commands: HarmonixCommand<true>[]
) => {
if (commands.length === 0) return
const rest = new REST().setToken(process.env.HARMONY_CLIENT_TOKEN || '')
const rest = new REST().setToken(process.env.HARMONIX_CLIENT_TOKEN || '')

await rest.put(
Routes.applicationCommands(
harmony.options.clientId || process.env.HARMONY_CLIENT_ID || ''
harmonix.options.clientId || process.env.HARMONIX_CLIENT_ID || ''
),
{ body: commands.map((cmd) => toJSON(cmd)) }
)
harmony.client?.on('interactionCreate', (interaction) => {
harmonix.client?.on('interactionCreate', (interaction) => {
if (!interaction.isChatInputCommand()) return
const cmd = commands.find(
(cmd) => cmd.options.name === interaction.commandName
)

if (!cmd || !cmd.options.slash) return
cmd.execute(harmony.client!, interaction, {
cmd.execute(harmonix.client!, interaction, {
slash: true,
params: interaction.options
})
})
}

export const registerEvents = (harmony: Harmony, events: HarmonyEvent[]) => {
export const registerEvents = (harmonix: Harmonix, events: HarmonixEvent[]) => {
for (const event of events) {
if (event.options.once) {
harmony.client?.once(event.options.name!, event.callback)
harmonix.client?.once(event.options.name!, event.callback)
} else {
harmony.client?.on(event.options.name!, event.callback)
harmonix.client?.on(event.options.name!, event.callback)
}
}
}
20 changes: 10 additions & 10 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import type {
DefineEventWithOptions,
EventCallback,
EventOptions,
Harmony,
HarmonyEvent,
HarmonyEventInput
Harmonix,
HarmonixEvent,
HarmonixEventInput
} from './types'

export const resolveHarmonyEvent = (
evt: HarmonyEventInput,
harmonyOptions: Harmony['options']
): HarmonyEvent => {
export const resolveHarmonixEvent = (
evt: HarmonixEventInput,
harmonixOptions: Harmonix['options']
): HarmonixEvent => {
if (typeof evt === 'string') {
const _jiti = jiti(harmonyOptions.rootDir, {
const _jiti = jiti(harmonixOptions.rootDir, {
interopDefault: true
})
const _evtPath = _jiti.resolve(evt)
const event = _jiti(_evtPath) as HarmonyEvent
const event = _jiti(_evtPath) as HarmonixEvent
const options: EventOptions = {
name: event.options.name || filename(_evtPath).split('.')[0],
once: event.options.once || filename(_evtPath).endsWith('.once')
Expand All @@ -33,7 +33,7 @@ export const resolveHarmonyEvent = (

export const defineEvent: DefineEvent & DefineEventWithOptions = (
...args: [EventOptions | EventCallback, EventCallback?]
): HarmonyEvent => {
): HarmonixEvent => {
let options: EventOptions = {}

if (args.length === 1) {
Expand Down
45 changes: 45 additions & 0 deletions src/harmonix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { LoadConfigOptions } from 'c12'
import { loadOptions } from './options'
import { scanCommands, scanEvents } from './scan'
import { resolveHarmonixCommand } from './commands'
import { resolveHarmonixEvent } from './events'
import {
initCient,
registerCommands,
registerEvents,
registerSlashCommands
} from './discord'
import type { Harmonix, HarmonixConfig, HarmonixOptions } from './types'

export const createHarmonix = async (
config: HarmonixConfig = {},
opts: LoadConfigOptions = {}
) => {
const options = await loadOptions(config, opts)
const harmonix: Harmonix = {
options: options as HarmonixOptions
}

const scannedCommands = await scanCommands(harmonix)
const _commands = [...(harmonix.options.commands || []), ...scannedCommands]
const commands = _commands.map((cmd) =>
resolveHarmonixCommand(cmd, harmonix.options)
)

const scannedEvents = await scanEvents(harmonix)
const _events = [...(harmonix.options.events || []), ...scannedEvents]
const events = _events.map((evt) => resolveHarmonixEvent(evt, harmonix.options))

harmonix.client = initCient(harmonix.options)
registerCommands(
harmonix,
commands.filter((cmd) => !cmd.options.slash)
)
await registerSlashCommands(
harmonix,
commands.filter((cmd) => cmd.options.slash)
)
registerEvents(harmonix, events)

return harmonix
}
45 changes: 0 additions & 45 deletions src/harmony.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './events'
export * from './commands'
export * from './options'
export * from './harmony'
export * from './harmonix'
export * from './scan'
18 changes: 9 additions & 9 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { LoadConfigOptions, loadConfig } from 'c12'
import { resolve } from 'pathe'
import type { HarmonyConfig } from './types'
import type { HarmonixConfig } from './types'

const HarmonyDefaults: HarmonyConfig = {
const HarmonixDefaults: HarmonixConfig = {
scanDirs: [],
ignore: [],
intents: ['Guilds', 'GuildMessages', 'MessageContent']
}

export const loadOptions = async (
configOverrides: HarmonyConfig = {},
configOverrides: HarmonixConfig = {},
opts: LoadConfigOptions
) => {
const { config } = await loadConfig<HarmonyConfig>({
name: 'harmony',
configFile: 'harmony.config',
rcFile: '.harmonyrc',
const { config } = await loadConfig<HarmonixConfig>({
name: 'harmonix',
configFile: 'harmonix.config',
rcFile: '.harmonixrc',
dotenv: true,
globalRc: true,
overrides: configOverrides,
defaults: HarmonyDefaults,
defaults: HarmonixDefaults,
...opts
})

Expand All @@ -40,6 +40,6 @@ export const loadOptions = async (
return options
}

export const defineHarmonyConfig = (config: HarmonyConfig) => {
export const defineHarmonixConfig = (config: HarmonixConfig) => {
return config
}
Loading

0 comments on commit 76f4957

Please sign in to comment.