<commit_before>require('dotenv').config(); // Read .env for local dev

import Botkit from 'botkit';
import winston from 'winston';

import {SQLiteStore} from './lib/store';
import {MemoryBotEngine} from './lib/engine';

const log = winston;
log.remove(winston.transports.Console);
log.add(winston.transports.Console, {timestamp: true, level: 'debug'});

for (let name of ['SLACK_TOKEN', 'DATA_DIR']) {
  if (!process.env[name]) {
    throw new Error(`Missing ${name} environment variable`);
  }
}

const store = new SQLiteStore(process.env.DATA_DIR);
const engine = new MemoryBotEngine(store);
const controller = Botkit.slackbot({
  debug: process.env.DEBUG_SLACK,
  send_via_rtm: true
});

controller.on('hello', (bot, _) => {
  let team = bot.team_info.id;
  store.initialize(team, err => {
    if (err) return log.error(`Couldn't initialize database: ${err}`);
    store.updateBotMetadata(bot, err => {
      if (err) return log.error(`Couldn't update bot metadata: ${err}`);
    });
  });
});

controller.on('direct_mention', (bot, msg) => {
  return handleMessage(bot, msg.user, msg.channel, true, msg.text);
});

controller.on('direct_message', (bot, msg) => {
  return handleMessage(bot, msg.user, msg.channel, true, msg.text);
});

controller.on('me_message', (bot, msg) => {
  handleMessage(bot, msg.user, msg.channel, false, msg.text);
});

controller.on('ambient', (bot, msg) => {
  let name = bot.identity ? bot.identity.name.toLowerCase() : null;
  let {text} = msg;
  // Sometimes users might say "membot hey" or "membot: hey" instead of "@membot hey"
  if (text.toLowerCase().startsWith(`${name} `) || text.toLowerCase().startsWith(`${name}: `)) {
    text = text.substr(name.length + 1).replace(/^:\s+/, '');
    handleMessage(bot, msg.user, msg.channel, true, text);
  } else {
    handleMessage(bot, msg.user, msg.channel, false, text);
  }
});

// Cache Slack user IDs to names in memory.
let userIdsToNames = {};

var handleMessage = (bot, sender, channel, isDirect, msg) => {
  if (sender in userIdsToNames) {
    engine.handleMessage(bot, userIdsToNames[sender], channel, isDirect, msg, err => {
      if (err) return log.error(`handleMessage failed: ${err}`);
    });
  } else {
    bot.api.users.info({user: sender}, (err, data) => {
      if (err) return log.error(`Could not call users.info for user ${sender}: ${err}`);
      let name = data ? data.user.name : sender;
      userIdsToNames[sender] = name;
      engine.handleMessage(bot, name, channel, isDirect, msg, err => {
        if (err) return log.error(`handleMessage failed: ${err}`);
      });
    });
  }
};

log.info("Starting memorybot...");
const bot = controller.spawn({token: process.env.SLACK_TOKEN});
function startRTM() {
  bot.startRTM((err, bot, payload) => {
    if (err) {<commit_msg>Fix unused parameter<commit_after>require('dotenv').config(); // Read .env for local dev

import Botkit from 'botkit';
import winston from 'winston';

import {SQLiteStore} from './lib/store';
import {MemoryBotEngine} from './lib/engine';

const log = winston;
log.remove(winston.transports.Console);
log.add(winston.transports.Console, {timestamp: true, level: 'debug'});

for (let name of ['SLACK_TOKEN', 'DATA_DIR']) {
  if (!process.env[name]) {
    throw new Error(`Missing ${name} environment variable`);
  }
}

const store = new SQLiteStore(process.env.DATA_DIR);
const engine = new MemoryBotEngine(store);
const controller = Botkit.slackbot({
  debug: process.env.DEBUG_SLACK,
  send_via_rtm: true
});

controller.on('hello', (bot, _) => {
  let team = bot.team_info.id;
  store.initialize(team, err => {
    if (err) return log.error(`Couldn't initialize database: ${err}`);
    store.updateBotMetadata(bot, err => {
      if (err) return log.error(`Couldn't update bot metadata: ${err}`);
    });
  });
});

controller.on('direct_mention', (bot, msg) => {
  return handleMessage(bot, msg.user, msg.channel, true, msg.text);
});

controller.on('direct_message', (bot, msg) => {
  return handleMessage(bot, msg.user, msg.channel, true, msg.text);
});

controller.on('me_message', (bot, msg) => {
  handleMessage(bot, msg.user, msg.channel, false, msg.text);
});

controller.on('ambient', (bot, msg) => {
  let name = bot.identity ? bot.identity.name.toLowerCase() : null;
  let {text} = msg;
  // Sometimes users might say "membot hey" or "membot: hey" instead of "@membot hey"
  if (text.toLowerCase().startsWith(`${name} `) || text.toLowerCase().startsWith(`${name}: `)) {
    text = text.substr(name.length + 1).replace(/^:\s+/, '');
    handleMessage(bot, msg.user, msg.channel, true, text);
  } else {
    handleMessage(bot, msg.user, msg.channel, false, text);
  }
});

// Cache Slack user IDs to names in memory.
let userIdsToNames = {};

var handleMessage = (bot, sender, channel, isDirect, msg) => {
  if (sender in userIdsToNames) {
    engine.handleMessage(bot, userIdsToNames[sender], channel, isDirect, msg, err => {
      if (err) return log.error(`handleMessage failed: ${err}`);
    });
  } else {
    bot.api.users.info({user: sender}, (err, data) => {
      if (err) return log.error(`Could not call users.info for user ${sender}: ${err}`);
      let name = data ? data.user.name : sender;
      userIdsToNames[sender] = name;
      engine.handleMessage(bot, name, channel, isDirect, msg, err => {
        if (err) return log.error(`handleMessage failed: ${err}`);
      });
    });
  }
};

log.info("Starting memorybot...");
const bot = controller.spawn({token: process.env.SLACK_TOKEN});
function startRTM() {
  bot.startRTM((err) => {
    if (err) {
{'fraction_non_alphanumeric': 0.1798237727027513, 'fraction_numerical': 0.0003596475454055026, 'mean_word_length': 4.116835326586936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '4275917', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1862, 'n_words': 464}