Skip to content

Instantly share code, notes, and snippets.

@codewithpom
Created May 21, 2021 05:28
Show Gist options
  • Save codewithpom/2f1420a1e41f9d25208f628704c1a619 to your computer and use it in GitHub Desktop.
Save codewithpom/2f1420a1e41f9d25208f628704c1a619 to your computer and use it in GitHub Desktop.
A discord bot for Trading
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
import requests
from discord.ext import commands
from nsetools import Nse
import discord
import os
options = Options()
options.add_argument("--headless")
nse = Nse()
driver = Chrome(chrome_options=options)
client = commands.Bot(command_prefix=".")
quote_link = "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol="
token = ""
@client.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
@client.event
async def on_ready():
print("Bot is Ready")
@client.command()
async def price(ctx, *, question):
print("One more command for stock price from a server")
print(question)
response = str(requests.get("https://smallbasicstockmarket.padmashreejha.repl.co?company=" + question).text)
print(response)
await ctx.send(f'The company was {question} and the stock price is {response}')
@client.command()
async def top_loser_link(ctx):
print("One more request from a server")
await ctx.send('The link is ' + "https://www.moneycontrol.com/stocks/marketstats/nseloser/index.php")
@client.command()
async def losers(ctx):
print("Request from a server for top losers")
await ctx.send("The top losers are ")
top_losers = nse.get_top_losers()
for loser in top_losers:
await ctx.send(f"The loser is {loser['symbol']} and the quote link is {quote_link + loser['symbol']}")
@client.command()
async def gainers(ctx):
print("Request from a server for top gainers")
await ctx.send("The top gainers are ")
top_gainers = nse.get_top_gainers()
for gainer in top_gainers:
await ctx.send(f"The gainer is {gainer['symbol']} and the quote link is {quote_link + gainer['symbol']}")
@client.command()
async def clear(channel):
messages = await channel.history().flatten()
for i in messages:
await i.delete()
@client.command()
async def graph(question, channel):
print("Request from a server for graph")
driver.get("https://google.com/search?q=The+stock+price+of+" + str(question))
print(question)
# noinspection PyBroadException
try:
symbol_of_the_company = driver.find_element_by_xpath("//span[@class='WuDkNe']")
print(str(symbol_of_the_company.text))
await channel.send(
f'The name of the company give was {question} and the symbol of the company is {symbol_of_the_company.text}')
except Exception:
channel.send("Sorry company not available on google")
await channel.send(file=discord.File(fp="graph_photos/Indosolar Limited.png"))
@client.command()
async def symbol(ctx, *, question):
print("Request from a server for symbol conversion")
driver.get("https://google.com/search?q=The+stock+price+of+" + str(question))
print(question)
# noinspection PyBroadException
try:
symbol_of_the_company = driver.find_element_by_xpath("//span[@class='WuDkNe']")
print(str(symbol_of_the_company.text))
await ctx.send(
f"The name of the company give was {question} and the symbol of the company is {symbol_of_the_company.text}")
except Exception:
ctx.send("Company not available")
@client.command()
async def reminder(ctx, question):
await ctx.send("What is the name of the company")
file = open("file.txt", 'w')
file.write(str(question))
file.close()
async for message in ctx.history(limit=1):
previous_message = message.content
print(previous_message)
while previous_message == "What is the name of the company":
async for message in ctx.history(limit=1):
previous_message = message.content
with open("company.txt", "w") as file:
async for message in ctx.history(limit=1):
previous_message = message.content
file.write(previous_message)
file.close()
os.startfile("info.py")
await ctx.send("Done!")
@client.command()
async def alert(ctx):
await ctx.send("Stock Alert")
@client.command()
async def bestbuy(ctx):
await ctx.send("The best buy companies")
driver.get("https://munafasutra.com/nse/tomorrowNewspaper/UP")
table = driver.find_element_by_tag_name("tbody")
table_rows = table.find_elements_by_tag_name("tr")
table_rows.pop(0)
print(table_rows)
for i in table_rows:
table_data = i.find_elements_by_tag_name('td')
for data in table_data:
print(data.text)
print(
f"Company name is {str(table_data[0].text)}\n\nToday's Movement {str(table_data[1].text)}\n\nTomorrow's Movement {str(table_data[2].text)}\n\nThe link for the quote is {table_data[3].find_element_by_tag_name('a').get_attribute('href')}")
await ctx.send("`\n\n`")
await ctx.send(
f"Company name is {str(table_data[0].text)}\n\nToday's Movement {str(table_data[1].text)}\n\nTomorrow's Movement {str(table_data[2].text)}\n\nThe link for the quote is {table_data[3].find_element_by_tag_name('a').get_attribute('href')}")
client.run(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment