@discordanalytics/discordjs 2.8.2 → 2.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js DELETED
@@ -1,175 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const core_1 = require("@discordanalytics/core");
7
- const package_json_1 = __importDefault(require("../package.json"));
8
- /**
9
- * @class DiscordAnalytics
10
- * @description The Discord.js class for the DiscordAnalytics library.
11
- * @param {AnalyticsOptions} options Configuration options.
12
- * @property {any} options.client The Discord.js client to track events for.
13
- * @property {string} options.api_key The API token for DiscordAnalytics.
14
- * @property {boolean} options.sharded Whether the Discord.js client is sharded.
15
- * @property {boolean} options.debug Enable or not the debug mode /!\ MUST BE USED ONLY FOR DEVELOPMENT PURPOSES /!\
16
- * @example
17
- * const { default: DiscordAnalytics } = require('discord-analytics/discordjs');
18
- * const { Client, IntentsBitField } = require('discord.js');
19
- * const client = new Client({
20
- * intents: [IntentsBitField.Flags.Guilds]
21
- * })
22
- * client.on('ready', () => {
23
- * const analytics = new DiscordAnalytics({
24
- * client: client,
25
- * api_key: 'YOUR_API_TOKEN'
26
- * });
27
- * analytics.init();
28
- * analytics.trackEvents();
29
- * });
30
- * client.login('YOUR_BOT_TOKEN');
31
- * @link https://discordanalytics.xyz/docs/main/get-started/installation/discord.js Check docs for more informations about advanced usages
32
- */
33
- class DiscordAnalytics extends core_1.AnalyticsBase {
34
- _client;
35
- _sharded = false;
36
- _isReady = false;
37
- constructor(options) {
38
- super(options.api_key, options.debug);
39
- this._client = options.client;
40
- this._sharded = options.sharded || false;
41
- }
42
- /**
43
- * Initialize DiscordAnalytics on your bot
44
- * /!\ Advanced users only
45
- * /!\ Required to use DiscordAnalytics
46
- * /!\ Must be used when the client is ready (recommended to use in ready event to prevent problems)
47
- */
48
- async init() {
49
- if (process.env.NODE_ENV !== 'production')
50
- return console.log("[DISCORDANALYTICS] NODE_ENV != 'production', initialization skipped");
51
- const url = core_1.ApiEndpoints.EDIT_SETTINGS_URL.replace(':id', this._client.user.id);
52
- const body = JSON.stringify({
53
- username: this._client.user.username,
54
- avatar: this._client.user.avatar,
55
- framework: 'discord.js',
56
- version: package_json_1.default.version,
57
- team: this._client.application.owner
58
- ? this._client.application.owner.hasOwnProperty('members')
59
- ? this._client.application.owner.members.map((member) => member.user.id)
60
- : [this._client.application.owner.id]
61
- : [],
62
- });
63
- await this.api_call_with_retries('PATCH', url, body);
64
- this.debug('[DISCORDANALYTICS] Instance successfully initialized');
65
- this.client_id = this._client.user.id;
66
- this._isReady = true;
67
- const fast_mode = process.argv[2] === '--fast';
68
- this.debug(`[DISCORDANALYTICS] Fast mode is ${fast_mode ? 'enabled' : 'disabled'}. Stats will be sent every ${fast_mode ? '30s' : '5min'}.`);
69
- setInterval(async () => {
70
- this.debug('[DISCORDANALYTICS] Sending stats...');
71
- const guildCount = this._sharded
72
- ? ((await this._client.shard?.broadcastEval((c) => c.guilds.cache.size))?.reduce((a, b) => a + b, 0) || 0)
73
- : this._client.guilds.cache.size;
74
- const userCount = this._sharded
75
- ? ((await this._client.shard?.broadcastEval((c) => c.guilds.cache.reduce((a, g) => a + (g.memberCount || 0), 0)))?.reduce((a, b) => a + b, 0) || 0)
76
- : this._client.guilds.cache.reduce((a, g) => a + (g.memberCount || 0), 0);
77
- const userInstallCount = this._sharded
78
- ? ((await this._client.shard?.broadcastEval((c) => c.approximateUserInstallCount))?.reduce((a, b) => a + b, 0) || 0)
79
- : this._client.approximateUserInstallCount;
80
- const guildMembers = !this._sharded
81
- ? this._client.guilds.cache.map((guild) => guild.memberCount)
82
- : ((await this._client.shard?.broadcastEval((c) => c.guilds.cache.map((guild) => guild.memberCount)))?.flat() ?? []);
83
- let guildLocales = [];
84
- this._client.guilds.cache.map((current) => guildLocales.find((x) => x.locale === current.preferredLocale) ?
85
- ++guildLocales.find((x) => x.locale === current.preferredLocale).number :
86
- guildLocales.push({ locale: current.preferredLocale, number: 1 }));
87
- this.stats_data.guildsLocales = guildLocales;
88
- await this.sendStats(this._client.user.id, guildCount, userCount, userInstallCount, guildMembers);
89
- }, fast_mode ? 30000 : 300000);
90
- }
91
- /**
92
- * Track interactions
93
- * /!\ Advanced users only
94
- * /!\ You need to initialize the class first
95
- * @param interaction BaseInteraction class and its extensions only
96
- * @param interactionNameResolver A function that will resolve the name of the interaction
97
- */
98
- async trackInteractions(interaction, interactionNameResolver) {
99
- this.debug(`[DISCORDANALYTICS] trackInteractions(${interaction.type}) triggered`);
100
- if (!this._isReady)
101
- return this.error(core_1.ErrorCodes.INSTANCE_NOT_INITIALIZED);
102
- this.updateOrInsert(this.stats_data.locales, (x) => x.locale === interaction.locale, (x) => x.number++, () => ({ locale: interaction.locale, number: 1 }));
103
- if (interaction.type === core_1.InteractionType.ApplicationCommand) {
104
- const commandType = interaction.command
105
- ? interaction.command.type
106
- : core_1.ApplicationCommandType.ChatInputCommand;
107
- const commandName = interactionNameResolver
108
- ? interactionNameResolver(interaction)
109
- : interaction.commandName;
110
- this.updateOrInsert(this.stats_data.interactions, (x) => x.name === commandName
111
- && x.type === interaction.type
112
- && x.command_type === commandType, (x) => x.number++, () => ({
113
- name: commandName,
114
- number: 1,
115
- type: interaction.type,
116
- command_type: commandType,
117
- }));
118
- }
119
- else if (interaction.type === core_1.InteractionType.MessageComponent || interaction.type === core_1.InteractionType.ModalSubmit) {
120
- const interactionName = interactionNameResolver
121
- ? interactionNameResolver(interaction)
122
- : interaction.customId;
123
- this.updateOrInsert(this.stats_data.interactions, (x) => x.name === interactionName && x.type === interaction.type, (x) => x.number++, () => ({
124
- name: interactionName,
125
- number: 1,
126
- type: interaction.type,
127
- }));
128
- }
129
- this.updateOrInsert(this.stats_data.guildsStats, (x) => x.guildId === (interaction.guild ? interaction.guild.id : 'dm'), (x) => x.interactions++, () => ({
130
- guildId: interaction.guild ? interaction.guild.id : 'dm',
131
- name: interaction.guild ? interaction.guild.name : 'DM',
132
- icon: interaction.guild && interaction.guild.icon ? interaction.guild.icon : undefined,
133
- interactions: 1,
134
- members: interaction.guild ? interaction.guild.memberCount : 0,
135
- }));
136
- const oneWeekAgo = new Date();
137
- oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
138
- if (!interaction.inGuild() || !interaction.guild)
139
- ++this.stats_data.users_type.private_message;
140
- else if (interaction.member
141
- && interaction.member.permissions
142
- && interaction.member.permissions.has(8n)
143
- || interaction.member.permissions.has(32n))
144
- ++this.stats_data.users_type.admin;
145
- else if (interaction.member
146
- && interaction.member.permissions
147
- && interaction.member.permissions.has(8192n)
148
- || interaction.member.permissions.has(2n)
149
- || interaction.member.permissions.has(4n)
150
- || interaction.member.permissions.has(4194304n)
151
- || interaction.member.permissions.has(8388608n)
152
- || interaction.member.permissions.has(16777216n)
153
- || interaction.member.permissions.has(1099511627776n))
154
- ++this.stats_data.users_type.moderator;
155
- else if (interaction.member
156
- && interaction.member.joinedAt
157
- && interaction.member.joinedAt > oneWeekAgo)
158
- ++this.stats_data.users_type.new_member;
159
- }
160
- /**
161
- * Let DiscordAnalytics declare the events necessary for its operation.
162
- * /!\ Not recommended for big bots
163
- * /!\ Not compatible with other functions
164
- * @param interactionNameResolver A function that will resolve the name of the interaction
165
- */
166
- trackEvents(interactionNameResolver) {
167
- this.debug('[DISCORDANALYTICS] trackEvents() triggered');
168
- if (!this._isReady)
169
- return this.error(core_1.ErrorCodes.INSTANCE_NOT_INITIALIZED);
170
- this._client.on('interactionCreate', async (interaction) => await this.trackInteractions(interaction, interactionNameResolver));
171
- this._client.on('guildCreate', (guild) => this.trackGuilds('create'));
172
- this._client.on('guildDelete', (guild) => this.trackGuilds('delete'));
173
- }
174
- }
175
- exports.default = DiscordAnalytics;