diff --git a/src/classes/Standardizer/plugins/Discord/getters/getCommunitiesStatistics.ts b/src/classes/Standardizer/plugins/Discord/getters/getCommunitiesStatistics.ts new file mode 100644 index 00000000..9f9e674a --- /dev/null +++ b/src/classes/Standardizer/plugins/Discord/getters/getCommunitiesStatistics.ts @@ -0,0 +1,38 @@ +import Discord from '../Discord' +import { StatisticType } from '../../../../../types/schemas/Statistic' + +const fs = require('fs'); + +const COMMUNITIES_FOLDER = '/servers/'; + +function getCommunitiesIds(path : string) { + return fs.readdirSync(path, { withFileTypes: true }) + .filter((dirent : any) => dirent.isDirectory()) + .map((dirent : any) => dirent.name); +} + +function isServerAdmin(communityFolderPath : string) : Boolean { + const files = fs.readdirSync(communityFolderPath); + return files.length > 2 +} + +Discord.prototype.getCommunitiesStatistics = async function getCommunitiesStatistics() { + const target = this.path + COMMUNITIES_FOLDER; + const folders = getCommunitiesIds(target); + + let count = 0 + folders.forEach((folder : string) => { + count += isServerAdmin(`${ target }/${ folder }`) ? 1 : 0 + }); + + return { + statistics: [ + { + type: StatisticType.NUMBER, + value: count, + name: 'Number of server where the user is admin.', + }, + ], + parsedFiles: [], + } +}