Build Discord Bot using Bitquery APIs

Today we will create a Discord bot that provides the user with the updated price of CAKE/USDT using Bitquery APIs.

Discord offers free VoIP, text, and video chat servers, along with unique features for gamers like Twitch integration and a document-sharing facility. It’s also a home for many crypto communities.

Using Bitquery APIs you can create discord bots providing various blockchain data updates.

Today we will build such a bot that provides the latest price of CAKE/USDT. It’s just an example to show how you can use our API to build a Discord bot, but you can use it for any type of data feed available with our APIs.

Note: You can get the Github link for the complete code at the end of the tutorial.

Sign up to Bitquery.io

If you are new to Bitquery’s interface, go to GraphQL IDE.

From here, sign up with your credentials. You might get a confirmation email from your registered email address.

After you are signed up, you would be redirected to GraphQL IDE from where you can get your unique API key.

Considering that you already have a DISCORD account, we will be building a discord bot using Python.

Creating a new Discord server

After logging in to your Discord account, you’ll see a similar user dashboard

You may or may not already know how to create a server but for the sake of the writer’s convenience, we’ll start off by creating a fresh Discord server.

When you are redirected to the main user dashboard of Discord, you might notice a ‘+’ sign. When clicked, it creates a new server in Discord as shown

Click on the CREATE MY OWN section or you can select from the templates as mentioned above.

You can either create your server for your personal use or create it for a community or a group of individuals from your organization. In our case, we chose to choose a server for a community.

Then, you’ll be asked to name your server as shown below.

After creating your server, we need to add an application that would eventually help us in creating a bot for our Discord server. For that, we need to go to Discord Developer Portal. You’ll see a similar UI when the site loads.

Next, click on New Application and you’ll see a modal popup similar to the below image which would ask you the name of your application.

After naming the application, click on Create and you’ll be redirected to the following page

From here, you need to click on Bot from the left menu bar.

Click on the Add Bot button which would add the functionalities of the bot to the application.

Click on Yes, do it! Button. You’ll see a similar message in your application UI as shown below.

Make sure to check that your Bot is public

From here, click on the OAuth2 from the left menu bar

Select the permissions of your bot according to your requirements and then, in the Scope section, select BOT.

Now, copy the link as shown above and paste it into a new tab of your browser. You’ll be redirected to a site similar to below

Add the name of the server, you want to add the bot to from the dropdown menu and press Continue when done.

After clicking on Continue, you’ll see a similar modal popup as shown below

This lists all the permissions you have given to your bot (Yours might be a bit different based upon your requirements). Then, click on the Authorize button.

If you see the above screen, that implies that your bot has integrated itself with your Discord server. You can confirm by checking your server.

You will see that your bot with an offline status gets added to your server.

Adding functionalities to your Discord bot and integrating Bitquery GraphQL API

Now, we’ll be adding functionalities to our Discord bot using Python and we’ll be integrating Bitquery GraphQL API with the same. Before starting with adding functionalities to the Discord bot, we need the secret Bot Token Key which we can generate from Discord Developer Portal.

Click on the copy button and you’ll see the button turn green and the button text will change to copied. Now that’s when you know that you have successfully copied the secret token key. We’ll save this token key in our project by creating a .env file in the root directory as shown below

Before adding the functionalities to our Discord Bot, we’ll integrate the Bitquery GraphQL API with it. Assuming that we have already signed up to Bitquery.io and generated our unique GraphQL API key from https://graphql.bitquery.io/, we’ll set another environment variable for the same.

Now, we’ll import the necessary libraries into our python code and create an instance of discord.Client() in our code as shown below

The requests library would help us take requests from the Bitquery GraphQL API and the Discord library would help us integrate and add functionalities to our Discord bot using Python.

Then, we’ll store the value of the API key generated from Bitquery GraphQL API in a separate variable as shown below

Now, before we create a function that takes a Graphql query as one of the parameters and integrates the Bitquery GraphQL API with our code, we need to make a graphQL query as shown below

Now, we’ll make the function that integrates Bitquery GraphQL API with our code and takes the above argument as one of the vital parameters

Store the result of the query and the price (as required) in separate variables for the sake of convenience

We’ll now create a python callback that logs the name of the bot client in the terminal

Then, we’ll make a set of replies and questions, our bot would be able to resolve and reply and would be able to inform the user about the latest price of CAKE/USDT using the quotePrice variable’s value in API result.

The code for the same is shown below

Finally, in order to keep our Discord Bot running we need to pass the secret Discord Bot Token from the .env file into our main code using os.getenv(‘TOKEN’) function as shown

To ensure that our bot is running, we need to start the terminal from the project’s root directory using python main.py command or if you have used repl.it for writing the code, you can simply click on the RUN button. To ensure that our Discord bot is running correctly, we can check by going into the server and checking whether the Discord bot is online or not. If it is online, that means the bot is up & running.

Resources

Link to the Github repository for this project

Link to the gists

Link to the Discord Bot run

1 Like