Browse Source

Working stock bot.

Matthew Clark 4 years ago
commit
d2bf56b759
2 changed files with 149 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 147 0
      stockbot.js

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+node_modules
+*.log

+ 147 - 0
stockbot.js

@@ -0,0 +1,147 @@
+const Discord = require('discord.js');
+const client = new Discord.Client();
+
+var http = require("http");
+
+var helpText = "```If you would like a stock symbol like $TSLA, just ask.```";
+
+var botHome = "https://www.mclarkdev.com/discord/bots/stockbot";
+
+var botDisclaimer = "All values are fetched in real time from the Yahoo Finance API. This data is not guarenteed to be real time or accurate, and should not be used as the basis of trading decisions.";
+
+var offset = -6;
+
+var key = "__YOUR__DISCORD__API_KEY__";
+
+String.prototype.format = function() {
+
+	var content = this;
+	for (var i=0; i < arguments.length; i++) {
+
+		var replacement = '{' + i + '}';
+		content = content.replace(replacement, arguments[i]);  
+	}
+
+	return content;
+};
+
+function lookupSymbols( symbols, channel ) {
+
+	for (var i = 0; i < symbols.length; i++) {
+
+		var symbol = symbols[i].substring(1).toUpperCase();
+
+		console.log( (new Date()).toLocaleString() + " - Looking up symbol [ " + symbol + " ]" );
+
+		var options = {
+			host: "download.finance.yahoo.com",
+			port: 80,
+			path: "/d/quotes.csv?f=sapp2j1c1ba5b6l1n&s=" + symbol
+		};
+
+		http.get(options, function (http_res) {
+
+			var data = "";
+
+			http_res.on("data", function (chunk) {
+
+				data += chunk;
+			});
+
+			http_res.on("end", function () {
+
+				data = data.replace(/\n$/g, '').replace(/\"/g, '');
+				console.log((new Date()).toLocaleString() + " - Fetched [ " + data + " ]");
+
+				var hour = (new Date().getHours() + offset);
+				var mins = new Date().getMinutes();
+				if ( hour > 23 ) {
+					hour -= 12;
+				}
+
+				if ( hour < 0 ) {
+					hour += 12;
+				}
+
+				if ( mins < 10 ) {
+					mins = "0" + mins;
+				}
+
+				var parts = data.split(",");
+
+				var upDown;
+
+				if ( parts[5] == 0 ) {
+
+					upDown = "https://www.mclarkdev.com/static/1488503892/no.png";
+				} else if ( parts[5] > 0 ) {
+
+					upDown = "https://www.mclarkdev.com/static/1488503892/up.png";
+				} else if ( parts[5] < 0 ) {
+
+					upDown = "https://www.mclarkdev.com/static/1488503892/dn.png";
+				}
+
+				var etURL = "https://www.etrade.wallst.com/v1/stocks/snapshot/snapshot.asp?rsO=new&symbol=" + parts[0];
+
+				var toSend = "```{11}\n\nSymbol         : {0}\nLast Trade     : {10}\n\nCurrent Ask    : {1} x {8}\nCurrent Bid    : {7} x {9}\n\nDaily Change   : {5}$\n               : {3}\n\nPrevious Close : {2}\nMarket Cap     : {6}```\nDashboard : [ETrade]({4})".format(
+					parts[0], parts[1], parts[2], parts[3], etURL, parts[5], parts[4], parts[6], parts[7], parts[8], parts[9], parts[10], parts[11]
+				);
+
+				var embed = {
+					"description" : toSend,
+					"thumbnail" : {
+						"url" : upDown
+					},
+					"author" : {
+						"name" : "StockBot, at your service... ({0}:{1} EST)".format( hour, mins ),
+						"url": botHome,
+						"icon_url" : "https://www.mclarkdev.com/static/1488503892/icon.png"
+					},
+					"footer" : {
+						"text" : botDisclaimer
+					}
+				};
+
+				if ( hour >= 16 || hour < 9 ) {
+
+					embed["description"] += "\n\nNOTE: Markets are currently closed.";
+				}
+
+				channel.sendEmbed( embed );
+			});
+		});
+	}
+}
+
+client.on( 'ready', () => {
+
+	console.log( (new Date()).toLocaleString() + " - StockBot, at your service." );
+});
+
+client.on( 'message', message => {
+
+	if ( message.author.id == client.user.id ) {
+
+		return;
+	}
+
+	var match = message.content.match( /\$help/i );
+	if ( match != null ) {
+
+		message.channel.sendMessage( helpText );
+		return;
+	}
+
+	// extract symbols
+	var symbols = message.content.match( /\$[A-Za-z]{2,5}/g );
+	if ( symbols == null ) {
+
+		return;
+	}
+
+	lookupSymbols( symbols, message.channel );
+});
+
+client.login( key );
+