From fab24b8a300c3ee4a7dff66c382ad8003580ceef Mon Sep 17 00:00:00 2001 From: Nolhan Date: Sat, 29 Mar 2025 14:13:27 +0100 Subject: [PATCH] feat: Add interaction name resolver to trackInteractions and trackEvents methods --- src/discordjs/index.ts | 26 ++++++++++++++++---------- src/test/discordjs/index.ts | 13 +++++++++++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/discordjs/index.ts b/src/discordjs/index.ts index 5932c68..5cc06ba 100644 --- a/src/discordjs/index.ts +++ b/src/discordjs/index.ts @@ -195,8 +195,9 @@ export default class DiscordAnalytics { * /!\ Advanced users only * /!\ You need to initialize the class first * @param interaction - BaseInteraction class and its extensions only + * @param interactionNameResolver - A function that will resolve the name of the interaction */ - public async trackInteractions(interaction: any) { + public async trackInteractions(interaction: any, interactionNameResolver?: (interaction: any) => string) { if (this._debug) console.log("[DISCORDANALYTICS] trackInteractions() triggered") if (!this._isReady) throw new Error(ErrorCodes.INSTANCE_NOT_INITIALIZED) @@ -213,15 +214,19 @@ export default class DiscordAnalytics { if (interaction.type === InteractionType.ApplicationCommand) { const commandType = interaction.command ? interaction.command.type : ApplicationCommandType.ChatInputCommand; - this.statsData.interactions.find((x) => x.name === interaction.commandName && x.type === interaction.type && x.command_type === commandType) ? - ++this.statsData.interactions.find((x) => x.name === interaction.commandName && x.type === interaction.type && x.command_type === commandType)!.number : - this.statsData.interactions.push({ name: interaction.commandName, number: 1, type: interaction.type as InteractionType, command_type: commandType }); + const commandName = interactionNameResolver ? interactionNameResolver(interaction) : interaction.commandName; + this.statsData.interactions.find((x) => x.name === commandName && x.type === interaction.type && x.command_type === commandType) ? + ++this.statsData.interactions.find((x) => x.name === commandName && x.type === interaction.type && x.command_type === commandType)!.number : + this.statsData.interactions.push({ name: commandName, number: 1, type: interaction.type as InteractionType, command_type: commandType }); } - else if (interaction.type === InteractionType.MessageComponent || interaction.type === InteractionType.ModalSubmit) - this.statsData.interactions.find((x) => x.name === interaction.customId && x.type === interaction.type) ? - ++this.statsData.interactions.find((x) => x.name === interaction.customId && x.type === interaction.type)!.number : - this.statsData.interactions.push({ name: interaction.customId, number: 1, type: interaction.type }); + else if (interaction.type === InteractionType.MessageComponent || interaction.type === InteractionType.ModalSubmit) { + const interactionName = interactionNameResolver ? interactionNameResolver(interaction) : interaction.customId; + + this.statsData.interactions.find((x) => x.name === interactionName && x.type === interaction.type) ? + ++this.statsData.interactions.find((x) => x.name === interactionName && x.type === interaction.type)!.number : + this.statsData.interactions.push({name: interactionName, number: 1, type: interaction.type}); + } const guildData = this.statsData.guildsStats.find(guild => interaction.guild ? guild.guildId === interaction.guild.id : guild.guildId === "dm") if (guildData) this.statsData.guildsStats = this.statsData.guildsStats.filter(guild => guild.guildId !== guildData.guildId) @@ -259,11 +264,12 @@ export default class DiscordAnalytics { * Let DiscordAnalytics declare the events necessary for its operation. * /!\ Not recommended for big bots * /!\ Not compatible with other functions + * @param interactionNameResolver - A function that will resolve the name of the interaction */ - public trackEvents() { + public trackEvents(interactionNameResolver?: (interaction: any) => string) { if (!this._client.isReady()) this._client.on("ready", async () => await this.init()) else this.init() - this._client.on("interactionCreate", async (interaction: any) => await this.trackInteractions(interaction)) + this._client.on("interactionCreate", async (interaction: any) => await this.trackInteractions(interaction, interactionNameResolver)) this._client.on("guildCreate", (guild: any) => this.trackGuilds(guild, "create")) this._client.on("guildDelete", (guild: any) => this.trackGuilds(guild, "delete")) } diff --git a/src/test/discordjs/index.ts b/src/test/discordjs/index.ts index a213814..b4802c9 100644 --- a/src/test/discordjs/index.ts +++ b/src/test/discordjs/index.ts @@ -3,6 +3,7 @@ import DiscordAnalytics from "../../discordjs"; import { ActionRowBuilder, Client, IntentsBitField, Interaction, ModalBuilder, TextInputBuilder, TextInputStyle } from "discord.js"; import {config} from "dotenv"; +import {InteractionType} from "../../utils/types"; config() @@ -41,7 +42,15 @@ client.on("ready", async () => { }); client.on("interactionCreate", async (interaction: Interaction) => { - await analytics.trackInteractions(interaction) + await analytics.trackInteractions(interaction, (int) => { + if (interaction.type === InteractionType.ApplicationCommand) + return interaction.commandName + else if (interaction.type === InteractionType.MessageComponent || interaction.type === InteractionType.ModalSubmit) { + if ((/\d{17,19}/g).test(interaction.customId)) return "this_awesome_button" + else return interaction.customId + } + return "" + }) if (interaction.isChatInputCommand()) { if (interaction.commandName === "test") { const option = interaction.options.getString("test"); @@ -53,7 +62,7 @@ client.on("interactionCreate", async (interaction: Interaction) => { type: 2, style: 1, label: "Test button", - custom_id: "✅" + custom_id: `button_${interaction.user.id}` }] }] })