Discord chatbot using Dialogflow

Ishita Chauhan
5 min readFeb 17, 2022

--

First, let us understand what is a chatbot?

Usually, when you are having a conversation, you are talking to another human. But with chatbots, you are not talking to a human, but to a computer that can automatically talkback based on your questions and answers.

Why do we need a chatbot on discord?

There are millions of discord users who are sending millions of messages on discord each day and it takes a lot of time to answer each message. But, by using chatbots you can :

  1. automatically reply to standard questions like “Can I have your gaming cheatsheet? ”, “Play Music”.
  2. Even if you are not actually online it can answer questions.

Let’s get started!

1. Setup the discord bot

To set up your bot click here. Click on New Application in the top right corner and give any cool name of your choice to your bot.

Now go to the bot section in the menu on the left-hand side, and add a new bot.

Now you should be greeted with this screen.

Now create your own server on discord and Visit here to set the permissions of your bot and get the link to add your bot to your server. Select these permissions. Go back to the discord dev page again and go to the “General Information” tab in the menu on the left side, copy your CLIENT ID, and paste it in the 'CLIENT ID' field on the permissions page.

You will get a link to add your bot to your server. So go on to that link and add your bot to the server you created.

2. Setting up the Dialogflow agent

Visit the Dialogflow console to set up the agent for that you will require a Google account to log in. Go to Create Agent on the top left corner of the console and create an agent.

Now click on the “Small Talk” tab on the left side, go ahead and enable the small talk toggle. This is required if you want your bot to be able to converse with you.

Now, we need to get API keys from Dialogflow. Click on the gear icon beside your agent name. This will take you to the settings panel of the agent. Here click on your Project ID and open it in a new tab.

Now go to project settings in your GCP console

Go to Service Account on the left-hand menu. Click on the Create Service Account button.

Now, give it a name and a description to go with.

Click on create and continue. Give Dialogflow admin access to the project and click on Done.

Now click on the link for the service account.

Now, click on “Add Key” and create a new key in JSON format. This will be automatically get downloaded for you to your local system. Keep it safe.

3. Start Coding the bot

Open terminal of any editor of your choice. run following commands in the terminal one by one.

npm init -ynpm install discord.jsnpm install dontenvnpm install dialogflow @google-cloud/dialogflow uuid

Now click here to set the environment. Use the path of the JSON file which we have downloaded from the service account.

Create a .env file in your editor. Create DISCORD_TOKEN=yourDiscordToken and PROJECT_ID=YourProjectId.

DISCORD_TOKEN=examplexyzPROJECT_ID=exapmple123

Create index.js file and paste code given below.

Here, in function client.on () we are first checking if the user sending message is not a bot. If you don’t include this condition, then there may be an infinite loop where the bot replies to itself.

After the checking, you relay the message sent from the user to Dialogflow. And when the response is received by Dialogflow, you are sending it back to the user.

Now to finally try out the bot and see if it actually works! Run the command given below in the terminal and check-in your Discord Server.

node index.js

Make sure that you are using node version 16.17.04 or above and discord version 13 or above.

Now, try sending a message to the server.

This is what I received from the bot when I tried to talk with it

You can go back to your Dialogflow console and customize your bot even more! You can go to the documentation and find stuff that can be added or modified.

Get the entire code here.

Thanks for reading this article! Share with me your reviews/ideas. If you have any doubts or face an error, let me know in the comments. Don’t forget to give me a clap👏 :)

--

--