@@ -195,8 +195,9 @@ export default class DiscordAnalytics {
195195 * /!\ Advanced users only
196196 * /!\ You need to initialize the class first
197197 * @param interaction - BaseInteraction class and its extensions only
198+ * @param interactionNameResolver - A function that will resolve the name of the interaction
198199 */
199- public async trackInteractions ( interaction : any ) {
200+ public async trackInteractions ( interaction : any , interactionNameResolver ?: ( interaction : any ) => string ) {
200201 if ( this . _debug ) console . log ( "[DISCORDANALYTICS] trackInteractions() triggered" )
201202 if ( ! this . _isReady ) throw new Error ( ErrorCodes . INSTANCE_NOT_INITIALIZED )
202203
@@ -213,15 +214,19 @@ export default class DiscordAnalytics {
213214
214215 if ( interaction . type === InteractionType . ApplicationCommand ) {
215216 const commandType = interaction . command ? interaction . command . type : ApplicationCommandType . ChatInputCommand ;
216- this . statsData . interactions . find ( ( x ) => x . name === interaction . commandName && x . type === interaction . type && x . command_type === commandType ) ?
217- ++ this . statsData . interactions . find ( ( x ) => x . name === interaction . commandName && x . type === interaction . type && x . command_type === commandType ) ! . number :
218- this . statsData . interactions . push ( { name : interaction . commandName , number : 1 , type : interaction . type as InteractionType , command_type : commandType } ) ;
217+ const commandName = interactionNameResolver ? interactionNameResolver ( interaction ) : interaction . commandName ;
218+ this . statsData . interactions . find ( ( x ) => x . name === commandName && x . type === interaction . type && x . command_type === commandType ) ?
219+ ++ this . statsData . interactions . find ( ( x ) => x . name === commandName && x . type === interaction . type && x . command_type === commandType ) ! . number :
220+ this . statsData . interactions . push ( { name : commandName , number : 1 , type : interaction . type as InteractionType , command_type : commandType } ) ;
219221 }
220222
221- else if ( interaction . type === InteractionType . MessageComponent || interaction . type === InteractionType . ModalSubmit )
222- this . statsData . interactions . find ( ( x ) => x . name === interaction . customId && x . type === interaction . type ) ?
223- ++ this . statsData . interactions . find ( ( x ) => x . name === interaction . customId && x . type === interaction . type ) ! . number :
224- this . statsData . interactions . push ( { name : interaction . customId , number : 1 , type : interaction . type } ) ;
223+ else if ( interaction . type === InteractionType . MessageComponent || interaction . type === InteractionType . ModalSubmit ) {
224+ const interactionName = interactionNameResolver ? interactionNameResolver ( interaction ) : interaction . customId ;
225+
226+ this . statsData . interactions . find ( ( x ) => x . name === interactionName && x . type === interaction . type ) ?
227+ ++ this . statsData . interactions . find ( ( x ) => x . name === interactionName && x . type === interaction . type ) ! . number :
228+ this . statsData . interactions . push ( { name : interactionName , number : 1 , type : interaction . type } ) ;
229+ }
225230
226231 const guildData = this . statsData . guildsStats . find ( guild => interaction . guild ? guild . guildId === interaction . guild . id : guild . guildId === "dm" )
227232 if ( guildData ) this . statsData . guildsStats = this . statsData . guildsStats . filter ( guild => guild . guildId !== guildData . guildId )
@@ -259,11 +264,12 @@ export default class DiscordAnalytics {
259264 * Let DiscordAnalytics declare the events necessary for its operation.
260265 * /!\ Not recommended for big bots
261266 * /!\ Not compatible with other functions
267+ * @param interactionNameResolver - A function that will resolve the name of the interaction
262268 */
263- public trackEvents ( ) {
269+ public trackEvents ( interactionNameResolver ?: ( interaction : any ) => string ) {
264270 if ( ! this . _client . isReady ( ) ) this . _client . on ( "ready" , async ( ) => await this . init ( ) )
265271 else this . init ( )
266- this . _client . on ( "interactionCreate" , async ( interaction : any ) => await this . trackInteractions ( interaction ) )
272+ this . _client . on ( "interactionCreate" , async ( interaction : any ) => await this . trackInteractions ( interaction , interactionNameResolver ) )
267273 this . _client . on ( "guildCreate" , ( guild : any ) => this . trackGuilds ( guild , "create" ) )
268274 this . _client . on ( "guildDelete" , ( guild : any ) => this . trackGuilds ( guild , "delete" ) )
269275 }
0 commit comments