Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Closes #11
  • Loading branch information
Remco authored and Remco committed Jan 13, 2016
1 parent 5b9bf3e commit 0d2a618
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 68 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 2.0.0-beta.4
Fixed `myapifilms_token` not existing.
Fixed problems with `++setowner`.
Added `debug_mode` and `verbose_logging`. *(Note, only enable these on request of the devs!)*
Added a config value that'll change the way `++help` functions.
Updated the layout of `config.json`, **meaning that users need to remake their config files.**

## 2.0.0-beta.3
Fixed `giphy.js` not having requires.
Fixed `suffix` not behaving accordingly.
Expand Down
122 changes: 115 additions & 7 deletions DougBot.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,67 @@
// DougBot 2.0 alpha
// DougBot 2.0 beta
// Define variables first
var Discord = require("discord.js");
var bot = new Discord.Client();
var ConfigFile = require("./config.json");
var Logger = require("./runtime/logger.js").Logger;
var DebugLogger = require("./runtime/logger.js").DebugModeLogger;
var VerboseLogger = require("./runtime/logger.js").VerboseModeLogger;
var ChatLogger = require("./runtime/logger.js").ChatLog;
var Commands = require("./runtime/commands.js").Commands;
var Permissions = require("./runtime/permissions.js");
var VersionChecker = require("./runtime/versionchecker.js");
var forever = require("forever");
var DebugMode;
var VerboseLog;

// Declare if debug mode or verbose logging is needed
if (ConfigFile.bot_settings.debug_mode === true){
DebugMode = true;
console.log("WARNING! Debug mode activated! Only use this if you're sure what you're doing!");
console.log("WARNING! Debug mode log may contain sensitive information, only share it with trustworthy people!.");
DebugLogger.debug("DEBUG MODE LOG: Debug mode has been enabled.");
DebugLogger.debug("DEBUG MODE LOG: WARNING! This log may contain sensitive information, only share it with trustworthy people!.");
DebugLogger.debug("DEBUG MODE LOG: Configuration information");
DebugLogger.debug("DEBUG MODE LOG: Master user: " + ConfigFile.permissions.masterUser);
DebugLogger.debug("DEBUG MODE LOG: Level 1 user: " + ConfigFile.permissions.level1);
DebugLogger.debug("DEBUG MODE LOG: Level 2 user: " + ConfigFile.permissions.level2);
DebugLogger.debug("DEBUG MODE LOG: Level 3 user: " + ConfigFile.permissions.level3);
DebugLogger.debug("DEBUG MODE LOG: Help mode: " + ConfigFile.bot_settings.help_mode);
DebugLogger.debug("DEBUG MODE LOG: Command prefix: " + ConfigFile.bot_settings.cmd_prefix);
DebugLogger.debug("DEBUG MODE LOG: Auto-Restart: " + ConfigFile.bot_settings.auto_restart);
if (ConfigFile.redis.host && ConfigFile.redis.port) {
DebugLogger.debug("DEBUG MODE LOG: Redis host: " + ConfigFile.redis.host);
DebugLogger.debug("DEBUG MODE LOG: Redis port: " + ConfigFile.redis.port);
} else if (ConfigFile.redis.url) {
DebugLogger.debug("DEBUG MODE LOG: Redis URL: " + ConfigFile.redis.url);
} else {
DebugLogger.debug("DEBUG MODE LOG: Invalid Redis configuration detected!");
}
DebugLogger.debug("DEBUG MODE LOG: End of configuration information.");
} else {
DebugMode = false;
}

if (ConfigFile.bot_settings.verbose_logging === true){
VerboseLog = true;
console.log("WARNING! Verbose logging activated! Only use this if you're sure what you're doing!");
} else {
VerboseLog = false;
}

// Error logger
bot.on("error", function(error) {
Logger.debug("Got an error: " + error);
Logger.error("Encounterd an error, please report this to the author of this bot, include any log files present in the logs folder.");
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: Encountered an error with discord.js most likely, got error: " + error);
}
});

