module.exports = (lib) => {
  lib.bot.on('join', (e) => {
    lib.bot.whois(e.user.getNick(), e.network, (err, data) => {
      if(data.account !== null) {
        lib.getUserlevel(e.user.getNick(), e.network, e.channel, (cb) => {
          var modes = lib.levelToModes(cb.level);
          modes = '+' + modes;
          if(modes !== "")
            lib.bot.mode(e.channel, modes + (' '+e.user.getNick()).repeat(modes.length-1), e.network);
        });
      }
    });
    if(e.channel.names && e.user.nick !== lib.bot.me.nick) {
      lib.bot.names(e.channel.getName(), e.network, (names) => {
        e.channel.names = names;
        var userCount = Object.keys(e.channel.names).length;
        lib.sql.query('select value from `f0ck`.`vars` where `key` = "highest_user_count" AND `channel` = ? AND `network` = ? limit 1', [e.channel.getName(), e.network], (err, rows) => {
          if(!rows.length) {
            lib.sql.query('insert into `f0ck`.`vars` (`key`,`value`,`channel`,`network`) values ("highest_user_count",?,?,?)', [userCount, e.channel.getName(), e.network]);
            e.reply('New user record for ' + e.channel.getName() + ': ' + userCount);
          }
          else if(parseInt(rows[0].value) < userCount) {
            lib.sql.query('update `f0ck`.`vars` set `value` = ? where `key` = "highest_user_count" AND `channel` = ? AND `network` = ? limit 1', [userCount, e.channel.getName(), e.network]);
            e.reply('New user record for ' + e.channel.getName() + ': ' + userCount);
          }
        });
      });
    }
  });
};