Tip: Make yourself a handy Discord (or Slack) ChatBot to do a bunch of tedious tasks for you.
So why make your own chatbot?
With your own chatbot, there are so many different things you can do to streamline your workflow. You can build it out however you want with the features personalized for your needs. You can also give the bot a personality so it feels you have a mini butler on hand (“Yo Alfred, warm up the batmobile!”).
A few things I noticed myself doing lately were
- looking up box scores of NBA games and player stats
- looking up UFC/MMA fight schedules and the betting odds
- stalking gym equipment to go on sale
- mashing F5 on a shopping site to see if the item is in stock
A solution that came to mind was: toss all of these tasks inside a chatbot and kill many birds with one stone.
My chat client of choice for this bot would be Discord since it’s what I use the most. If you’re not a Discord user, there are other popular chat clients that supports chat bots as well like Slack or Skype. A Discord tip I recommend is to create your own personal server so you can interact with the bot however you like without disturbing others.
The Discord client
Building a bot on Discord right now is very straight forward with Commando — a flexible, fully object-oreinted command framework for Discordjs. Another framework to keep an eye out for in the future is Wokcommands — a fully featured Discordjs framework with built in Mongodb support, built by Worn Off Keys. For this Demo, I’ll be using Commando.
Let’s build a simple bot that gets current stats for a NBA player.
- Follow the steps in the Discord Developers docs to create a bot and retrieve the bot token.
- Create an empty directory.
- Create an index.js file, config.json (or dotenv) file , and a commands folder.
- Pop open your terminal and run npm init -y to create the package.json with default settings.
- The bot’s folder should look like this:
Trending Bot Articles:
- npm i discord.js discord.js-commando to install the packages.
- In the config.json file
- In the index.js file
- Now with the Commando framework and Discordjs wired up, we can start adding command files inside the commands folder.
- Our NBA stat fetcher command will go inside commands/sports/nbastats.js. All sports related commands will reside there as well.
- updated directory tree
- inside nbastats.js
- The API used for this command was the balldontlie API from balldontlie.io
- In the run class method, we put the code that we want to execute whenever the bot detects that command. This method takes in 4 arguments (message, args, fromPattern, and result). For the nbastat grabber command, we will only need message and args.
- When the command is executed, it passes the argument(s) (the player’s name) into the endpoint and fetches the data for the player. That object is then passed into another method to fetch a second layer of data (pts, assists, rebounds, shooting %, etc) for the player. Finally, that player’s data gets passed into a method that builds the embedded message.
Once you add the bot to your personal server and activate it (I recommend using nodemon to run the bot while in development), it begins watching each message. If the message starts with the prefix followed by the command name, the code in the run() method gets executed.
Here is the command in action. Typing out these few words were much faster than visiting a website.
The convenience of having my own chat bot do tedious tasks for me is a game changer. Some of my favorite features include:
- fetching box scores and statistics of NBA games/players
- pinging me when an item is in stock
- pinging me 15 mins before a specific event starts
- check prices for things like stocks/crypto
This isn’t even the tip of the iceberg, as long as you can find an API or scrape the site, you can find ways to benefit using a personal chat bot. You can even take it to the next level and connect the bot to a database to share data across other clients (websites, mobile apps, etc).
There are a few different ways to host the bot for free. Heroku has a free tier that offers a generous amount of run time (more if you have a linked credit card). AWS and Digital Ocean are also popular hosting options if you can rack up some free credits.