// Ready announcment
bot.on("ready", function() {
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: Ready event fired.");
}
Logger.info("Joining pre-defined servers...");
for (var index in ConfigFile.join_on_launch){
bot.joinServer(ConfigFile.join_on_launch[index], function(error, server){
Expand All @@ -30,21 +75,24 @@ bot.on("ready", function() {

// Disconnected announcment
bot.on("disconnected", function() {
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: Disconnected from Discord.");
}
Logger.warn("Disconnected, if this wasn't a connection issue or on purpose, report this issue to the author of the bot.");
process.exit(0); // Disconnected announcments are not always an error, seeing that disconnections can be triggered by the user.
});

// Command checker
bot.on("message", function(msg) {
if (ConfigFile.log_chat === true && msg.channel.server) {
if (ConfigFile.bot_settings.log_chat === true && msg.channel.server) {
var d = new Date();
var n = d.toUTCString();
ChatLogger.info(n + ": " + msg.channel.server.name + ", " + msg.channel.name + ": " + msg.author.username + " said <" + msg + ">");
}
if (msg.author.equals(bot.user)) {
return;
}
var prefix = ConfigFile.cmd_prefix.split("");
var prefix = ConfigFile.bot_settings.cmd_prefix.split("");
if (msg.content.charAt(0) === prefix[0]) {
if (msg.content.charAt(1) === prefix[1] ){
Logger.info("Executing <" + msg.content + "> from " + msg.author.username);
Expand All @@ -57,62 +105,122 @@ bot.on("message", function(msg) {
return;
}
if (Commands[command]) {
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: Command detected.");
}
if (msg.channel.server) {
Permissions.GetLevel((msg.author.id + msg.channel.server.id), msg.author.id, function (err, level){
if (err){
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: GetLevel failed, got error: " + err);
}
Logger.debug("An error occured!");
return;
}
if (level >= Commands[command].level){
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: Execution of command allowed.");
}
if (!Commands[command].nsfw){
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: Safe for work command executed.");
}
Commands[command].fn(bot, msg, suffix);
return;
} else {
Permissions.GetNSFW(msg.channel, function (err, reply){
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: Command is NSFW, checking if channel allows that.");
}
if (err){
Logger.debug("Got an error! <" + err + ">");
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: NSFW channel check failed, got error: " + err);
}
bot.sendMessage(msg.channel, "Sorry, an error occured, try again later.");
return;
}
if (reply === "on"){
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: NSFW command successfully executed.");
}
Commands[command].fn(bot, msg, suffix);
return;
} else {
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: NSFW command execution failed because of channel settings.");
}
bot.sendMessage(msg.channel, "You cannot use NSFW commands in this channel!");
}});}
} else {
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: User has no permission to use that command.");
}
bot.sendMessage(msg.channel, "You don't have permission to use this command!");
return;
}
});
} else {
Permissions.GetLevel(0, msg.author.id, function (err, level){ // Value of 0 is acting as a placeholder, because in DM's. only global permissions apply.
Permissions.GetLevel(0, msg.author.id, function (err, level){ // Value of 0 is acting as a placeholder, because in DM's only global permissions apply.
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: DM command detected, getting global perms.");
}
if (err){
Logger.debug("An error occured!");
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: GetLevel failed, got error: " + err);
}
return;
}
if (level >= Commands[command].level){
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: User has sufficient global perms.");
}
Commands[command].fn(bot, msg, suffix);
return;
} else {
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: User does not have enough global permissions.");
}
bot.sendMessage(msg.channel, "Only global permissions apply in DM's, your server specific permissions do nothing here!");
}
});
}
}}}});

// Initial functions
function init() {
function init(token) {
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: Sucessfully logged into Discord, returned token: " + token);
DebugLogger.debug("DEBUG MODE LOG: Continuing start-up sequence.");
}
Logger.info("Loading DougleyBot...");
Logger.info("Checking for updates...");
VersionChecker.getStatus(function(err, status) {
if (err) {
Logger.error(err);
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: Version checking failed, got error: " + err);
}
} // error handle
if (status && status !== "failed") {
Logger.info(status);
}
});
}

function err(error) {
if (DebugMode === true){
DebugLogger.debug("DEBUG MODE LOG: Logging into Discord probably failed, got error: " + error);
}
}

process.on("beforeExit", function() {
Logger.info("About to exit Node!");
if (ConfigFile.bot_settings.auto_restart === true){
Logger.info("Auto restart initiated...");
forever.start("-m 1 DougBot.js");
}
});
// Connection starter
bot.login(ConfigFile.email, ConfigFile.password).then(init);
bot.login(ConfigFile.discord.email, ConfigFile.discord.password).then(init).catch(err);
8 changes: 8 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Reintroduce a timeout feature, but better. *(RC 1)*
Music streaming, maybe rewrite to discord.io or discordie to make this easier. *(Proposed)*
Move to MongoDB instead of Redis. *(RC 1)*
Make command prefixes dynamic in their length requirement. *(Beta 5)*
Make `memes.json` update automatically with new memes from Imgflip. *(RC 2)*
`++remindme`. *(RC 1)*
OOTB experience improvements. *(Beta 5)*
???
50 changes: 50 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"useful_comments": [
"Please only enable debug_mode or verbose_logging if requested by one of the devs, as enabling this extends the logging capabilities and could potentially include sensitive info.",
"Command prefixes are double prefix activation ONLY.",
"There can only be 1 master user, 1 global level 1 user, etc.",
"If you define a different path to the image or music directory, type it like this:", "C:\\Users\\foobar\\Music",
"help_mode can either be `private` or `channel`",
"auto_restart tries to reconnect to Discord after an error or disconnection automatically."
],

"discord" : {
"email" : "foo@bar.net",
"password" : "hunter2"
},
"bot_settings" : {
"cmd_prefix": "++",
"help_mode": "private",
"log_chat": false,
"verbose_logging": false,
"auto_restart": false,
"debug_mode": false,
"image_folder": "./images",
"music_folder": "./music"
},
"join_on_launch" : [
"https://discord.gg/0cFoiR5QVh4agupi",
"https://discord.gg/0cFoiR5QVh57Spqg"
],
"api_keys" : {
"google_key": "Create one here: https://console.developers.google.com",
"cse_key": "https://github.com/SteamingMutt/DougleyBot/wiki/CSE",
"mashape_key": "https://market.mashape.com/dashboard",
"myapifilms_token": "token",
},
"imgflip" : {
"username": "foobar",
"password": "hunter2"
},
"permissions": {
"masterUser": "Master user Discord ID",
"level1": "Global level 1 user Discord ID",
"level2": "Global level 2 user Discord ID",
"level3": "Global level 3 user Discord ID"
},
"redis" : {
"host" : "localhost",
"port" : 6379,
"url" : "If you need to connect to a Redis URL, use this key and remove `host` and `port`, if you don't need this, remove this key and the comma at the end of `port`"
},
}
27 changes: 0 additions & 27 deletions config.json.example

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "DougleyBot",
"version": "2.0.0-beta.3",
"version": "2.0.0-beta.4",
"description": "A Discord bot",
"readme": "README.md",
"maintainers": ["Perpetucake"],
Expand All @@ -22,6 +22,7 @@
"csgo-market": "*",
"unirest": "*",
"youtube-node": "1.2.x",
"forever": "*",
"redis": "*"
},
"main": "DougBot.js"
Expand Down
Loading

0 comments on commit 0d2a618

Please sign in to comment.