# Discord+Notion Bot

---

Discord is the modern party house.

It is essential to be updated with the latest trends. As COVID-19 hit social life badly, the majority population switched to Discord servers to interact and have fun. As we had created our server, we felt the need to have our own specialized bots so that it adds value to the server.

In case you weren’t aware, robots have taken over. Having chatbots run your Discord servers is incredible since you can focus on what’s important. Keeping this in mind and taking into consideration our daily usage of Notion for managing resources and making notes, we thought of creating a discord bot that consumes Notion API to add and retrieve data from Notion databases.

[**Start building with the Notion API**  
\*Connect Notion pages and databases to the tools you use every day, creating powerful workflows. Get started Aggregate…\*developers.notion.com](https://developers.notion.com/).

---

#### Now, how to use this bot?

**Pre-Requisites:**

a) Install all the requirements using `pip3 install -r requirements.txt` b) Create a discord bot using the developer platform of discord and obtain your OAuth2 token. Keep it somewhere safe.  
c) Go to Notion and create a new Integration [https://www.notion.so/my-integrations](https://www.notion.so/my-integrations). Note the internal Integration.  
d) Go to Notion and create a table like this:

![](https://cdn-images-1.medium.com/max/1000/0*fDh9bEyezubkccg8 align="left")

Table

Add the following 4 columns :

* Contributor is of property type Title
    
* URL of ‘URL’ type
    
* Title of text type
    
* Tag of multi-select type
    

e) Click on the three dots and press Open as a page.

![](https://cdn-images-1.medium.com/max/1000/0*L7YyJ0pEqeZBgPCX align="left")

Opening the [notion.so](http://notion.so) file.

f) Then press share and copy the URL. The URL will look something like this: [https://www.notion.so/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX?v=YYYYYYYYYYYYYYYYYYYYYYYYY](https://www.notion.so/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX?v=YYYYYYYYYYYYYYYYYYYYYYYYY)

![](https://cdn-images-1.medium.com/max/1000/0*L8_wkk38K0S25RVW align="left")

Extracting from the link

Note down the X part of the URL(RED part). This is your database ID.  
g) Also press share again, press Invite and then click on the integration you made earlier.  
h) Google Drive setup: All physical files or PDF files from links are downloaded on a google drive folder of your choice.

*Setup Procedure:*  
a) Follow the README file located in the GitHub repo.  
b) Move the `credentials.json` file and `token.json` to the `creds` folder in the Bot folder.  
c) Now go to google drive and create a folder. The link to the folder will be something like this [https://drive.google.com/drive/folders/ID.  
d](https://drive.google.com/drive/folders/ID.%EF%BF%BCd)) Note the ID down. This will be the id of the folder where all files will be stored. Also, share the folder accordingly.  
f) Now, on the terminal of your machine:  
`$export DATABASE_TOKEN=<THE DATABASE TOKEN YOU NOTED DOWN>  $export AUTH_KEY=<THE INTEGRATION SECRET KEY FROM NOTION> $export GDRIVE_FOLDER=<FOLDER ID YOU NOTED ABOVE> $export DISCORD_AUTH=<THE SECRET TOKEN OF BOT>`  
If you want your prefix to be something else other than '/' then   
`$export PREFIX=<Your Desired character>`

**Running Procedure**

a) Now simply go into the Bot folder and run: `$python3` [`bot.py`](http://bot.py) b)On the discord developer site, copy the OAuth2 link and paste it into the browser and invite it to a server you own/have permission to do so.  
c) Bot Usage:  
\*Adding Record:  
\*`/add <URL_YOU_WANT_TO_RECORD>` =&gt; This will add a new record to your database with a `misc` tag.  
`/add <URL_YOU_WANT_TO_RECORD> <TAG>` =&gt; This will add a new record to your database with the `<TAG>` tag.

*For multiple tags:* `/add <URL_YOU_WANT_TO_RECORD> <TAG>,<TAG1>,<TAG2>`

![](https://cdn-images-1.medium.com/max/1000/0*koMY2s0D_y141WY5 align="left")

Example

![](https://cdn-images-1.medium.com/max/1000/0*JIgD5NKugfa4Ftna.png align="left")

Sample database populated by the bot

**Searching records through tags:**

`/search <Tag1>` *For multiple tags:* `/search <Tag1> <Tag2>.....<TagN>`

![](https://cdn-images-1.medium.com/max/1000/0*K6FMZ5vb-xSctFK7.png align="left")

Example (1)

![](https://cdn-images-1.medium.com/max/1000/0*AEruNaftwDuQrlh-.png align="left")

Example (2)

**Deleting records through tags:**

`/delete <Tag 1>` *For multiple tags:* `/delete <Tag 1> <Tag2>......<Tag N>`

Example:

![](https://cdn-images-1.medium.com/max/1000/0*lplmZDME03raxzDR.png align="left")

Initial Database

![](https://cdn-images-1.medium.com/max/1000/0*XMZcWcAJg2dg7DyF.png align="left")

Deleting the record

![](https://cdn-images-1.medium.com/max/1000/0*sBZi0Nj--sPHayvS.png align="left")

Final Database

**Uploading Files:**

NOTE: In the below images, the prefix used is: `!`

You can upload any file to the notion database. It can be a PDF, PNG, JPEG, etc.  
In the example below, a PNG file is being uploaded.  
a) Drag your file to the chat in discord.

![](https://cdn-images-1.medium.com/max/1000/0*OTIzquk1T6xtNxBA align="left")

b) Add a comment to the file you uploaded.

![](https://cdn-images-1.medium.com/max/1000/0*SNqOaeXRTTJuBLXC align="left")

Here memoji and profile-pic are tags for the file

c) It will then ask for the title of the file you uploaded.

![](https://cdn-images-1.medium.com/max/1000/0*unKwTvIZ6l06jWym align="left")

d) Now, the file will be uploaded to the google drive folder which you mentioned above.  
e) The database will be updated.

![](https://cdn-images-1.medium.com/max/1000/0*pco9X-_flhguN0MQ align="left")

The link will open the file in Google Drive.

---

If your team is using Discord and looking for a database management bot, Notion bot is here for you! You can access all the open source repositories of this bot on GitHub and docker-hub and, in fact, invite it to your server as well since it is verified by Discord. That’s pretty amazing, right?

The above example is fulfilled by accessing our GitHub repository:  
[https://github.com/Servatom/Notion-DiscordBot](https://github.com/Servatom/Notion-DiscordBot)

Instead of running the discord bot via python and exporting environment variables, you can use our docker image.  
[https://hub.docker.com/repository/docker/servatom/notiondiscordbot](https://hub.docker.com/repository/docker/servatom/notiondiscordbot)
