1+ // we need to await in a loop as we're rate-limited anyway
2+ /* eslint-disable no-await-in-loop */
13/* eslint-disable @typescript-eslint/no-non-null-assertion */
24import type {
35 ApplicationCommand ,
@@ -7,12 +9,15 @@ import type {
79 Guild ,
810 GuildApplicationCommandManager ,
911 GuildResolvable ,
12+ ApplicationCommandType ,
1013} from 'discord.js' ;
1114import { Collection } from 'discord.js' ;
15+ import { ApplicationCommandTypes } from 'discord.js/typings/enums' ;
1216import { filter } from 'domyno' ;
1317import { isEqual } from 'lodash-es' ;
1418
1519import type { CommandDataWithHandler } from '../../types' ;
20+ import { commands } from '../modules/modmail' ;
1621import { asyncCatch } from '../utils/asyncCatch.js' ;
1722import { map , mapʹ } from '../utils/map.js' ;
1823import { merge } from '../utils/merge.js' ;
@@ -46,6 +51,7 @@ export const guildCommands = new Map(
4651 shitpostInteraction ,
4752 npmInteraction ,
4853 whynoInteraction ,
54+ ...commands ,
4955 // warn // Not used atm
5056 ] . map ( command => [ command . name , command ] )
5157) ; // placeholder for now
@@ -58,16 +64,22 @@ export const applicationCommands = new Collection<
5864const getRelevantCmdProperties = ( {
5965 description,
6066 name,
67+ type = ApplicationCommandTypes . CHAT_INPUT ,
6168 options,
69+ defaultPermission = true ,
6270} : {
63- description : string ;
71+ type ?: ApplicationCommandTypes | ApplicationCommandType ;
72+ description ?: string ;
6473 name : string ;
6574 options ?: unknown [ ] ;
75+ defaultPermission ?: boolean ;
6676} ) : ApplicationCommandData => {
6777 const relevantData = {
78+ type : _normalizeType ( type ) ,
6879 description,
6980 name,
7081 options,
82+ defaultPermission,
7183 } as unknown as ApplicationCommandData ;
7284 return stripNullish ( normalizeApplicationCommandData ( relevantData ) ) ;
7385} ;
@@ -92,7 +104,7 @@ export const registerCommands = async (client: Client): Promise<void> => {
92104 client . on (
93105 'interactionCreate' ,
94106 asyncCatch ( async interaction => {
95- if ( ! interaction . isCommand ( ) ) {
107+ if ( ! interaction . isCommand ( ) && ! interaction . isContextMenu ( ) ) {
96108 return ;
97109 }
98110
@@ -123,13 +135,13 @@ export const registerCommands = async (client: Client): Promise<void> => {
123135
124136 for ( const { onAttach } of applicationCommands . values ( ) ) {
125137 // We're attaching these so it's fine
126-
138+
127139 onAttach ?.( client ) ;
128140 }
129141
130142 for ( const { onAttach } of guildCommands . values ( ) ) {
131143 // We're attaching these so it's fine
132-
144+
133145 onAttach ?.( client ) ;
134146 }
135147
@@ -161,6 +173,25 @@ export const registerCommands = async (client: Client): Promise<void> => {
161173 // })
162174} ;
163175
176+ function _normalizeType (
177+ type : ApplicationCommandType | ApplicationCommandTypes
178+ ) {
179+ if ( typeof type === 'number' ) {
180+ return type ;
181+ }
182+
183+ switch ( type ) {
184+ case 'MESSAGE' :
185+ return ApplicationCommandTypes . MESSAGE ;
186+ case 'USER' :
187+ return ApplicationCommandTypes . USER ;
188+ case 'CHAT_INPUT' :
189+ default :
190+ return ApplicationCommandTypes . CHAT_INPUT ;
191+ }
192+ }
193+
194+ const interactionTypes = new Set ( [ 'CHAT_INPUT' , 'USER' , 'MESSAGE' ] )
164195async function addCommands (
165196 serverCommands : Collection <
166197 string ,
@@ -169,25 +200,22 @@ async function addCommands(
169200 commandDescriptions : Map < string , CommandDataWithHandler > ,
170201 commandManager : ApplicationCommandManager | GuildApplicationCommandManager
171202) {
172- const discordChatInputCommandsById = serverCommands . filter (
173- x => x . type === 'CHAT_INPUT'
203+ const discordInteractionsById = serverCommands . filter (
204+ x => interactionTypes . has ( x . type )
174205 ) ;
175206
176207 const discordCommands = new Collection (
177- discordChatInputCommandsById . map ( value => [ value . name , value ] )
208+ discordInteractionsById . map ( value => [ value . name , value ] )
178209 ) ;
179210
180- const validCommands = pipe <
181- Iterable < [ string , CommandDataWithHandler ] > ,
182- Iterable < string >
183- > ( [
211+ const validCommands = pipe ( [
184212 filter < [ string , CommandDataWithHandler ] > (
185- ( [ key , val ] : [ string , CommandDataWithHandler ] ) =>
213+ ( [ , val ] : [ string , CommandDataWithHandler ] ) =>
186214 'guild' in commandManager && val . guildValidate
187215 ? val . guildValidate ( commandManager . guild )
188216 : true
189217 ) ,
190- map ( ( [ key ] ) => key ) ,
218+ map ( ( [ key ] ) : string => key ) ,
191219 ] ) ;
192220
193221 const newCommands = difference (
@@ -218,9 +246,7 @@ async function addCommands(
218246}
219247
220248function getDestination (
221- commandManager :
222- | ApplicationCommandManager
223- | GuildApplicationCommandManager
249+ commandManager : ApplicationCommandManager | GuildApplicationCommandManager
224250) {
225251 return 'guild' in commandManager
226252 ? `Guild: ${ commandManager . guild . name } `
@@ -236,10 +262,13 @@ function createNewCommands(
236262 const command = cmdDescriptions . get ( name ) ;
237263 // this is always true
238264 if ( command ) {
239- const { onAttach, handler, ...rest } = command ;
265+ const { onAttach, handler, managePermissions , ...rest } = command ;
240266 console . info ( `Adding Command ${ name } for ${ destination } ` ) ;
241267
242- return cmdMgr . create ( rest ) ;
268+ const guildCmd = await cmdMgr . create ( rest ) ;
269+ const { permissions, guild } = guildCmd ;
270+
271+ await managePermissions ?.( guild , permissions ) ;
243272 }
244273 } ) ;
245274}
@@ -250,11 +279,11 @@ function editExistingCommands(
250279 existingCommands : Map < string , ApplicationCommand >
251280) {
252281 const destination = getDestination ( cmdMgr ) ;
253- return map ( ( name : string ) => {
282+ return map ( async ( name : string ) => {
254283 const cmd = cmdDescriptions . get ( name ) ;
255284 const existing = existingCommands . get ( name ) ;
256285
257- const { onAttach, handler, ...command } = cmd ;
286+ const { onAttach, handler, managePermissions , ...command } = cmd ;
258287
259288 if (
260289 ! isEqual (
@@ -263,8 +292,18 @@ function editExistingCommands(
263292 )
264293 ) {
265294 console . info ( `Updating ${ name } for ${ destination } ` ) ;
295+ console . log (
296+ getRelevantCmdProperties ( cmd ) ,
297+ getRelevantCmdProperties ( existing ) )
266298
267- return cmdMgr . edit ( existing . id , command ) ;
299+ await cmdMgr . edit ( existing . id , command ) ;
300+ }
301+
302+ try {
303+ const { permissions, guild } = existing ;
304+ await managePermissions ?.( guild , permissions ) ;
305+ } catch ( error ) {
306+ console . log ( { error } ) ;
268307 }
269308 } ) ;
270309}
@@ -278,6 +317,6 @@ function deleteRemovedCommands(
278317 const existing = existingCommands . get ( name ) ! ;
279318 console . warn ( `Deleting ${ name } from ${ destination } ` ) ;
280319
281- return cmdMgr . delete ( existing . id ) ;
320+ await cmdMgr . delete ( existing . id ) ;
282321 } ) ;
283322}
0 commit comments