PythonMusings: Make a Stock/Crypto Ticker Discord Bot with this Simple Script!

This article was first published on Python Musings – bensstats , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

With a little bit of time off I decided to explore the module again with trying to make some of my own Discord bots. The bot I decided to make is a simple stock/crypto ticker bot that tracks the live price of a stock/crypto that I want.

This was inspired by the r/Cryptocurrency discord server which had such bots for various cryptocurrencies/DEFI but for some reason did not have them publicly available.

Some of the tickers publicly available on the r/Cryptocurrency Discord server.

Because I thought it was really cool to see live prices of stocks/crypto in real time on Discord, I wanted to make one myself. Lets run through the steps for making a ticker bot like I did today!

The script requires that you have the discord and the yahoo_fin modules installed. So before you start be sure to have them installed in your environment.

pip install discord
pip install yahoo_fin

Step 1: Go to the Discord Developers Portal and create an application with a bot

First things first, you’re going to want to log into the Discord developers portal navigate to “Applications” and click on “New Application” after which you will be given a prompt to put in the name of the application.

After the application is created, click on your application and create a bot by going to the “Bot” section of the settings and create a bot.

I made a Bitcoin Bot

After that, go to the OAuth2 section of the bot and under “scopes” click “bot” and invite the bot with the link generated.

Once you invited you can proceed with developing your own bot.

Step 2: Get your token

It is very important to have your unique token for your bot. This can be found in your application settings under the “bot” tab.

This bot is for tracking BTC-USD prices

Once the token is copied, put it in a .txt file that you can access.

Step 3: The Script

The script follows the following structure:

from discord.ext import commands
from yahoo_fin import stock_info as si

def read_token():
    with open('botTokenPath.txt', 'r') as f:
        lines = f.readlines()
        return lines[0].strip()

token = read_token()

bot = commands.Bot(command_prefix="!!")


async def run(ctx):
    while True:
        change = si.get_quote_data("StockSymbol")

        name="S&P500: \n$"+ str(round(si.get_live_price("StockSymbol"),2))+ "("+str(round(change["regularMarketChangePercent"],2))+")"+"%"

Run the script, and there you have it! Once you have your token and Stock Symbols picked, type !run in one of your channel servers and you should have your stock ticker up and running!

Its alive!!

As for hosting, there are lots of great resources out there for figuring how to do that. If you’re curious, I recommend either the solution offered by FreeCodeCamp (see the end of the article) or by Tech With Tim.

Safety Tips

  1. Never share your bot token with anyone– this gives the opportunity for hackers to enter into your server and expose vulnerabilities and ruin your overall discord experience
  2. Be care with the permissions you give your bot– the permissions you give your bot will enable your bot to function. But if your give any bot full reign to do whatever it wants-regardless of the abilities you programmed it to have- you are exposing your server to being hacked or nuked . For these bots, the only real permission they should have is the ability to change their nicknames. Everything else is not needed.

Thank you for reading! If you have any questions be sure to reach out!

Want to see more of my content?

Be sure to subscribe and never miss an update!

To leave a comment for the author, please follow the link and comment on their blog: Python Musings – bensstats .

Want to share your content on python-bloggers? click here.