Skip to content

Commit f7d19ad

Browse files
feat: Add interaction name resolver to trackInteractions and trackEvents methods
1 parent f71f89c commit f7d19ad

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

src/discordjs/index.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/test/discordjs/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import DiscordAnalytics from "../../discordjs";
44
import { ActionRowBuilder, Client, IntentsBitField, Interaction, ModalBuilder, TextInputBuilder, TextInputStyle } from "discord.js";
55
import {config} from "dotenv";
6+
import {InteractionType} from "../../utils/types";
67

78
config()
89

@@ -41,7 +42,15 @@ client.on("ready", async () => {
4142
});
4243

4344
client.on("interactionCreate", async (interaction: Interaction) => {
44-
await analytics.trackInteractions(interaction)
45+
await analytics.trackInteractions(interaction, (int) => {
46+
if (interaction.type === InteractionType.ApplicationCommand)
47+
return interaction.commandName
48+
else if (interaction.type === InteractionType.MessageComponent || interaction.type === InteractionType.ModalSubmit) {
49+
if ((/\d{17,19}/g).test(interaction.customId)) return "this_awesome_button"
50+
else return interaction.customId
51+
}
52+
return ""
53+
})
4554
if (interaction.isChatInputCommand()) {
4655
if (interaction.commandName === "test") {
4756
const option = interaction.options.getString("test");
@@ -53,7 +62,7 @@ client.on("interactionCreate", async (interaction: Interaction) => {
5362
type: 2,
5463
style: 1,
5564
label: "Test button",
56-
custom_id: "✅"
65+
custom_id: `button_${interaction.user.id}`
5766
}]
5867
}]
5968
})

0 commit comments

Comments
 (0)