Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
Added a version checker
Browse files Browse the repository at this point in the history
Directly lifted from Neko.js from @TehSeph
  • Loading branch information
SteamingMutt authored and SteamingMutt committed Dec 4, 2015
1 parent bbdecaa commit a7cbb8a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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.
11 changes: 10 additions & 1 deletion discord_bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "DougleyBot",
"version": "1.2.7",
"version": "1.2.8",
"description": "Bot for Discord app",
"readme": "README.md",
"maintainers": [],
Expand Down Expand Up @@ -34,4 +34,4 @@
"youtube-node": "1.2.x"
},
"main": "discord_bot.js"
}
}
98 changes: 98 additions & 0 deletions versioncheck.js
Original file line number Diff line number Diff line change
@@ -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(".") + ")");
});
};

2 comments on commit a7cbb8a

@TehSeph
Copy link

@TehSeph TehSeph commented on a7cbb8a Dec 4, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enjoy my messy code! :D

@Dougley
Copy link
Owner

@Dougley Dougley commented on a7cbb8a Dec 4, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :P

Please sign in to comment.