File tree Expand file tree Collapse file tree 4 files changed +27
-4
lines changed Expand file tree Collapse file tree 4 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,8 @@ import {
2929 registerCommands ,
3030 registerButtons ,
3131 registerModals ,
32- registerSelectMenus
32+ registerSelectMenus ,
33+ registerAutocomplete
3334} from './register'
3435import {
3536 resolveButton ,
@@ -130,6 +131,7 @@ export const createHarmonix = async (
130131 registerButtons ( harmonix )
131132 registerModals ( harmonix )
132133 registerSelectMenus ( harmonix )
134+ registerAutocomplete ( harmonix )
133135
134136 return harmonix
135137}
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { ClientEvents, Events } from 'discord.js'
22import consola from 'consola'
33import { colors } from 'consola/utils'
44import { resolveOption } from './utils'
5- import { ctx } from './harmonix'
5+ import { createError , ctx } from './harmonix'
66import type { Harmonix , ParsedInputs , ParsedOptions } from './types'
77
88export const registerEvents = ( harmonix : Harmonix ) => {
@@ -143,3 +143,17 @@ export const registerSelectMenus = (harmonix: Harmonix) => {
143143 slm . callback ( interaction )
144144 } )
145145}
146+
147+ export const registerAutocomplete = ( harmonix : Harmonix ) => {
148+ harmonix . client ?. on ( Events . InteractionCreate , async ( interaction ) => {
149+ if ( ! interaction . isAutocomplete ( ) ) return
150+ const cmd = harmonix . commands . get ( interaction . commandName )
151+
152+ if ( ! cmd || ! cmd . config . autocomplete ) return
153+ try {
154+ await cmd . config . autocomplete ( interaction )
155+ } catch ( error : any ) {
156+ createError ( error . message )
157+ }
158+ } )
159+ }
Original file line number Diff line number Diff line change @@ -9,7 +9,8 @@ import type {
99 APIInteractionDataResolvedChannel ,
1010 GuildMember ,
1111 APIInteractionDataResolvedGuildMember ,
12- Attachment
12+ Attachment ,
13+ AutocompleteInteraction
1314} from 'discord.js'
1415
1516type OptionType =
@@ -28,7 +29,9 @@ type _OptionDef<T extends OptionType> = {
2829 description ?: string
2930 required ?: boolean
3031 metadata ?: Record < string , any >
31- }
32+ } & ( T extends 'String' | 'Integer' | 'Number'
33+ ? { autocomplete ?: boolean }
34+ : { } )
3235
3336type StringOptionDef = _OptionDef < 'String' >
3437type IntegerOptionDef = _OptionDef < 'Integer' >
@@ -170,6 +173,7 @@ export interface CommandConfig<T extends OptionsDef = OptionsDef> {
170173 userPermissions ?: PermissionsString [ ]
171174 preconditions ?: string [ ]
172175 dm ?: boolean
176+ autocomplete ?: ( interaction : AutocompleteInteraction ) => Promise < void > | void
173177}
174178
175179export type CommandExecute < T extends OptionsDef = OptionsDef > = (
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ export const slashToJSON = (cmd: HarmonixCommand<OptionsDef>) => {
3737 . setName ( name )
3838 . setDescription ( arg . description ?? 'No description provided' )
3939 . setRequired ( arg . required ?? true )
40+ . setAutocomplete ( arg . autocomplete ?? false )
4041
4142 if ( arg . metadata ?. minLength ) {
4243 opt . setMinLength ( arg . metadata . minLength )
@@ -60,6 +61,7 @@ export const slashToJSON = (cmd: HarmonixCommand<OptionsDef>) => {
6061 . setName ( name )
6162 . setDescription ( arg . description ?? 'No description provided' )
6263 . setRequired ( arg . required ?? true )
64+ . setAutocomplete ( arg . autocomplete ?? false )
6365
6466 if ( arg . metadata ?. minValue ) {
6567 opt . setMinValue ( arg . metadata . minValue )
@@ -123,6 +125,7 @@ export const slashToJSON = (cmd: HarmonixCommand<OptionsDef>) => {
123125 . setName ( name )
124126 . setDescription ( arg . description ?? 'No description provided' )
125127 . setRequired ( arg . required ?? true )
128+ . setAutocomplete ( arg . autocomplete ?? false )
126129
127130 if ( arg . metadata ?. minValue ) {
128131 opt . setMinValue ( arg . metadata . minValue )
You can’t perform that action at this time.
0 commit comments