Zephyrnet Logo

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

Date:


Source
Jose Figueredo

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 #3 of the series.

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

  • 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
  • Python 3 and the module pip installed in your machine

If we need to view the log of the discord.py library for debugging we can output to console

1. Build a Telegram Bot Scheduler with Python

2. A Conversational UI Maturity Model: a guide to take your bot to the next level

3. Sentiment Analysis Voice Bot

4. Chatbot, Natural Language Processing (NLP) and Search Services and how to mash them up for a better user experience

And the output will be something like this

Or outputting to a file

If we want to chat in a channel and invoke the bot we need a way to identify what is a comment and what is a command. For that purpose we will use a prefix, in this case the exclamation sign “!”

If you message a channel with the prefix nothing will happen because the bot is not listening for any specific commands yet

For creating a command we’ll use the decorator @bot.command using the parameters name and help.

When we write “!bot” the bot will handle the message and reply it’s info

Discord have a special type of message that have colored border and have embedded images, text fields and other fancy stuff. Here we transform the information command that we’ve created before and stylize it

When we write “!bot” the bot will handle the message and reply it’s info in a more appealing way

Discord use three types of status and called them presence and you can choose from three types: watching, playing and listening

When you type the command “!status” and pass it the text of the status

The status of the Bot will display the presence type and the text that you passed it

We’ll now create a command that executes a command in the shell of the machine that the bot is running. For that we will use the subprocess package of python

Let’s write “!exec ls”

If we write a command that doesn’t exist for example “!exec a”, we will get the handled exception message

If you’ve payed attention maybe you’ve realize that In the previous example we could only send one parameter to our exec command and that is not realistic so to make our command listen to any amount of parameters we will do this

Let’s write “!exec ls -ltr”

If you have a long output, you want to save a copy of it for later processing/backup or you don’t want the output to stay in the chat we can create a file with the output and send it as an attachment like this

Let’s write “!exec ls -ltr”

If we click on the “exec_output.txt” blue text or the down arrow symbol discord will download the file locally. For example the content of my file is

We are all set for this step. In the Part #4 we will secure our bot with roles and ownership then we will chain commands and sanityze the output that is colored or specially formatted for the console.

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 12 2020.

Source: https://chatbotslife.com/building-a-discord-bot-for-chatops-pentesting-or-server-automation-part-3-1ceeecdc3752?source=rss—-a49517e4c30b—4

spot_img

Latest Intelligence

spot_img