This sample bot project aims to help you get started with interacting Clash of Clans API using discord.js
and clashofclans.js
.
- 📝 Databse integration (PostgreSQL)
- ✍️ Fully typechecked code
- 🚀 3 commands and 2 events
- 😎 It just works
More thing around, figure out on yourself 🤷♂️
Before you dive into running this project, I expect you to have these things ready:
- NodeJs (version>=16)
- Postgresql installed locally and ruuning
- git (This is required to clone the project on your local machine)
- Account on Clash of Clans API
- Bot account on Discord
After completing the above steps, do invite the bot to your server.
- clashofclans.js Documentation
- Clash of Clans Developer Website
- Clash of Clans API Community Discord
- Discord Developer Website
- discord.js Documentation
- discord.js Guide
- Prisma ORM docs
- Clone the project using
git clone https://github.com/r-priyam/cocjs-smaple-bot.git
- Install dependencies using
npm install
- Make
.env
file in the project root directory and set the following values:DATABASE_URL
: Your postgres database connection stringclashEmail
- Clash of Clans API account email addressclashPassword
- Password for the account ofCLASH_EMAIL
projectName
- Your project namebotClientId
- Bot client idtestGuildId
- Guild id of the test guild in which bot was addedbotToken
- Discord bot token of your bot account created on DiscordclanTags
: Clan tags separated by comma to be added to theclashofclans.js
for receiving clan events.memberReportingChannelId
: Channel id of the channel where member join/leave reporting is to be done.
- Synchronize the Application commands(Slash commands) with the test guild using
npm run sync:commands
. This will add the commands to the test guild. Please note that it takes sometime to cache the commands in guild but not much when we are adding commands specifically for guild only. Have some patience. - Run the bot using
npm run start
.
Once the bot is running and slash commands are reflecting in the test server then you can try running the commands.
/player
- To get player information/clan
- To get clan information/war
- To get clan war information/link
- To link a clan or a player to your discord account.
Tag is an optional parameter in /player
, /clan
and /war
commands. If you don't provide it then it will attempt to get the respective tag information from the database for the user who sent the command and will return the data for the first tag found in the database.
If you provide the tag then it will attempt to query the data directly from the API and will return the data for the tag provided.
-
Q: Why it requires my Clash of Clans API account credentials?
-
A: It is required to get the access token to interact with clash of clans api. If you don't want to give your account credentials, you can use the Clash of Clans API website directly to get the access token and then you can setup the
CLASH_TOKEN
environment variable. It will then require you do some extra steps to get theclasofclans.js
to work. To use API token diectly do these changes in the following files -In
index.ts
- client.coc = new ClashClient({ cache: true }); + client.coc = new ClashClient({ keys: [config.CLASH_TOKEN], cache: true });
In
utils/EnvValidator.ts
interface EnviromentVariables { // ... + CLASH_TOKEN: string; } const envVarsSchema = joi .object() .keys({ // ... + CLASH_TOKEN: joi.string().required(), }) .unknown();
-
-
Q: Why adding commands to the test guild only and not globally?
- A: When adding commands globally then it takes at least 1 hour ~ to cache commands on discord side and then reflect in the servers in which bot is added into
but not much when we are adding commands specifically for guild only
. So, it is better to add commands to the test guild only when developing.
- A: When adding commands globally then it takes at least 1 hour ~ to cache commands on discord side and then reflect in the servers in which bot is added into
-
Q: How can I add commands globally?
- A: Please read discordjs guide here and follow the steps to add commands globally.
-
Q: How can I add more commands?
- A: Please read discordjs guide here and follow the steps to add more commands.
If you have any questions or suggestions, please feel free to open an issue here.