[EN] Create Discord Chatbot with Gemini API

Razan Fawwaz
4 min readNov 24, 2023

--

This is the new version of the article, I already update the PaLM to the Gemini.

Background

Discord is a chat application that is quite widely used recently. Discord is comfortable to use for hobbies, community purposes, and also can be used by companies for employee communication. The features that Discord provided is very easy to use. One of the features is the bot integration. We can develop our own bit or use other available bot for our server needs. By integrating Discord bot with the Gemini API we can create a smart chatbot that can help answer questions in our server.

Steps

In this time, we will try to develop the bot using JavaScript. I used NodeJS, Discord.JS, and Gemini API.

1. Create a Discord Bot

We will start by creating and getting tokens from Discord. To create and get it, we have to go to the https://discord.com/developers/applications page and select New Application.

Please fill in the name according to your individual needs. This time I will use the name PaLM Bot and don’t forget to press the button Create.

After that we enter the page Botand will find a buttonReset Token

To get a token, we can press the button Reset Tokenand the token will appear. Make sure to copy and save the token! :D

On the same page, you have to scroll down a bit until you find Privileged Gateway Intents, make sure to turn everything on Intentlike the image below.

After that, we will go to the OAuth2 page and select the section Url Generator. For scopes, select Botand Permissionsmatch it to the image below

You will see a link and click copy, this link functions is to add the bot to our server. You should open the link and choos the server that you want to use.

If successful, the text above will appear on the selected server .

2. Get the Makersuite API

To be able to use the Gemini API it is necessary to log in and register at https://makersuite.google.com

You can click Get API keyin the left corner. Then select Create API keyyou can choose to create it in a new project or in an existing project. After that, you will get API keymake sure to copy or save it.

Documentation regarding the Gemini API can be read further at https://ai.google.dev/tutorials/node_quickstart

3. Create a NodeJS project

We’ll start by creating a NodeJS project. I will create a new directory called gemini-bot and initialize NPM.

mkdir gemini-bot
cd gemini-bot
npm init

If it is successful, then next we will install the required libraries, we need dotenv, google-auth-library, and @google-ai/generativelanguage

npm install dotenv google-auth-library @google-ai/generativelanguage

After that, I will create an .env file containing the Discord API Key & Gemini API Key as in the example below.

Next I will create a file calledindex.js

Then I’ll run it with the commandnode index.js

Bot is running!

If you are connected to Discord, it will produce output Bot is ready!and an online sign will also appear on the Discord server .

Then we can send a message and tag the bot account that we have created. If we send a message and don’t tag the bot account then the message will not be processed.

Due to Discord’s character limitations, if a message is more than 2000 characters long then the message will be split and sent into different messages.

The program code is also available in the GitHub repository https://github.com/razanfawwaz/gemini-bot

Finished!

Bots can be developed further according to individual wishes. For exploration with Discord it is recommended to read the guide from https://discordjs.guide/

Happy hacking!

--

--

Razan Fawwaz
Razan Fawwaz

Written by Razan Fawwaz

Interested in Backend & Infra | Technology Management Trainee (Backend) @EDTS | Kuli Ketik @SendTheSong

No responses yet