Skip to content

Commit 186ac54

Browse files
committed
feat: discord interaction
1 parent 01857b9 commit 186ac54

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

src/discord.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

src/harmony.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { loadOptions } from './options'
33
import { scanCommands, scanEvents } from './scan'
44
import { resolveHarmonyCommand } from './commands'
55
import { resolveHarmonyEvent } from './events'
6+
import { initCient, registerCommands, registerEvents } from './discord'
67
import type { Harmony, HarmonyConfig, HarmonyOptions } from './types'
78

89
export const createHarmony = async (
@@ -34,8 +35,9 @@ export const createHarmony = async (
3435
})
3536
)
3637

37-
console.log('commands', commands)
38-
console.log('events', events)
38+
harmony.client = initCient(harmony.options)
39+
registerCommands(harmony, commands)
40+
registerEvents(harmony, events)
3941

4042
return harmony
4143
}

src/options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type { HarmonyConfig } from './types'
44

55
const HarmonyDefaults: HarmonyConfig = {
66
scanDirs: [],
7-
ignore: []
7+
ignore: [],
8+
intents: ['Guilds', 'GuildMessages', 'MessageContent']
89
}
910

1011
export const loadOptions = async (

src/types/harmony.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { C12InputConfig } from 'c12'
2-
import { HarmonyCommandInput } from './commands'
3-
import { HarmonyEventInput } from './events'
2+
import type {
3+
Client,
4+
BitFieldResolvable,
5+
GatewayIntentsString
6+
} from 'discord.js'
7+
import type { HarmonyCommandInput } from './commands'
8+
import type { HarmonyEventInput } from './events'
49

510
export interface HarmonyOptions {
611
rootDir: string
@@ -10,6 +15,7 @@ export interface HarmonyOptions {
1015
commands: HarmonyCommandInput[]
1116
events: HarmonyEventInput[]
1217
defaultPrefix: string
18+
intents: BitFieldResolvable<GatewayIntentsString, number>
1319
}
1420

1521
type DeepPartial<T> =
@@ -26,4 +32,5 @@ export interface HarmonyConfig
2632

2733
export interface Harmony {
2834
options: HarmonyOptions
35+
client?: Client
2936
}

0 commit comments

Comments
 (0)