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,aAAa,CAAC,CAAC;AACnI,aAAO,MAAM,OAAO,CAAC,MAAM,gBAAgB,OAAO,aAAa,CAAC;AAAA,IACjE;AAEA,WAAO,UAAU,OAAO,OAAO,MAAM,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,CAAC,GAAG,MAAM,IAAI,GAAG,CAAC;AAAA,EACzF;AAAA,EAEA,MAAc,qBAAqB;AAClC,QAAI,UAAU,OAAO,SAAS,KAAK,SAAS,UAAU;AACrD,YAAM,QAAQ,MAAM,UAAU,OAAO,MAAM,cAAc,CAAC,MAAM,EAAE,OAAO,MAAM,IAAI;AACnF,aAAO,MAAM,OAAO,CAAC,MAAM,gBAAgB,OAAO,aAAa,CAAC;AAAA,IACjE;AAEA,WAAO,UAAU,OAAO,OAAO,MAAM;AAAA,EACtC;AAAA,EAEQ,qBAAqB;AAC5B,WAAO,KAAK,gBAAgB,KAAK,CAAC,GAAG,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC;AAAA,EACzE;AAAA,EAEQ,cAAc;AACrB,WAAO,KAAK,SAAS,aAAa,UAAU,OAAO,MAAM;AAAA,EAC1D;AACD;AArL2C;AAApC,IAAM,WAAN","sourcesContent":["import { fetch, FetchMethods, FetchResultTypes, QueryError } from '@sapphire/fetch';\nimport { container } from '@sapphire/framework';\nimport { totalmem } from 'os';\nimport { currentLoad } from 'systeminformation';\nimport EventEmitter from 'events';\nimport type { Snowflake } from 'discord.js';\n\nimport { type PostStats, type ClientStats, type BucketStats, type UserVotesStats, type StatcordOptions, StatcordEvents } from './types';\n/**\n * The Statcord class.\n * @since 1.0.0\n * @deprecated Statcord and all related discordlabs have been discontinued. This plugin will be discontinued soon.\n */\nexport class Statcord extends EventEmitter {\n\tprivate activeUsers: Snowflake[] = [];\n\tprivate popularCommands: Array<{ name: string; count: number }> = [];\n\tprivate commandsRun = 0;\n\tprivate defaultHeader = { 'content-type': 'application/json', authorization: '' };\n\tprivate bandwidthUsage = '0';\n\tprivate baseUrl = 'https://api.statcord.com/v3';\n\n\tpublic constructor(private readonly options?: StatcordOptions) {\n\t\tsuper();\n\n\t\tif (!this.options?.key) {\n\t\t\tthrow new Error('[Statcord-Plugin]: Statistics will not be sent because no API key has been provided in the Statcord options.');\n\t\t}\n\n\t\tif (this.options.baseUrl) {\n\t\t\tthis.baseUrl = this.options.baseUrl;\n\t\t}\n\n\t\tif (this.options.key) {\n\t\t\tthis.defaultHeader.authorization = this.options.key;\n\t\t}\n\t}\n\n\t/**\n\t * Post the statistics collected from the client to Statcord.\n\t * @since 1.0.0\n\t */\n\tpublic async postStats() {\n\t\ttry {\n\t\t\tconst request = await fetch<PostStats>(\n\t\t\t\t`${this.baseUrl}/stats`,\n\t\t\t\t{\n\t\t\t\t\tmethod: FetchMethods.Post,\n\t\t\t\t\theaders: this.defaultHeader,\n\t\t\t\t\tbody: {\n\t\t\t\t\t\tid: this.options?.client_id ?? container.client.user?.id,\n\t\t\t\t\t\tkey: this.options?.key,\n\t\t\t\t\t\tservers: await this.computeTotalGuilds(),\n\t\t\t\t\t\tusers: await this.computeMembersGuilds(),\n\t\t\t\t\t\tactive: this.activeUsers,\n\t\t\t\t\t\tcommands: this.commandsRun.toString(),\n\t\t\t\t\t\tpopular: this.getPopularCommands(),\n\t\t\t\t\t\tmemactive: process.memoryUsage().heapUsed.toString(),\n\t\t\t\t\t\tmemload: Math.round((process.memoryUsage().heapUsed / totalmem()) * 100).toString(),\n\t\t\t\t\t\tcpuload: Math.round((await currentLoad()).currentLoad).toString(),\n\t\t\t\t\t\tbandwidth: this.bandwidthUsage\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tFetchResultTypes.JSON\n\t\t\t);\n\n\t\t\tif (this.options?.debug) {\n\t\t\t\tcontainer.logger.debug(\n\t\t\t\t\t`[Statcord-Plugin]: Statistics sent correctly > Commands executed: ${this.commandsRun.toLocaleString()} | Active users: ${this.activeUsers.length.toLocaleString()}`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthis.commandsRun = 0;\n\t\t\tthis.popularCommands = [];\n\t\t\tthis.activeUsers = [];\n\t\t\tthis.bandwidthUsage = '0';\n\n\t\t\tif (this.options?.debug) {\n\t\t\t\tcontainer.logger.debug('[Statcord-Plugin]: Resetting of temporary statistics for the new statistics collection');\n\t\t\t}\n\n\t\t\tthis.emit(StatcordEvents.POST_STATS, request);\n\t\t\treturn request;\n\t\t} catch (err: unknown) {\n\t\t\tif (err instanceof QueryError) {\n\t\t\t\tconst responseCloned = err.response.clone();\n\t\t\t\tconst responseParsed = await responseCloned\n\t\t\t\t\t.json()\n\t\t\t\t\t.catch(async () => responseCloned.text())\n\t\t\t\t\t.catch(() => 'Unknow error');\n\n\t\t\t\tthis.emit(StatcordEvents.POST_STATS_ERROR, responseParsed);\n\t\t\t} else {\n\t\t\t\tthis.emit(StatcordEvents.POST_STATS_ERROR, err);\n\t\t\t}\n\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Get current client statistics in Statcord.\n\t * @since 1.0.0\n\t */\n\tpublic async clientStats() {\n\t\ttry {\n\t\t\treturn await fetch<ClientStats>(\n\t\t\t\t`${this.baseUrl}/${this.getClientId()}`,\n\t\t\t\t{ method: FetchMethods.Get, headers: this.defaultHeader },\n\t\t\t\tFetchResultTypes.JSON\n\t\t\t);\n\t\t} catch (_: unknown) {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Check everyone who has voted for the bot today.\n\t * @param days How many days back votes should be fetched for.\n\t * @since 1.0.0\n\t */\n\tpublic async bucketStats(days = 'all') {\n\t\ttry {\n\t\t\treturn await fetch<BucketStats>(\n\t\t\t\t`${this.baseUrl}/${this.getClientId()}/aggregate?days=${days}`,\n\t\t\t\t{ method: FetchMethods.Get, headers: this.defaultHeader },\n\t\t\t\tFetchResultTypes.JSON\n\t\t\t);\n\t\t} catch (_: unknown) {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Check if someone has voted for the bot today.\n\t * @param userId The ID of the user you are checking for.\n\t * @param days How many days back votes should be fetched for.\n\t * @since 1.0.0\n\t */\n\tpublic async userVotesStats(userId: string, days = 1) {\n\t\ttry {\n\t\t\treturn await fetch<UserVotesStats>(\n\t\t\t\t`${this.baseUrl}/${this.getClientId()}/votes/${userId}?days=${days}`,\n\t\t\t\t{ method: FetchMethods.Get, headers: this.defaultHeader },\n\t\t\t\tFetchResultTypes.JSON\n\t\t\t);\n\t\t} catch (_: unknown) {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Set the current bot network usage. It is reset at each statistics post.\n\t * @param usage Bandwidth usage.\n\t * @since 1.0.0\n\t */\n\tpublic setBandwidthUsage(usage: string) {\n\t\tthis.bandwidthUsage = usage;\n\t}\n\n\tpublic postCommand(commandId: string, author: Snowflake) {\n\t\tthis.commandsRun++;\n\t\tif (!this.activeUsers.includes(author)) this.activeUsers.push(author);\n\t\tconst commandData = this.popularCommands.find((c) => c.name === commandId);\n\t\tif (commandData) return commandData.count++;\n\n\t\tthis.emit(StatcordEvents.POST_COMMAND, { id: commandId, author });\n\t\treturn this.popularCommands.push({ name: commandId, count: 1 });\n\t}\n\n\tprivate async computeMembersGuilds() {\n\t\tif (container.client.shard && this.options?.sharding) {\n\t\t\tconst total = await container.client.shard.broadcastEval((c) => c.guilds.cache.reduce((prev, guild) => prev + guild.memberCount, 0));\n\t\t\treturn total.reduce((prev, memberCount) => prev + memberCount, 0);\n\t\t}\n\n\t\treturn container.client.guilds.cache.map((g) => g.memberCount).reduce((a, b) => a + b, 0);\n\t}\n\n\tprivate async computeTotalGuilds() {\n\t\tif (container.client.shard && this.options?.sharding) {\n\t\t\tconst total = await container.client.shard.broadcastEval((c) => c.guilds.cache.size);\n\t\t\treturn total.reduce((prev, memberCount) => prev + memberCount, 0);\n\t\t}\n\n\t\treturn container.client.guilds.cache.size;\n\t}\n\n\tprivate getPopularCommands() {\n\t\treturn this.popularCommands.sort((a, b) => b.count - a.count).slice(0, 5);\n\t}\n\n\tprivate getClientId() {\n\t\treturn this.options?.client_id ?? container.client.user?.id;\n\t}\n}\n"]}