<commit_before>var MongoClient = require('mongodb').MongoClient;
var platforms = require('./deathsnacks.js').PLATFORM;
var rewardTypes = require('./reward.js').TYPES;

var USERS_COLLECTION = 'users';

/**
 * Create a new user database instance
 *
 * @constructor
 * @param {string} mongoURL MongoDB url
 */
var Users = function(mongoURL) {
  this.mongoURL = mongoURL;
}

// Default settings for new users
Users.DEFAULT_SETTINGS = {
    platform: 'PC',
    items: [
      'alerts',
      'invasions',
      'news',
      'helmet',
      'clantech',
      'nightmareMod',
      'aura',
      'resource',
      'nitain',
      'voidKey',
      'skin',
      'weapon',
      'other'
    ]
};

/**
 * Add new user to the database
 * If the user didn't exist before, true is passed to the callback
 *
 * @param {string} chatID ID of the user
 * @param {function} callback Callback function
 */
Users.prototype.add = function(chatID, callback) {
  MongoClient.connect(this.mongoURL, function(err, db) {
    if(err) {
      callback(err, null);
    } else {
      var filter, update, options, c;

      c = db.collection(USERS_COLLECTION);

      filter = {
        chatID: chatID
      };

      update = {
        $setOnInsert: Users.DEFAULT_SETTINGS
      };

      options = {
        upsert: true
      };

      c.findOneAndUpdate(filter, update, options, function (err, r) {
        db.close();
        if(err) {
          callback(err, null);
        } else {
          if(!r.value) {
            // New user was added
            callback(null, true);
          } else {
            // Existing user
            callback(null, false);
          }
        }
      });
    }
  });
}

/**
 * Remove user from database
 *
 * @param {string} chatID ID of the user
 * @param {function} callback Callback function
 */
Users.prototype.remove = function(chatID, callback) {
  MongoClient.connect(this.mongoURL, function(err, db) {
    if(err) {
      callback(err);
    } else {
      var query, c;

      c = db.collection(USERS_COLLECTION);

      query = {
        chatID: chatID
      };

      c.deleteOne(query, function(err, result) {
        db.close();
        callback(err);
      });
    }
  });
}

/**
 * Returns an user's settings
 *
 * @param {string} chatID ID of the user
 * @param {object} projection Query projection
 * @param {function} callback Callback function
 */
Users.prototype.getSettings = function(chatID, projection, callback) {
  MongoClient.connect(this.mongoURL, function(err, db) {
    if(err) {
      callback(err, null);
    } else {
      var query, c;

      c = db.collection(USERS_COLLECTION);

      query = {
        chatID: chatID
      };

      projection._id = false;

      c.find(query).limit(1).project(projection).next(function(err, doc) {
        db.close();
        if(err) {
          callback(err, null);
        } else {
          callback(null, doc);
        }
      });
    }
  });
}

/**
 * Returns an user's platform. Convenience method
 *
 * @param {string} chatID ID of the user
 * @param {function} callback Callback function
 */
Users.prototype.getPlatform = function(chatID, callback) {
  this.getSettings(chatID, {platform: true}, function(err, res) {
    if(err) {
      callback(err, null);
    } else {
      callback(null, res.platform);
    }
  });
}

/**
 * Returns an user's tracked items. Convenience method
 *
 * @param {string} chatID ID of the user
 * @param {function} callback Callback function
 */
Users.prototype.getTrackedItems = function(chatID, callback) {
  this.getSettings(chatID, {items: true}, function(err, res) {
    if(err) {
      callback(err, null);
    } else {
      callback(null, res.items);
    }<commit_msg>Fix commands sent by untracked users crashing the bot<commit_after>var MongoClient = require('mongodb').MongoClient;
var platforms = require('./deathsnacks.js').PLATFORM;
var rewardTypes = require('./reward.js').TYPES;

var USERS_COLLECTION = 'users';

/**
 * Create a new user database instance
 *
 * @constructor
 * @param {string} mongoURL MongoDB url
 */
var Users = function(mongoURL) {
  this.mongoURL = mongoURL;
}

// Default settings for new users
Users.DEFAULT_SETTINGS = {
    platform: 'PC',
    items: [
      'alerts',
      'invasions',
      'news',
      'helmet',
      'clantech',
      'nightmareMod',
      'aura',
      'resource',
      'nitain',
      'voidKey',
      'skin',
      'weapon',
      'other'
    ]
};

/**
 * Add new user to the database
 * If the user didn't exist before, true is passed to the callback
 *
 * @param {string} chatID ID of the user
 * @param {function} callback Callback function
 */
Users.prototype.add = function(chatID, callback) {
  MongoClient.connect(this.mongoURL, function(err, db) {
    if(err) {
      callback(err, null);
    } else {
      var filter, update, options, c;

      c = db.collection(USERS_COLLECTION);

      filter = {
        chatID: chatID
      };

      update = {
        $setOnInsert: Users.DEFAULT_SETTINGS
      };

      options = {
        upsert: true
      };

      c.findOneAndUpdate(filter, update, options, function (err, r) {
        db.close();
        if(err) {
          callback(err, null);
        } else {
          if(!r.value) {
            // New user was added
            callback(null, true);
          } else {
            // Existing user
            callback(null, false);
          }
        }
      });
    }
  });
}

/**
 * Remove user from database
 *
 * @param {string} chatID ID of the user
 * @param {function} callback Callback function
 */
Users.prototype.remove = function(chatID, callback) {
  MongoClient.connect(this.mongoURL, function(err, db) {
    if(err) {
      callback(err);
    } else {
      var query, c;

      c = db.collection(USERS_COLLECTION);

      query = {
        chatID: chatID
      };

      c.deleteOne(query, function(err, result) {
        db.close();
        callback(err);
      });
    }
  });
}

/**
 * Returns an user's settings
 *
 * @param {string} chatID ID of the user
 * @param {object} projection Query projection
 * @param {function} callback Callback function
 */
Users.prototype.getSettings = function(chatID, projection, callback) {
  MongoClient.connect(this.mongoURL, function(err, db) {
    if(err) {
      callback(err, null);
    } else {
      var query, c;

      c = db.collection(USERS_COLLECTION);

      query = {
        chatID: chatID
      };

      projection._id = false;

      c.find(query).limit(1).project(projection).next(function(err, doc) {
        db.close();
        if(err) {
          callback(err, null);
        } else {
          callback(null, doc);
        }
      });
    }
  });
}

/**
 * Returns an user's platform, or the default if the user is not in the database.
 * Convenience method
 *
 * @param {string} chatID ID of the user
 * @param {function} callback Callback function
 */
Users.prototype.getPlatform = function(chatID, callback) {
  this.getSettings(chatID, {platform: true}, function(err, res) {
    if(err) {
      callback(err, null);
    } else {
      if(res) {
        callback(null, res.platform);
      } else {
        callback(null, Users.DEFAULT_SETTINGS.platform);
      }
    }
  });
}

/**
 * Returns an user's tracked items. Convenience method
 *
 * @param {string} chatID ID of the user
 * @param {function} callback Callback function
 */
Users.prototype.getTrackedItems = function(chatID, callback) {
  this.getSettings(chatID, {items: true}, function(err, res) {
    if(err) {
      callback(err, null);
    } else {
      callback(null, res.items);
    }
{'fraction_non_alphanumeric': 0.13442277279915657, 'fraction_numerical': 0.0002635740643120717, 'mean_word_length': 2.319772528433946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6092339', 'n_tokens_mistral': 2412, 'n_tokens_neox': 2374, 'n_words': 591}