Building a Discord Bot for ChatOps , Pentesting or Server Automation (Part 5)

Jose Figueredo
Chatbots Life

--

In this step by step tutorial we will create fully functional bot from scratch in which you can execute any command or cli program you like on your server or integrate with any API that you have access for example Jira. This is the part #5 of the series.

You can access the explanation of the discord setup in the Part #4 of this series here:

Prerequisites

  • A discord client: web, desktop app or mobile app
  • A discord server. You can create one inside your discord client of choice
  • A discord bot configured and added to the server
  • The bot token
  • A code editor of your choice. If you don’t have one use Visual Studio Code
  • Some programming knowledge
  • A Digital Ocean account (Or any other cloud provider)

Part #5: Deploying into a cloud server

Creating the account

I will demonstrate it using Digital Ocean because in my opinion is the one with the lower learning curve and it gives you $100 in initial credits. You can use this link to redeem it (and by referring you it will give me $25)

Creating the server

In your web browser log in to your Digital Ocean account and click the green button named “Create”. Then select “Droplets”

Trending Bot Articles:

1. Top 5 Music Bots for Discord and Telegram

2. GUIDE: How to Create a (damn cool) Multi-Language Chatbot with Manychat

3. A Powerful Chatbot CMS Solution for Conversational Chatbots

4. Platforms, NLP Systems, and Courses for Voice Bots and Chatbots

Select the Ubuntu 20.04 version for operative system, choose the starter “Standard” plan, and the “$5/mo” version for testing purpose. Later if you feel that this server settings are too low you can upgrade your plan

Select the datacenter region that is closest to your discord server and enable monitoring

For authentication it’s recommended that you use SSH keys. Press the “New SSH Key” button

Follow the instructions for creating a SSH Keys that are explained and then paste the public SSH key into the “SSH key” content text area and press the “Add SSH Key” button

Choose a hostname or leave the default and press create Droplet

Go to droplets to view your “IP Address” and write it down for later use

Preparing the server

In a terminal we will log into our server as root using the “IP Address”. In this test case the command will be: ssh root@<your IP Address>

Once inside we will create a non root user with the command “adduser bot”

Then we will give add the user “bot” to the sudo group with the command “usermod -aG sudo bot” and login as bot with “su — bot” to test that it work

Then update the OS with the command

And if the previous command hint us with the warning that there's packages to upgrade, we will upgrade the OS with the command

For the python part we will install some basic packages with

No to check that python3 is installed use the command “python3 -v” and to install the package installer “pip” we have to write the command “sudo apt install -y python3-pip”

To create separated environments for out python projects we can chose a variety of modules, in my case I will use virtual env. To install it write the command “sudo apt install -y python3-venv”

To create the environment for the bot create a new directory, enter the directory and write the creation command “python3 -m venv bot_env” and to activate it write “source bot_env/bin/activate”

Now you can copy your bot files in that directory and run it. I will show you a visual way using Visual Studio Code

Coding and debugging with Visual Studio Code

Open Visual Studio Code and press CTRL+Shift+P to enter the input window. Write “ssh” and select “Remote-SSH: Add New SSH Host…” for adding our server. It will ask you IP Address and the user of our Digital Ocean server

The app will show us the success message allowing us to connect directly

Once again press CTRL+Shift+P and enter “Remote-SSH: Connect to Host…” and select the connection

Now we will use the knowledge of the previous steps. Create the “.env” file with your secret constants, the “requirements.txt” file with the dependencies and the “bot.py” file with your existing bot’s code

To test it quickly we need a “.env” file with the “DISCORD_TOKEN” constant

A “requirements.txt” file like this one

And for the simplest bot code write this in the “bot.py” file

In summary

Go back to the terminal or use the integrated terminal in Visual Studio Code and install the requirements with the command

To test the bot write the command

You should see the “<Your bots name and id> is connected” message in the terminal and in Discord you should see the bot status as online

If you like to debug in Visual Studio Code to fix some bugs or to understand the logic, press F5 key in the IDE and select “Python File”

The IDE will enter debug mode allowing you to breakpoint the code and see the content of the variables

We are all set for this step.

Note:

If you encounter typos or something doesn’t work no more write me a comment and I will keep this guide updated. Last update June 28 2020.

Don’t forget to give us your 👏 !

--

--