diff --git a/README.md b/README.md index 8826d5a..54c512d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # DougleyBot -[![Version](https://img.shields.io/badge/Version-1.2.7-green.svg?style=flat-square)](https://github.com/SteamingMutt/DougleyBot/releases) +[![Version](https://img.shields.io/badge/Version-1.2.8-green.svg?style=flat-square)](https://github.com/SteamingMutt/DougleyBot/releases) [![Status](https://img.shields.io/badge/Status-Ready-green.svg?style=flat-square)]() [![Node](https://img.shields.io/badge/Node-4.2.2-blue.svg?style=flat-square)](http://nodejs.org) [![NPM](https://img.shields.io/badge/NPM-3.5.0-blue.svg?style=flat-square)](http://nodejs.org) @@ -34,6 +34,6 @@ Check the wiki for more info. - [ ] Integrate a WolframAlpha command. The original DiscordBot had one, but that didn't work. - [ ] Make it so that log files are written when !log is used, instead of printing them to the console. - [x] Make it so that !help outputs into a DM, instead of printing it into the channel. -- [ ] Intergrate Cleverbot support, but restrict it to a certain channel. +- [ ] Integrate Cleverbot support, but restrict it to a certain channel. - [ ] Integrate a function to make DougleyBot stream music to a certain voice channel. (discord.js is being rewritten to support voice.) - [ ] Create a function to make DougleyBot create text/voice channels, but restrict it to a certain permission. diff --git a/discord_bot.js b/discord_bot.js index 5955309..2e4bba2 100644 --- a/discord_bot.js +++ b/discord_bot.js @@ -6,10 +6,11 @@ */ //PMX breaks end-user functions //var pmx = require('pmx').init(); +var VersionChecker = require("./versioncheck"); var maintenance; -var version = "1.2.7"; +var version = "1.2.8"; var Discord = require("discord.js"); @@ -811,6 +812,14 @@ When all commands are loaded, start the connection to Discord! bot.on("ready", function () { loadFeeds(); + console.log("Initializing..."); + console.log("Checking for updates..."); + VersionChecker.getStatus(function(err, status) { + if (err) { error(err); } // error handle + if (status && status !== "failed") { + console.log(status); + } + }); console.log("Ready to begin! Serving in " + bot.channels.length + " channels"); bot.setPlayingGame(Math.floor(Math.random() * (max - min)) + min); }); diff --git a/package.json b/package.json index e1ba60a..6d9988d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "DougleyBot", - "version": "1.2.7", + "version": "1.2.8", "description": "Bot for Discord app", "readme": "README.md", "maintainers": [], @@ -34,4 +34,4 @@ "youtube-node": "1.2.x" }, "main": "discord_bot.js" -} +} \ No newline at end of file diff --git a/versioncheck.js b/versioncheck.js new file mode 100644 index 0000000..0203768 --- /dev/null +++ b/versioncheck.js @@ -0,0 +1,98 @@ +var Request = require("request"); + +// ======================================================================== +// Version Fetching +// ======================================================================== + +var version = require("../package.json").version.split("."); + +exports.getCurrentVersion = function() { return version.join("."); }; +exports.getCurrentMajor = function() { return version[0]; }; +exports.getCurrentMinor = function() { return version[1]; }; +exports.getCurrentPatch = function() { return version[2]; }; + +exports.getLatestVersion = function(callback) { + + // fetch latest version number from GitHub + Request("https://raw.githubusercontent.com/SteamingMutt/DougleyBot/master/package.json", function (error, response, body) { + + if (error) { return callback(error, null); } // error handle + + if (response.statusCode == 200) { + + var latest = JSON.parse(body).version.split("."); + return callback(null, JSON.parse(body).version); // return version + + } else { // some other response code... + console.log("versioncheck failed:", { response: response.statusCode }); + return callback(null, "failed"); + } + }); +}; + +exports.getLatestMajor = function(callback) { + this.getLatest(function(err, latest) { + if (err) { return callback(err, null); } // error handle + return callback(null, parseInt(latest.split(".")[0])); + }); +}; + +exports.getLatestMinor = function(callback) { + this.getLatest(function(err, latest) { + if (err) { return callback(err, null); } // error handle + return callback(null, parseInt(latest.split(".")[1])); + }); +}; + +exports.getLatestPatch = function(callback) { + this.getLatest(function(err, latest) { + if (err) { return callback(err, null); } // error handle + return callback(null, parseInt(latest.split(".")[2])); + }); +}; + +// ======================================================================== +// Version Checking +// ======================================================================== + +exports.getStatus = function(callback) { + + // fetch latest version number from GitHub + this.getLatestVersion(function(err, latest) { + + if (err) { return callback(err, null); } // error handle + if (latest === "versioncheck failed") { return callback(null, latest); } // failure handle + + // split result into an array + latest = latest.split("."); + + // create variables for differences + var majorDiff = parseInt(latest[0]) - parseInt(version[0]); + var minorDiff = parseInt(latest[1]) - parseInt(version[1]); + var patchDiff = parseInt(latest[2]) - parseInt(version[2]); + + // check for major updates + if (majorDiff < 0) { + return callback(null, "Bot is " + Math.abs(majorDiff) + " major versions ahead! (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); + } else if (majorDiff > 0) { + return callback(null, "Bot is " + Math.abs(majorDiff) + " major versions behind. (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); + } + + // check for minor updates + if (minorDiff < 0) { + return callback(null, "Bot is " + Math.abs(minorDiff) + " minor versions ahead! (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); + } else if (minorDiff > 0) { + return callback(null, "Bot is " + Math.abs(minorDiff) + " minor versions behind. (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); + } + + // check for patch updates + if (patchDiff < 0) { + return callback(null, "Bot is " + Math.abs(patchDiff) + " patch versions ahead! (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); + } else if (patchDiff > 0) { + return callback(null, "Bot is " + Math.abs(patchDiff) + " patch versions behind. (current: " + version.join(".") + ", latest: " + latest.join(".") + ")"); + } + + // up to date :) + return callback(null, "Bot is fully up to date. (version: " + version.join(".") + ")"); + }); +};