← Back
Cards Icon

Clash Royale Discord Bot

A Java Discord bot helping Clash Royale players trade cards.

This post goes a bit in-depth about a personal project I contributed to in 2019. This is a Discord bot built using Java, storing user-related information using MySQL and scraping from a public REST API. By helping build this bot, I gained some technical experience in my first ever non-class project.

Understanding what this bot actually does requires some context about the game and the targeted use cases, so... I did the best I could to not make this too long lol


Preface

Back in 2019, I played a mobile game called Clash Royale, a 1v1 strategy and card collecting game. You build your deck of cards to battle against opponents, and you loot cards to level them up. You can join clans, where players can request and donate cards to help each other level up their decks.

Around this time, they introduced a new feature called Trade Tokens. Players can obtain these tokens to request a card they want to receive, and offer cards to give in exchange. Other clan members can use a token to accept these trades. This helped speed up progression for players to level up their decks, while trading away cards they weren't using.

CR Card Trade

The Problem: Communication

Initiating a trade means that you're hoping another player has a sufficient quantity of the card you want (and that they don't need that same card for any reason), while also wanting one of the cards you're offering. Sometimes it's difficult to meet those conditions with players in your clan. There's no way in-game to know what cards your clanmates have in their collections, and there's no way to know what cards your clanmates want without asking around. So to have the best trading experience, there needs to be plenty of communication.

Some clans may not have the best communication going around. This is a mobile game with varying levels of commitment, and these interactions to find optimal trades within the clan depends on how often the players log in. There can be players that treat this game casually and log in only once a day, or maybe even once a week! So they won't check the clan chat enough to see these trades or related interactions.

The usual behavior from players is that when they get tokens, they post trades that offer random cards they don't need. This becomes a problem when these trades never get accepted, due to a lack of communication. Consider this example where a player (King Hugo XVI) throws up a counteroffer, because they want to give the card but want a different card in exchange:

CR Counter Offer

SmashAddict wouldn't accept this trade if he:

  • doesn't have enough of the card that King Hugo XVI wants

  • doesn't want to give away the card that King Hugo XVI wants

  • doesn't log in to see this counteroffer

These cases happened a lot in my clan, to the point where one of my clanmates decided to develop something to help this situation. And this was while I was in my first year of CS at UT Austin, so it was a chance for exposure to a programming project outside of classes.

Solution: Using a Public API

It's not possible to see other players' card collections in-game. But thankfully, there was a public API for the game that revealed statistics about players, including what cards they had in their collection. (They discontinued the public API in 2020, but the API data is still available on their site.)

Developers could use the provided endpoints and the tags associated with players to retrieve their stats and card collections. The API also shows what clan the player is in, which also has a tag, and you can retrieve all of the players in that clan using the tag.

Using a Discord bot, we could create a command to retrieve all members in your clan, go through their card collections, and see if they have a card that you want. This is the whoHas <card> command. Here's an example:

CR whoHas

It lists all the members who has enough of that card to give away. If they don't have enough, they're not listed. With this information, you can ask members on this list if they're willing to do a trade with you and give this card.

Notice that there's one name labeled in red: that's because they indicated that they want this card themselves, and they added it to their wish list. Which brings me to my next point...

Solution: Storing Wish Lists

This is where the MySQL database comes in. Players using this bot can indicate which cards they want by creating a wish list and adding cards to it. We used MySQL to map users of the bot to the wish lists they created. So when players are looking for trades involving certain cards, anyone who has the card on their wishlist will be marked as not wanting to trade it away.

Here is an example of someone adding a card to their wish list, then viewing their wish list:

CR wishlist

So now, if someone is looking for trades involving The Log, Mcman will not appear as a potential candidate for those trades.

We took these two concepts (finding who has a card, and wish list management) and created more advanced commands that made it easier for players to find trades. For example, the findMeTrades command uses all of this information to list members of your clan who have a card that you want, and want a card that you have! It's basically a matchmaking command that reveals the most beneficial trades.

Here's a visual that I made myself to promote this bot, showcasing its features:

CR Trade Bot promotion

And here are the usage stats of the bot back in September 2019, provided by the main developer, Eitri Ymir. 80+ Discord servers for clans were interested in using this bot to help facilitate trades between clan members.

CR bot stats

I was very interested in working on this project because of its potential to help ease the trading process for some clans, and because it was a problem I faced often myself (as a clan leader). It's not quite the perfect solution, but it's a great tool to help reduce the problem. The shortcoming of this bot is that it's still user-driven, as it's the players' decisions to create and maintain their wish lists. Depending on how active your clan is and how many people use the bot, you may not get the most benefit from it. It still has useful features that don't depend on players using the bot, such as scraping card collections from your clan members using the API.