db.query('SELECT UserPermission AS userPerm FROM UserData WHERE UserID=?', fromID, function (err, result) {
   if (err) throw err;
   else {
     if (foxbot.foxy.botData.botLoaded === true) {
       var userPerm = result[0].userPerm;
       if (userPerm === foxbot.foxy.perm.manager || userPerm === foxbot.foxy.perm.cohost || userPerm === foxbot.foxy.perm.host) {
         fs.readdir('./foxbot_modules_test/', function(err, files) {
           if (err) throw err;
           else {
             var moduleCount = files.length;
             for (var i = 0; i < moduleCount; i++) {
               var module = files[i].substring(0,files[i].lastIndexOf('.'));
               foxbot.unloadModule(module);
               foxbot.foxy.modules[module] = false;
               foxbot.log(foxbot.console.info + 'Module ' + module + ' successfully unloaded');
             }
           }
         });
         foxbot.log(foxbot.console.info + 'Bot stopped successfully');
         foxbot.foxy.botData.botLoaded = false;
       }
     }
     else {
       foxbot.log(foxbot.console.info + 'Bot was not running');
     }
   }
 });
 db.on('error', function (err) {
   if (!err.fatal) return;
   if (err.code !== 'PROTOCOL_CONNECTION_LOST') throw err;
     foxbot.log(foxbot.console.info + 'Re-connecting lost MySQL connection: ' + err.stack);
     db = mysql.createConnection(dbconfig);
     foxbot.handleDisconnect(db);
     db.connect();
 });
 db.query('SELECT UserName AS username FROM UserData WHERE UserID=?', user.id, function (err, result) {
   if (err) {
     foxbot.handleDisconnect(db);
   }
   else {
     var username = result[0].username;
     foxbot.log(foxbot.console.leave + username + ' left the room');
   }
 });
 fs.readdir('./foxbot_modules_test/', function(err, files) {
   if (err) throw err;
   else {
     var moduleCount = files.length;
     for (var i = 0; i < moduleCount; i++) {
       var module = files[i].substring(0,files[i].lastIndexOf('.'));
       foxbot.unloadModule(module);
       foxbot.foxy.modules[module] = false;
       foxbot.log(foxbot.console.info + 'Module ' + module + ' successfully unloaded');
     }
   }
 });
foxbot.on('userJoin', function(user) {
  foxbot.log(foxbot.console.join + user.username + ' joined the room');
  foxbot.foxy.userData.lastJoinTime = 0;
  foxbot.foxy.userData.currentJoinTime = Date.now();
  var selUser = foxbot.getUser(user.id);
  var updateUser = {
    UserName          : selUser.username,
    UserPermission    : selUser.permission,
    UserAvatar        : selUser.avatarID,
    UserStatus        : selUser.status,
    UserFans          : selUser.fans,
    UserLanguage      : selUser.language,
    UserListenerPoints: selUser.listenerPoints,
    UserCuratorPoints : selUser.curatorPoints,
    UserDJPoints      : selUser.djPoints,
    UserOnline        : 1
  };
  db.query('INSERT INTO UserData SET ? ON DUPLICATE KEY UPDATE ?', [require('merge')({UserID: user.id}, updateUser), updateUser], function (err) {
    if (err) {
      foxbot.handleDisconnect(db);
    }
  });
  db.query('SELECT UserJoinTime AS lastJoinTime FROM UserData WHERE UserID=?', user.id, function (err, result) {
    if (err) {
      foxbot.handleDisconnect(db);
    }
    else {
      foxbot.foxy.userData.lastJoinTime = result[0].lastJoinTime;
    }
  });
  db.query('UPDATE UserData SET UserJoinTime=? WHERE UserID=?', [foxbot.foxy.userData.currentJoinTime, user.id], function (err) {
    if (err) throw err;
  });
  db.query('SELECT UserID AS userCountDB FROM UserData WHERE UserOnline=?', 1, function (err, result) {
    if (err) {
      foxbot.handleDisconnect(db);
    }
    else {
      foxbot.foxy.roomData.userCount = result.length;
    }
  });
});
var reconnect = function() {
  foxbot.log('Bot restarted itself');
  foxbot.connect(ROOM);
};
foxbot.on('roomJoin', function() {
  foxbot.populateDB();
  foxbot.foxy.botData.startTime = Date.now();
  foxbot.log('Bot joined the ' + ROOM + ' room.');
});