Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/discordjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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"))
}
Expand Down
13 changes: 11 additions & 2 deletions src/test/discordjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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");
Expand All @@ -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}`
}]
}]
})
Expand Down