A simple Node.js module designed to streamline interactions with the Clash of Clans API.
- Promise-based for asynchronous handling
- Fully object-oriented for intuitive usage
- Predictable abstractions for ease of development
- Fully typed with JSDoc specifications for improved code quality and developer experience
- Automatic token creation and deletion for if you don't have a static IP address
- Node.js 20.13.1 or higher (previous versions may work but are not tested)
Install package via NPM:
npm i clash.js
Here's some basic examples of how to interact with clash.js:
import { Client } from 'clash.js'
You can import by destructuring or by default import (eg:
import Client from 'clash.js'
).
(async () => {
const client = new Client({
email: '[email protected]',
password: '*'
})
Replace
[email protected]
with your email, and*
with your password for your Clash of Clans API developer account.
const player = await client.getPlayer('#ABCDEFG')
Replace
#ABCDEFG
with your player tag of choice. You could even provide a tag without a hashtag and lower-case characters (eg:ab1d23e
) and it would resolve (eg:#AB1D23E
).
if (player) {
const barbarianKing = player.getHero('Barbarian King')
Look up a player's hero from their class object (parameter fully typed), or you could view a whole array of their heroes with the
heroes
data member ofPlayer
.
const playerClan = await player.getClan()
You can even retrieve a player's full clan from their class object without passing a tag as a parameter.
if (playerClan) {
const capital = playerClan.capital
const golemQuarry = capital?.getDistrict('Golem Quarry')
}
Look up a capital's district from it's class object (parameter fully typed).
}
const clan = await client.getClan('#ABCDEFG')
You can also get a clan by providing a clan tag to the
getClan
member function ofClient
.
const war = await client.getWar('#ABCDEFG')
You can get a clan's current war by providing a clan tag to the
getWar
member function ofClient
.
if (war) {
const ally = war.ally
const allyMembers = ally.members
const enemy = war.enemy
const enemyMembers = enemy.members
const firstAllyMember = allyMembers[0]
const bestEnemyAttack = firstAllyMember.bestEnemyAttack
}
You can access members, attacks, and more with the
War
object.
const clans = new Clans(client)
.add('#ABCDEFG')
.set('description', (oldClan, newClan) => oldClan.description !== newClan.description)
clans.on('description', (oldClan, newClan) => {
console.log(`${newClan.name} changed their description from '${oldClan.description}' to '${newClan.description}'`)
})
const players = new Players(client)
.add('#ABCDEFG')
.set('barbarian-king-upgrade', (oldPlayer, newPlayer) => oldPlayer.getHero('Barbarian King')?.level < newPlayer.getHero('Barbarian King')?.level )
players.on('barbarian-king-upgrade', (oldPlayer, newPlayer) => {
console.log(`${newPlayer.name}'s Barbarian King upgraded to level ${newPlayer.getHero('Barbarian King')?.level}`)
})
You can listen to events by using the Clans and Players polling managers.
})()
Use clash.js in your Clash of Clan's project workflow and let your intellisense thank you later!