AI/ML

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


Source

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

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:

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

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)

DigitalOcean – The developer cloud

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>

https://medium.com/media/e6dfd68bb2134cc9ac90bb71fdf1b819/href

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

https://medium.com/media/bbfa6420180e69eed9a07ee316de0aab/href

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

https://medium.com/media/037a4c73153eefef41bbb9b5bd2b8769/href

Then update the OS with the command

https://medium.com/media/640d210f0a4b5a254a0451b1ddb6930d/href

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

https://medium.com/media/d13700974c96defed7360d982472665f/href

For the python part we will install some basic packages with

https://medium.com/media/1523e6931481e0f810a4b19a9533ad1d/href

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”

https://medium.com/media/f19fdd3ae27520461327db2933e7c908/href

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”

https://medium.com/media/d36805d1561cf257334b4651d7b0db62/href

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”

https://medium.com/media/85e4166a47529d09f23096664b47b887/href

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

https://medium.com/media/3858257404647e51e472329aa7eb7052/href

A “requirements.txt” file like this one

https://medium.com/media/f456346b3f95a79ca51535ab0211ecc8/href

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

https://medium.com/media/d50fb0a9228f31de70ba06d3dd23b4bf/href

In summary

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

https://medium.com/media/6587e47c3322f15d3374e6dbfd8d9ec7/href

To test the bot write the command

https://medium.com/media/3e8f01a0a1869e0eb31fc2148094e93a/href

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 👏 !

https://medium.com/media/7078d8ad19192c4c53d3bf199468e4ab/href


Building a Discord Bot for ChatOps , Pentesting or Server Automation (Part 5) was originally published in Chatbots Life on Medium, where people are continuing the conversation by highlighting and responding to this story.

Source link

Related posts

How large enterprises are implementing machine learning: 3 main use cases – TechRepublic

Newsemia

Luminance and Omnius are bringing AI to legacy industries

Newsemia

Mixed Maximum Loss Design for Optic Disc and Optic Cup Segmentation with Deep Learning from Imbalanced Samples.

Newsemia

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy

COVID-19

COVID-19 (Coronavirus) is a new illness that is having a major effect on all businesses globally LIVE COVID-19 STATISTICS FOR World