Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.7 kB
6
Indexable
Never
import discord
from discord.ext import commands
import yfinance as yf
import matplotlib.pyplot as plt
from io import BytesIO

TOKEN = "MTE3OTcxMTE2NzcwNzQ5MjQyMw.G_pP24.ekloXFhWOugQeej7iTXmLHeth3UPDL08IqZ8S8"

intents = discord.Intents.all()
intents.all()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user.name}')

@bot.command(name='stock')
async def stock(ctx, symbol):
    """Get stock information for a given symbol."""
    stock_data = yf.Ticker(symbol)
    
    try:
        info = stock_data.info
        history = stock_data.history(period='1y')
        
        # Sending the text message to Discord
        stock_info_message = f"Stock Information for {symbol}:\n"
        for key, value in info.items():
            stock_info_message += f"{key}: {value}\n"
        await ctx.send(stock_info_message)
        
        # Plotting the data
        plt.figure(figsize=(10, 6))
        plt.plot(history.index, history['Close'], label='Closing Price')
        plt.title(f"Stock Data for {symbol} - Last Year")
        plt.xlabel("Date")
        plt.ylabel("Closing Price")
        
        # Save the plot to a BytesIO object
        image_stream = BytesIO()
        plt.savefig(image_stream, format='png')
        plt.close()
        
        # Sending the plot as an image
        image_stream.seek(0)
        await ctx.send(file=discord.File(image_stream, filename='stock_plot.png'))
    except Exception as e:
        await ctx.send(f"Error: Unable to fetch data. {str(e)}")

bot.run(TOKEN)
Leave a Comment