Building a game search Chatbot for Discord

Discord is currently one of the fastest growing chat communities all over the world.

With 250 million users accounts and an average of 15 active million users daily it is becoming the platform of choice for a staggering amount of communities.

Xenioo just added full support for Discord as a brand new channel! In this article we will see in detail how we could build a simple game search bot that fully integrates in any Discord server.

Designing our Discord Bot

The target functionality for our chatbot would be:

  • Wait for a trigger keyword from any user on a channel
  • Search game information on an API service
  • Present our results to the group

Thanks to Xenioo multi-channel designer, designing our Discord chatbot is exactly like designing any other chatbot so let’s start by the keyword trigger.

What we want is to catch any sentence starting with &search so that our chatbot is invoked only on specific requests by our users.

The above screenshot shows how this is easily done by simply using a Global Detection based on a Text Expression.

We could push it further and train our AI to detect free text search but a simple keyword is perfect for our current example.

As the keyword is detected we want to extract only the part of the search that interests us. To do that we simply remove the &search keyword and trim spaces from the final text.

The last message we added is just a small test we can use to check that our user_search variable really contains only the text for our lookup.

Integrating with a gaming API service

Now that we have the search text for our user we need a gaming API service that can provide us with information. For our example we picked RAWG, one of the top players in this business.

After acquiring an API Key for their service we need to integrate the URL method in Xenioo by using the standard API Action in our design.

Our action will invoke the RAWG service submitting the user search and will store the resulting data into the search_results variable.

From there, using Xenioo Dynamic Parsing we can immediately check if we received any result.

As you can see in our variable check we can directly refer to JSON variables as they were properties of our search_results variable: the count field, contained in the json can be specified simply as search_results.count.

The same way we notify the user with zero results we want to make sure that search is sufficiently refined.

Let’s add add another Variable Switch Action that checks if there are too many games found.

If our search results are in an acceptable range we display a result card to our user with a nice screenshot and some additional details.

Again, here we use direct Dynamic Parsing to refer to the model entry at index 0 (zero) without using any code at all.

Discord does not support carousel cards but Xenioo will automatically translate the content to the next best thing: Discord Embedded Content.

A nice thing about Embedded Content is that it supports a nice view of multiple values that can be used to a good effect here and is fully supported by Xenioo.

Now that was quick! Our chatbot is ready and all we need to do now is publishing it on Discord.

Creating a Discord Bot

Creating a bot on Discord is a very simple process made of a few steps.

First of all we need, of course, a Discord account and with that login to the Discord Developers Portal. Once we’ve logged to our account we just need to create a new Application. Give it any name: it won’t be the name of the bot on Discord.

After the application is created just click on the “Bot” menu option and create a new bot.

This time give it a name you like as it will be the name that all users on the channel will see. Right below the bot name you’ll see the Token area: click on the copy button and go back to Xenioo.

On Xenioo, click on the publish button and the select discord. On the Auth Token field paste the token you just copied from the Discord bot page.

Since we’re on this page it would be a good idea to add a message filter for this bot.

Why? Discord bots can be added on channels with hundreds of thousands of users and it wouldn’t be of any use to just parse every message: we can just get any message starting with a & (since our only command now is &search).

Save and click on publish. Your bot is online! Time to invite the bot to your channel.

Inviting the Discord chatbot

Your chatbot can live in any number of channels and Discord approach to bot is very interesting: you share an invite url for the bot that anyone can visit. As they visit the link they’re presented with the option to add your chatbot to their server.

To build our invite link let’s go back to the Discord application page and select OAuth2 on the left menu. Scroll to the bottom in the scopes section click on “bot”.

An additional permissions area will open up. Select the basic permission for our bot following the ticks below:

Your invite link is now ready. Click on copy and share your bot to the world!

Wrap Up

We’ve just seen how easy it is to integrate Xenioo new Discord channel and create awesome bots with it.

Do you have any question? Do you want to let us help on your awesome bot idea? Be sure to get in touch with the Xenioo team at team@xenioo.com or leave a message on our Facebook Group.

Further reading