@@ -6,16 +6,18 @@ import {
66 type User ,
77 ApplicationCommandOptionType
88} from 'discord.js'
9+ import consola from 'consola'
910import {
1011 type CommandArg ,
1112 CommandArgType ,
1213 type Harmonix ,
1314 type HarmonixCommand ,
1415 type HarmonixEvent ,
15- type MessageOrInteraction
16+ type MessageOrInteraction ,
17+ HarmonixContextMenu
1618} from './types'
1719import 'dotenv/config'
18- import { toJSON } from './commands'
20+ import { slashToJSON , contextMenuToJSON } from './commands'
1921
2022export const initCient = ( harmonixOptions : Harmonix [ 'options' ] ) => {
2123 const client = new Client ( { intents : harmonixOptions . intents } )
@@ -25,6 +27,51 @@ export const initCient = (harmonixOptions: Harmonix['options']) => {
2527 return client
2628}
2729
30+ export const refreshApplicationCommands = async (
31+ harmonix : Harmonix ,
32+ commands : ( HarmonixCommand < true , CommandArg [ ] > | HarmonixContextMenu ) [ ]
33+ ) => {
34+ if ( commands . length === 0 ) return
35+ const rest = new REST ( ) . setToken ( process . env . HARMONIX_CLIENT_TOKEN ! )
36+
37+ try {
38+ consola . info ( 'Started refreshing application commands.' )
39+ await rest . put (
40+ Routes . applicationCommands (
41+ harmonix . options . clientId || process . env . HARMONIX_CLIENT_ID !
42+ ) ,
43+ {
44+ body : commands . map ( ( cmd ) =>
45+ isHarmonixCommand ( cmd ) ? slashToJSON ( cmd ) : contextMenuToJSON ( cmd )
46+ )
47+ }
48+ )
49+ consola . success ( 'Successfully reloaded application commands.' )
50+ } catch {
51+ consola . error ( 'Failed to reload application commands.' )
52+ }
53+ }
54+
55+ export const registerEvents = ( harmonix : Harmonix , events : HarmonixEvent [ ] ) => {
56+ for ( const event of events . filter ( ( evt ) => ! evt . options . type ) ) {
57+ if ( event . options . once ) {
58+ harmonix . client ?. once ( event . options . name ! , event . callback )
59+ } else {
60+ harmonix . client ?. on ( event . options . name ! , event . callback )
61+ }
62+ }
63+
64+ harmonix . client ?. on ( Events . InteractionCreate , ( interaction ) => {
65+ if ( ! interaction . isModalSubmit ( ) ) return
66+ const event = events
67+ . filter ( ( evt ) => evt . options . type === 'modal' )
68+ . find ( ( evt ) => evt . options . name === interaction . customId )
69+
70+ if ( ! event ) return
71+ event . callback ( interaction )
72+ } )
73+ }
74+
2875export const registerCommands = (
2976 harmonix : Harmonix ,
3077 commands : HarmonixCommand < false , CommandArg [ ] > [ ]
@@ -50,19 +97,10 @@ export const registerCommands = (
5097 } )
5198}
5299
53- export const registerSlashCommands = async (
100+ export const registerSlashCommands = (
54101 harmonix : Harmonix ,
55102 commands : HarmonixCommand < true , CommandArg [ ] > [ ]
56103) => {
57- if ( commands . length === 0 ) return
58- const rest = new REST ( ) . setToken ( process . env . HARMONIX_CLIENT_TOKEN ! )
59-
60- await rest . put (
61- Routes . applicationCommands (
62- harmonix . options . clientId || process . env . HARMONIX_CLIENT_ID !
63- ) ,
64- { body : commands . map ( ( cmd ) => toJSON ( cmd ) ) }
65- )
66104 harmonix . client ?. on ( Events . InteractionCreate , async ( interaction ) => {
67105 if ( ! interaction . isChatInputCommand ( ) ) return
68106 const cmd = commands . find (
@@ -89,26 +127,29 @@ export const registerSlashCommands = async (
89127 } )
90128}
91129
92- export const registerEvents = ( harmonix : Harmonix , events : HarmonixEvent [ ] ) => {
93- for ( const event of events . filter ( ( evt ) => ! evt . options . type ) ) {
94- if ( event . options . once ) {
95- harmonix . client ?. once ( event . options . name ! , event . callback )
96- } else {
97- harmonix . client ?. on ( event . options . name ! , event . callback )
98- }
99- }
100-
101- harmonix . client ?. on ( Events . InteractionCreate , ( interaction ) => {
102- if ( ! interaction . isModalSubmit ( ) ) return
103- const event = events
104- . filter ( ( evt ) => evt . options . type === 'modal' )
105- . find ( ( evt ) => evt . options . name === interaction . customId )
130+ export const registerContextMenu = (
131+ harmonix : Harmonix ,
132+ contextMenus : HarmonixContextMenu [ ]
133+ ) => {
134+ harmonix . client ?. on ( Events . InteractionCreate , async ( interaction ) => {
135+ if ( ! interaction . isContextMenuCommand ( ) ) return
136+ const ctm = contextMenus . find (
137+ ( ctm ) => ctm . options . name === interaction . commandName
138+ )
106139
107- if ( ! event ) return
108- event . callback ( interaction )
140+ if ( ! ctm ) return
141+ ctm . callback ( interaction )
109142 } )
110143}
111144
145+ const isHarmonixCommand = (
146+ command : HarmonixCommand < true , CommandArg [ ] > | HarmonixContextMenu
147+ ) : command is HarmonixCommand < true , CommandArg [ ] > => {
148+ return (
149+ ( command as HarmonixCommand < true , CommandArg [ ] > ) . options . slash !== undefined
150+ )
151+ }
152+
112153export const resolveArgument = async (
113154 entity : MessageOrInteraction ,
114155 type : CommandArgType ,
0 commit comments