|
| 1 | +import { Client } from 'discord.js' |
| 2 | +import type { Harmony, HarmonyCommand, HarmonyEvent } from './types' |
| 3 | +import 'dotenv/config' |
| 4 | + |
| 5 | +export const initCient = (harmonyOptions: Harmony['options']) => { |
| 6 | + const client = new Client({ intents: harmonyOptions.intents }) |
| 7 | + |
| 8 | + client.login(process.env.HARMONY_TOKEN) |
| 9 | + |
| 10 | + return client |
| 11 | +} |
| 12 | + |
| 13 | +export const registerCommands = ( |
| 14 | + harmony: Harmony, |
| 15 | + commands: Map<string, HarmonyCommand<boolean>> |
| 16 | +) => { |
| 17 | + harmony.client?.on('messageCreate', (message) => { |
| 18 | + if (message.author.bot) return |
| 19 | + const prefix = harmony.options.defaultPrefix |
| 20 | + const args = message.content.slice(prefix.length).trim().split(/ +/) |
| 21 | + const command = args.shift()?.toLowerCase() |
| 22 | + |
| 23 | + if (!command) return |
| 24 | + const cmd = commands.get(command) |
| 25 | + |
| 26 | + if (!cmd || cmd.options.slash) return |
| 27 | + cmd.execute(harmony.client!, message) |
| 28 | + }) |
| 29 | + |
| 30 | + harmony.client?.on('interactionCreate', (interaction) => { |
| 31 | + if (!interaction.isChatInputCommand()) return |
| 32 | + const cmd = commands.get(interaction.commandName) |
| 33 | + |
| 34 | + if (!cmd || !cmd.options.slash) return |
| 35 | + cmd.execute(harmony.client!, interaction) |
| 36 | + }) |
| 37 | + |
| 38 | + for (const [name, command] of commands) { |
| 39 | + if (!command.options.slash) continue |
| 40 | + harmony.client?.application?.commands.create({ |
| 41 | + name: name, |
| 42 | + description: command.options.description || 'No description provided', |
| 43 | + options: command.options.arguments |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export const registerEvents = ( |
| 49 | + harmony: Harmony, |
| 50 | + events: Map<string, HarmonyEvent> |
| 51 | +) => { |
| 52 | + for (const [, event] of events) { |
| 53 | + if (event.options.once) { |
| 54 | + harmony.client?.once(event.options.name!, event.callback) |
| 55 | + } else { |
| 56 | + harmony.client?.on(event.options.name!, event.callback) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments