Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Add stats command
Browse files Browse the repository at this point in the history
  • Loading branch information
techcoderx committed Mar 8, 2019
1 parent 3abb3c0 commit 72bd695
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
Binary file modified .DS_Store
Binary file not shown.
69 changes: 69 additions & 0 deletions bot.js
Expand Up @@ -3,6 +3,7 @@ const Discord = require('discord.js');
const WGET = require('wget-improved');
const IPFS = require('ipfs-http-client')('localhost',Config.IPFS_API_Port,{protocol: 'http'})
const Steem = require('steem');
const async = require('async')
const Auth = require('./auth.json');
const fs = require('fs');

Expand Down Expand Up @@ -159,6 +160,8 @@ bot.on('message', (message) => {
addHashToDatabase(message,ipfs240hash);
addDTubeVideoToIPFS(message,ipfs240hash,Config.trickledag,Config.VIDEO_DOWNLOAD_COMPLETE);
});
} else {
message.channel.send(Config.IPFS_PIN_LS_ERROR + err)
}
})
});
Expand Down Expand Up @@ -224,6 +227,8 @@ bot.on('message', (message) => {
addHashToDatabase(message,ipfs480hash);
addDTubeVideoToIPFS(message,ipfs480hash,Config.trickledag,Config.VIDEO_DOWNLOAD_COMPLETE);
});
} else {
message.channel.send(Config.IPFS_PIN_LS_ERROR + err)
}
})
});
Expand Down Expand Up @@ -294,6 +299,8 @@ bot.on('message', (message) => {
addHashToDatabase(message,ipfs720hash);
addDTubeVideoToIPFS(message,ipfs720hash,Config.trickledag,Config.VIDEO_DOWNLOAD_COMPLETE);
});
} else {
message.channel.send(Config.IPFS_PIN_LS_ERROR + err)
}
})
});
Expand Down Expand Up @@ -366,6 +373,8 @@ bot.on('message', (message) => {
addHashToDatabase(message,ipfs1080hash);
addDTubeVideoToIPFS(message,ipfs1080hash,Config.trickledag,Config.VIDEO_DOWNLOAD_COMPLETE);
});
} else {
message.channel.send(Config.IPFS_PIN_LS_ERROR + err)
}
})
});
Expand Down Expand Up @@ -416,6 +425,8 @@ bot.on('message', (message) => {
addHashToDatabase(message,dsoundhash)
addDTubeVideoToIPFS(message,dsoundhash,false,Config.AUDIO_DOWNLOAD_COMPLETE)
});
} else {
message.channel.send(Config.IPFS_PIN_LS_ERROR + err)
}
})
});
Expand Down Expand Up @@ -571,6 +582,64 @@ bot.on('message', (message) => {
} else {
sendMessage(message,Config.ERROR_NO_PERMISSION);
}
} else if (message.content == Config.commandPrefix + 'stats') {
let statops = {
bw: (cb) => {
IPFS.stats.bw((err,stat) => {
if (err != null) {
console.log(err)
cb(err,null)
return
}
let incomingbw = Math.floor(stat.rateIn / 10.24) / 100 + ' KB/s'
let outgoingbw = Math.floor(stat.rateOut / 10.24) / 100 + ' KB/s'

if (stat.rateIn > 1000000)
incomingbw = Math.floor(stat.rateIn / 10485.76) / 100 + ' MB/s'

if (stat.rateOut > 1000000)
outgoingbw = Math.floor(stat.rateOut / 10485.76) / 100 + ' MB/s'
cb(null,{
in: incomingbw,
out: outgoingbw
})
})
},
storage: (cb) => {
IPFS.stats.repo({human: true},(err,stat) => {
if (err != null) {
console.log(err)
cb(err,null)
return
}
let size = Math.floor(stat.repoSize / 10485.76) / 100 + ' MB'
if (stat.repoSize > 1000000000)
size = Math.floor(stat.repoSize / 10737418.24) / 100 + ' GB'
cb(null,size)
})
},
pincount: (cb) => {
IPFS.pin.ls({type: 'recursive'},(err,pinset) => {
if (err != null) {
console.log(err)
cb(err,null)
return
}
cb(null,pinset.length)
})
}
}
async.parallel(statops,(err,results) => {
if (err != null) {
message.channel.send(Config.IPFS_STAT_ERROR)
} else {
let embed = new Discord.RichEmbed()
embed.setTitle(Config.IPFS_STAT_EMBED_TITLE)
embed.setDescription('Bandwidth In: ' + results.bw.in + '\nBandwidth Out: ' + results.bw.out + '\nNo. of pins: ' + results.pincount + '\nUsed storage: ' + results.storage)
embed.setColor(0x499293);
message.channel.send(embed)
}
})
} else if (message.content == (Config.commandPrefix + 'ipfshelp')) {
if (Config.silentModeEnabled != true) {
// Bot command list
Expand Down
2 changes: 2 additions & 0 deletions config.json
Expand Up @@ -49,6 +49,8 @@
"HELP_IPFSHELP": "Shows this cheatsheet with all available commands for this bot.",
"HELP_PING": "Use this to check if bots are online!",
"IPFS_PIN_LS_ERROR": "IPFS API error: ",
"IPFS_STAT_ERROR": "An error occured! Check the logs for more details.",
"IPFS_STAT_EMBED_TITLE": "IPFS Node Stats",
"WHITELIST_FILE404": "Hmmm the whitelist file doesn't exist in this bot. Perhaps the file could have been misplaced in the bot server? :thinking:",
"WHITELIST_TRUE": "You're in the whitelist! You may pin your DTube videos at 720p, 1080p and source resolution to our IPFS node :wink:",
"WHITELIST_FALSE": "Sorry, you're not in the whitelist. Perhaps contact the admins of this server to find out how to get there? :thinking:",
Expand Down
7 changes: 0 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -5,6 +5,7 @@
"main": "bot.js",
"author": "techcoderx",
"dependencies": {
"async": "^2.6.2",
"discord.js": "^11.4.2",
"fs": "0.0.1-security",
"ipfs-http-client": "^29.1.1",
Expand Down

0 comments on commit 72bd695

Please sign in to comment.