UNPKG

supercell-api-scraper

Version:

A supercell games scraper that has additional calculations and info, easy to use methods. [BETA]

216 lines (215 loc) 6.71 kB
/** @format */ import BaseGame from './BaseGame.js'; import SupercellError from './SupercellError.js'; export default class ClashOfClans extends BaseGame { constructor(client) { super(client); if (!this.client.options.ClashOfClans) throw new SupercellError('No token was provided, you can generate a token on the official developer site of Clash of Clans here: https://developer.clashofclans.com/#/new-key'); this.clans = { leagueGroup: (tag) => this.leagueGroup(tag), leagueWar: (tag) => this.leagueWar(tag), warlog: (tag) => this.warlog(tag), find: (name) => this.find(name), currentWar: (tag) => this.currentWar(tag), get: (tag) => this.clan(tag), members: (tag) => this.members(tag), capitalRaidSeasons: (tag) => this.capitalRaidSeasons(tag), }; this.players = { find: (tag) => this.player(tag), }; this.leagues = { capitalLeagues: this.capitalLeagues, capitalLeague: this.capitalLeague, leagues: this.idk2, get: this.league, leagueSeason: this.leagueSeason, season: this.season, warLeague: this.warLeague, warLeagues: this.warLeagues, }; this.rankings = { clans: this.clanRankings, players: this.playerRankings, locations: this.locations, location: this.location, }; this.goldpass = { current: this.currentSeason, }; this.esports = {}; this.labels = { players: this.playerLabels, clans: this.clanLabels, }; } // Clans /** * Retrieve information about a clan's current clan war league group. * @param {string} tag * @returns something */ async leagueGroup(tag) { return await this.cocFetch('clans', { tag, subdomain: 'currentwar/leaguegroup' }); } /** * Retrieve information about individual clan war league war. * @param {string} tag * @returns something */ async leagueWar(tag) { return await this.cocFetch('clanwarleagues', { predomain: 'wars', tag }); } /** * Retrieve clan's clan war log. * @param {string} tag * @returns something */ async warlog(tag) { const response = await this.cocFetch('clans', { tag, subdomain: 'warlog', }); return response?.items || response; } /** * Search clans by name. * @param {string} name * @returns something */ async find(name) { // TODO - Add all query options. const response = await this.cocFetch('clans', { name }); return response?.items || response; } /** * Retrieve information about the clan's current clan war * @param {string} tag * @returns something */ async currentWar(tag) { return await this.cocFetch('clans', { tag, subdomain: 'currentwar' }); } /** * Get clan information * @param tag * @returns something */ async clan(tag) { const clan = await this.cocFetch('clans', { tag }); if (!clan?.tag) return clan; const modifiedClan = this.modifyClan(clan); return modifiedClan; } /** * List clan members. * @param {string} tag * @returns something */ async members(tag) { const response = await this.cocFetch('clans', { tag, subdomain: 'members' }); return response?.items || response; } /** * Retrieve clan's capital raid seasons * @param {string} tag * @returns something */ async capitalRaidSeasons(tag) { const response = await this.cocFetch('clans', { tag, subdomain: 'capitalraidseasons' }); return response?.items || response; } // Players /** * * @param {string} tag * @returns something */ async player(tag) { return await this.cocFetch('players', { tag }); } // Leagues async capitalLeagues() { } async idk2() { } // leagues async leagueSeason() { } async capitalLeague() { } async league() { } async season() { } async warLeague() { } async warLeagues() { } // Rankings async clanRankings() { } async playerRankings() { } async locations() { } async location() { } // Goldpass async currentSeason() { } // Esports // Labels async playerLabels() { } async clanLabels() { } // Util /** * * @param domain * @param options * @returns */ async cocFetch(domain, options) { return await this.fetch('Clash of Clans', domain, options); } async modifyClan(clan, baseClan = false) { const tag = clan.tag; const warlog = await this.warlog(tag), capitalRaidSeasons = (await this.capitalRaidSeasons(tag)); const modifiedClan = { profile: { tag: clan.tag, name: clan.name, description: clan.description, location: clan.location, language: clan.chatLanguage, type: clan.type, friendly: clan.isFamilyFriendly, badgeURLs: clan.badgeUrls, }, stats: { level: clan.clanLevel, }, homeVillage: { trophies: clan.clanPoints, requiredTrophies: clan.requiredTrophies, requiredTownhall: clan.requiredTownhallLevel, }, builderVillage: { trophies: clan.clanVersusPoints, requiredTrophies: clan.requiredTrophies, }, capitalVillage: { capitalHall: clan.clanCapital.capitalHallLevel, districts: clan.clanCapital.districts, trophies: clan.clanCapitalPoints, league: clan.capitalLeague, log: capitalRaidSeasons, }, war: { frequency: clan.warFrequency, wins: clan.warWins, ties: clan.warTies, losses: clan.warLosses, publicLog: clan.isWarLogPublic, league: clan.warLeague, log: warlog, }, members: { count: clan.members, list: clan.memberList, }, labels: clan.labels, }; if (baseClan) modifiedClan.baseClan = clan; return modifiedClan; } }