January 4th, 2021
Unlike my most other posts that are and will be in this blog, this post is going to be a tutorial sort of application for a simple AI-Powered Discord Bot. I mean I wouldn't consider that one as a ready to use Discord Bot with a decent amount of features, yet I found it useful for understanding the concepts. Firstly, this tutorial is based on 2 other tutorials I saw online and I thought they could nicely merge to work together.
The first video that I'll refer to is this one, all the main Discord bot logic, and if I'm not very good at explaining stuff about the bot I highly encourage you to go ahead and watch this video first. Both the content and the tutor are awesome.
The second resource that I'll refer to is this one, a basic sentiment analysis application on Digital Ocean, for the sake of bearability purposes, I'll just leave the model training out and use a trained model with the source code from the resource I just provided. Yet again I highly encourage you to go ahead and check the tutorial if you are interested in ML and stuff.
I'll just briefly explain why and how we used all those pieces I used in this tutorial. I'll however explain some of them in the future in depth here in their own posts.
Replit is an online IDE/compiler/interpreter with lots of easy to use features. Since I know reading long blog posts are boring I'll just copy and paste some links for some parts here, this is how you can easily start to use Repl.it.
Uptimer Robot is an online service that can help you to keep a service alive by constantly pinging is basically. We'll use it to avoid the free tier limitations of Replit. Because Replit's free tier only keeps your code alive for 1 hour while you don't commit any activity.
This tutorial is written in Python, we also used a basic Flask server in order to keep the server alive with the help of Uptimer Robot. You always can go ahead and watch the source videos if you want to go into more details.
All the screenshots of the Discord bot are taken from Discord Desktop, chances are you know how to install Discord Desktop if you are here.
For simplicity purposes, I'll start with the required accounts and API keys. We need our API key ready and also a bunch of stuff that needs to be sorted out. So instead of stacking a bunch of pictures and text here, I'll just post a link for the Discord Bot creation and channel invite. All we need from now on from the Discord side is a token ( link, create a bot, 6th step).
For being able to use Uptimer Robot we basically will just need to create an account and then create a watcher for our service. For now, we can just need to create an account after the coding part is over we can come back here and create our watcher by following the picture here.
I'll also post the source code for the repo in case you don't want to follow along with me. You also should be able to get the trained model file from there.
Now we can start creating our app. I'll just go through what is unique about this tutorial since this one is already a merge of 2 other tutorials, all the steps are explained in detail on those resources. If you don't want to do that you can use the source code directly.
By the end of our tutorial file structure should look like that. So you can go ahead and create those files.
.env file contains our token for Discord connection. How we can access that token is explained before.
Lastly, before we start adding code to our main folder we can go ahead and download the pre-trained model from the source code link given above. The original tutorials and the code covers many more features but since they are mostly not relevant, not covered here.
All the imports we'll need for our main.py are given below, we first import what we need, then get our model as "classifier" and lastly initialize our client.
To get our token from .env file we can add this line of code at the end of our main.py file.
After that, we can just create a string array that contains our negative and positive responses.
With this code block below we can basically catch the message event and assign the content of the message to a variable.
Now we have a message that we can offer to our model. Our model takes tokenized sentences and decides if the message's content is negative or positive. After we have such a sentiment analysis results are ready we just basically return an answer from our hardcoded string array. So our model is just a basic sentiment analyzer that is using the Naive Bayes Classifier.
The tokenizer comes from the nltk.tokenize module and remove_noise comes from noise_remover.
Noise remover is a file with lots and regex and string manipulations. You can check that out from the original tutorial.
keep_alive.py file contains this code block, a basic Flask server. We should call it at the end of our main.py file with the following line.
After everything is set, our project should be ready to go. On the top right corner, we can view our API endpoint, so if you want your bot up and running all the time you can copy and paste that link to Uptime Robot watcher as described above.
After everything is set we can go ahead and check our bot and see how it's working. An example is down below.
I hope that you enjoyed that tutorial, I'll see you in the next post. Take care :)