From fed6335ed5be9a21973799abc06c317cb054cb6d Mon Sep 17 00:00:00 2001 From: MSFTserver Date: Fri, 10 Nov 2017 11:27:13 -0800 Subject: [PATCH 1/3] fix hash bot when WTM api is down --- bot/modules/hash.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/bot/modules/hash.js b/bot/modules/hash.js index 42f9453..973a9d1 100644 --- a/bot/modules/hash.js +++ b/bot/modules/hash.js @@ -2,7 +2,7 @@ let needle = require('needle'); let config = require('config'); let hasHashBotChannels = require('../helpers.js').hasHashBotChannels; let inPrivate = require('../helpers.js').inPrivate; -let ChannelID = config.get('hashbot').mainchannel; +let ChannelID = config.get('hashbot').mainchannel; exports.commands = [ "hash" // command that is in this file, every command needs it own export as shown below ] @@ -62,7 +62,6 @@ needle.get('https://explorer.lbry.io/api/v1/status', function(error, response) { } } - exports.hash = { usage: "", description: 'Displays current Hashrate of Network\n**!hash power **\n Displays potential Earnings For Given Hashrate', @@ -79,7 +78,7 @@ exports.hash = { sendMiningInfo(bot, msg, suffix); return } - + function sendMiningInfo(bot, msg, suffix) { if(!inPrivate(msg) && !hasHashBotChannels(msg)){ @@ -98,7 +97,7 @@ function sendMiningInfo(bot, msg, suffix) { needle.get('https://whattomine.com/coins/164.json', function(error, response) { if (error || response.statusCode !== 200) { msg.channel.send('whattomine API is not available'); - } else { + } var data = response.body; var reward = Number(data.block_reward); var block_time = Number(data.block_time); @@ -119,9 +118,8 @@ function sendMiningInfo(bot, msg, suffix) { "icon_url": "https://i.imgur.com/yWf5USu.png" } }; - msg.channel.send({ embed }); + msg.channel.send({ embed }); return - } }); } }); @@ -148,7 +146,7 @@ function sendProfitInfo(bot, msg, suffix) { "1 Hour = **" + LBC.toFixed(4) + "** \n" + "1 Day = **" + LBC24.toFixed(2) + "** \n" + "1 Week = **" + LBC1w.toFixed(4) + "** \n" + - "1 Month = **" + LBC1m.toFixed(4) + "** \n" + "1 Month = **" + LBC1m.toFixed(4) + "** \n" const embed = { "description": message, "color": 7976557, @@ -169,4 +167,4 @@ function numberWithCommas(x) { } -} \ No newline at end of file +} From b113c136790c0aceed563bbab6c648b4b50f5756 Mon Sep 17 00:00:00 2001 From: MSFTserver Date: Fri, 10 Nov 2017 11:28:59 -0800 Subject: [PATCH 2/3] update timed function with fix --- bot/modules/hash.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bot/modules/hash.js b/bot/modules/hash.js index 973a9d1..4805d9d 100644 --- a/bot/modules/hash.js +++ b/bot/modules/hash.js @@ -29,7 +29,7 @@ needle.get('https://explorer.lbry.io/api/v1/status', function(error, response) { needle.get('https://whattomine.com/coins/164.json', function(error, response) { if (error || response.statusCode !== 200) { msg.channel.send('whattomine API is not available'); - } else { + } var data = response.body; var reward = Number(data.block_reward); var block_time = Number(data.block_time); @@ -52,7 +52,6 @@ needle.get('https://explorer.lbry.io/api/v1/status', function(error, response) { }; bot.channels.get(ChannelID).send({ embed }) return - } }); } }); From 2ede4326b35aab0edb49dbf18949ec8320248f73 Mon Sep 17 00:00:00 2001 From: MSFTserver Date: Fri, 10 Nov 2017 11:33:01 -0800 Subject: [PATCH 3/3] fix format --- bot/modules/hash.js | 259 ++++++++++++++++++++++---------------------- 1 file changed, 127 insertions(+), 132 deletions(-) diff --git a/bot/modules/hash.js b/bot/modules/hash.js index 4805d9d..81f8a7c 100644 --- a/bot/modules/hash.js +++ b/bot/modules/hash.js @@ -2,7 +2,7 @@ let needle = require('needle'); let config = require('config'); let hasHashBotChannels = require('../helpers.js').hasHashBotChannels; let inPrivate = require('../helpers.js').inPrivate; -let ChannelID = config.get('hashbot').mainchannel; +let ChannelID = config.get('hashbot').mainchannel; exports.commands = [ "hash" // command that is in this file, every command needs it own export as shown below ] @@ -17,59 +17,59 @@ exports.timedhash = function(bot) { }, 6 * 60 * 60 * 1000); function sendMiningInfo(bot) { -needle.get('https://explorer.lbry.io/api/v1/status', function(error, response) { - if (error || response.statusCode !== 200) { - msg.channel.send('Explorer API is not available'); - } - else { - var data = response.body; - var height = Number(data.status.height); - var hashrate = data.status.hashrate; - var difficulty = Number(data.status.difficulty); - needle.get('https://whattomine.com/coins/164.json', function(error, response) { - if (error || response.statusCode !== 200) { - msg.channel.send('whattomine API is not available'); - } + needle.get('https://explorer.lbry.io/api/v1/status', function(error, response) { + if (error || response.statusCode !== 200) { + msg.channel.send('Explorer API is not available'); + } else { var data = response.body; - var reward = Number(data.block_reward); - var block_time = Number(data.block_time); - var difficulty24 = Number(data.difficulty24); - description = "Hashrate: "+numberWithCommas(hashrate)+"\n" + - "Difficulty: "+numberWithCommas(difficulty.toFixed(0))+"\n" + - "Difficulty 24 Hour Average: "+numberWithCommas(difficulty24.toFixed(0))+"\n" + - "Current block: "+numberWithCommas(height.toFixed(0))+"\n" + - "Block Time: "+numberWithCommas(block_time.toFixed(0))+" seconds \n" + - "Block Reward: "+numberWithCommas(reward.toFixed(0))+" LBC \n" + - "Sources: https://explorer.lbry.io & \n" + - "https://whattomine.com/coins/164-lbc-lbry"; - const embed = { - "description": description, - "color": 7976557, - "author": { - "name": "LBRY Network Stats", - "icon_url": "https://i.imgur.com/yWf5USu.png" - } - }; - bot.channels.get(ChannelID).send({ embed }) - return + var height = Number(data.status.height); + var hashrate = data.status.hashrate; + var difficulty = Number(data.status.difficulty); + needle.get('https://whattomine.com/coins/164.json', function(error, response) { + if (error || response.statusCode !== 200) { + msg.channel.send('whattomine API is not available'); + } + var data = response.body; + var reward = Number(data.block_reward); + var block_time = Number(data.block_time); + var difficulty24 = Number(data.difficulty24); + description = "Hashrate: "+numberWithCommas(hashrate)+"\n" + + "Difficulty: "+numberWithCommas(difficulty.toFixed(0))+"\n" + + "Difficulty 24 Hour Average: "+numberWithCommas(difficulty24.toFixed(0))+"\n" + + "Current block: "+numberWithCommas(height.toFixed(0))+"\n" + + "Block Time: "+numberWithCommas(block_time.toFixed(0))+" seconds \n" + + "Block Reward: "+numberWithCommas(reward.toFixed(0))+" LBC \n" + + "Sources: https://explorer.lbry.io & \n" + + "https://whattomine.com/coins/164-lbc-lbry"; + const embed = { + "description": description, + "color": 7976557, + "author": { + "name": "LBRY Network Stats", + "icon_url": "https://i.imgur.com/yWf5USu.png" + } + }; + bot.channels.get(ChannelID).send({ embed }) + return + }); + } }); + function numberWithCommas(x) { + return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); + } } - }); - function numberWithCommas(x) { - return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); -} -} } + exports.hash = { usage: "", description: 'Displays current Hashrate of Network\n**!hash power **\n Displays potential Earnings For Given Hashrate', process: function(bot,msg,suffix){ - var command = '!hash'; - words = suffix.trim().split(' ').filter( function(n){return n !== "";} ); - profitcommand = words[0]; - myhashrate = words[1]; - console.log(suffix) + var command = '!hash'; + words = suffix.trim().split(' ').filter( function(n){return n !== "";} ); + profitcommand = words[0]; + myhashrate = words[1]; + console.log(suffix) if (profitcommand == "power") { sendProfitInfo(bot, msg, suffix); return @@ -77,93 +77,88 @@ exports.hash = { sendMiningInfo(bot, msg, suffix); return } - - -function sendMiningInfo(bot, msg, suffix) { - if(!inPrivate(msg) && !hasHashBotChannels(msg)){ - msg.channel.send('Please use <#' + ChannelID + '> or DMs to talk to hash bot.'); - return; - } - needle.get('https://explorer.lbry.io/api/v1/status', function(error, response) { - if (error || response.statusCode !== 200) { - msg.channel.send('Explorer API is not available'); - } - else { - var data = response.body; - var height = Number(data.status.height); - var hashrate = data.status.hashrate; - var difficulty = Number(data.status.difficulty); - needle.get('https://whattomine.com/coins/164.json', function(error, response) { - if (error || response.statusCode !== 200) { - msg.channel.send('whattomine API is not available'); + + function sendMiningInfo(bot, msg, suffix) { + if(!inPrivate(msg) && !hasHashBotChannels(msg)){ + msg.channel.send('Please use <#' + ChannelID + '> or DMs to talk to hash bot.'); + return; + } + needle.get('https://explorer.lbry.io/api/v1/status', function(error, response) { + if (error || response.statusCode !== 200) { + msg.channel.send('Explorer API is not available'); + } else { + var data = response.body; + var height = Number(data.status.height); + var hashrate = data.status.hashrate; + var difficulty = Number(data.status.difficulty); + needle.get('https://whattomine.com/coins/164.json', function(error, response) { + if (error || response.statusCode !== 200) { + msg.channel.send('whattomine API is not available'); + } + var data = response.body; + var reward = Number(data.block_reward); + var block_time = Number(data.block_time); + var difficulty24 = Number(data.difficulty24); + description = "Hashrate: "+numberWithCommas(hashrate)+"\n" + + "Difficulty: "+numberWithCommas(difficulty.toFixed(0))+"\n" + + "Difficulty 24 Hour Average: "+numberWithCommas(difficulty24.toFixed(0))+"\n" + + "Current block: "+numberWithCommas(height.toFixed(0))+"\n" + + "Block Time: "+numberWithCommas(block_time.toFixed(0))+" seconds \n" + + "Block Reward: "+numberWithCommas(reward.toFixed(0))+" LBC \n" + + "Sources: https://explorer.lbry.io & \n" + + "https://whattomine.com/coins/164-lbc-lbry"; + const embed = { + "description": description, + "color": 7976557, + "author": { + "name": "LBRY Network Stats", + "icon_url": "https://i.imgur.com/yWf5USu.png" + } + }; + msg.channel.send({ embed }); + return + }); + } + }); + } + function sendProfitInfo(bot, msg, suffix) { + needle.get('https://whattomine.com/coins/164.json', function(error, response) { + if (error || response.statusCode !== 200) { + msg.channel.send('whattomine API is not available'); + } else { + words = suffix.trim().split(' ').filter( function(n){return n !== "";} ); + var myhashrate = words[1]; + if (myhashrate == "" || myhashrate == null || myhashrate == undefined || myhashrate == " ") { + myhashrate = "100"; + } + var Diff = response.body.difficulty24; + var Reward = response.body.block_reward; + var myHash = Number(myhashrate) + var LBC = myHash / 2000 * (1 / (Diff * 2^32) * Reward) * 3600 + var LBC24 = myHash / 2000 * (1 / (Diff * 2^32) * Reward) * 86400 + var LBC1w = myHash / 2000 * (1 / (Diff * 2^32) * Reward) * 604800 + var LBC1m = myHash / 2000 * (1 / (Diff * 2^32) * Reward) * 2628000 + var message = "With **" + myHash + " Mh/s** and Average 24 hour Difficulty: **" + Diff.toFixed(0) + "**\n" + + "You can potentially earn the following amounts of **LBC**: \n" + + "1 Hour = **" + LBC.toFixed(4) + "** \n" + + "1 Day = **" + LBC24.toFixed(2) + "** \n" + + "1 Week = **" + LBC1w.toFixed(4) + "** \n" + + "1 Month = **" + LBC1m.toFixed(4) + "** \n" + const embed = { + "description": message, + "color": 7976557, + "author": { + "name": "Hashing Power Calculator!", + "icon_url": "https://i.imgur.com/nKHVQgq.png" + } + }; + msg.channel.send({ embed }) + return + } + }); + } + function numberWithCommas(x) { + return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } - var data = response.body; - var reward = Number(data.block_reward); - var block_time = Number(data.block_time); - var difficulty24 = Number(data.difficulty24); - description = "Hashrate: "+numberWithCommas(hashrate)+"\n" + - "Difficulty: "+numberWithCommas(difficulty.toFixed(0))+"\n" + - "Difficulty 24 Hour Average: "+numberWithCommas(difficulty24.toFixed(0))+"\n" + - "Current block: "+numberWithCommas(height.toFixed(0))+"\n" + - "Block Time: "+numberWithCommas(block_time.toFixed(0))+" seconds \n" + - "Block Reward: "+numberWithCommas(reward.toFixed(0))+" LBC \n" + - "Sources: https://explorer.lbry.io & \n" + - "https://whattomine.com/coins/164-lbc-lbry"; - const embed = { - "description": description, - "color": 7976557, - "author": { - "name": "LBRY Network Stats", - "icon_url": "https://i.imgur.com/yWf5USu.png" - } - }; - msg.channel.send({ embed }); - return - }); } - }); -} -function sendProfitInfo(bot, msg, suffix) { - needle.get('https://whattomine.com/coins/164.json', function(error, response) { - if (error || response.statusCode !== 200) { - msg.channel.send('whattomine API is not available'); - } else { - words = suffix.trim().split(' ').filter( function(n){return n !== "";} ); - var myhashrate = words[1]; - if (myhashrate == "" || myhashrate == null || myhashrate == undefined || myhashrate == " ") { - myhashrate = "100"; - } - var Diff = response.body.difficulty24; - var Reward = response.body.block_reward; - var myHash = Number(myhashrate) - var LBC = myHash / 2000 * (1 / (Diff * 2^32) * Reward) * 3600 - var LBC24 = myHash / 2000 * (1 / (Diff * 2^32) * Reward) * 86400 - var LBC1w = myHash / 2000 * (1 / (Diff * 2^32) * Reward) * 604800 - var LBC1m = myHash / 2000 * (1 / (Diff * 2^32) * Reward) * 2628000 - var message = "With **" + myHash + " Mh/s** and Average 24 hour Difficulty: **" + Diff.toFixed(0) + "**\n" + - "You can potentially earn the following amounts of **LBC**: \n" + - "1 Hour = **" + LBC.toFixed(4) + "** \n" + - "1 Day = **" + LBC24.toFixed(2) + "** \n" + - "1 Week = **" + LBC1w.toFixed(4) + "** \n" + - "1 Month = **" + LBC1m.toFixed(4) + "** \n" - const embed = { - "description": message, - "color": 7976557, - "author": { - "name": "Hashing Power Calculator!", - "icon_url": "https://i.imgur.com/nKHVQgq.png" - } - }; - msg.channel.send({ embed }) - return - } - }); -} -function numberWithCommas(x) { - return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); -} - - - -} }