<commit_before>"use strict";

class CommandManager {
  constructor(container) {
    this.container = container;

    this.bot = null;
    this.logger = container.get('logger');

    this._commands = {};
    this._prefix = null;
  }

  botReady() {
    this.bot = this.container.get('bot');
  }

  setPrefix(prefix) {
    this._prefix = prefix;
  }

  add(mod, cmd, func) {
    this._commands[cmd] = func;
  }

  /**
   * Precondition: msg != null, msg.content != null
   */
  handle(msg, errCb) {
    const content = msg.content.trim();

    if (msg.author.id === this.bot.client.User.id || !content.startsWith(this._prefix)) {
      return false;
    }

    this.logger.debug(`handling ${content}`);

    const fullCmd = content.slice(this._prefix.length);
    const args = fullCmd.split(' ').filter(x => x.length);
    const name = args[0].toLowerCase();

    this.logger.debug(`args: ${args}`);

    const func = this._commands[name];
    if (func) {
      this.logger.debug(`found registered command for ${name}`);

      let res = null;
      try {
        res = func(msg, args.slice(1));
      } catch (err) {
        errCb(err);
      }
    }

    return false;
  }
}

module.exports = CommandManager;<commit_msg>CommandManager: handle non-prefixed commands separately<commit_after>"use strict";

const util = require('util');

class CommandManager {
  constructor(container) {
    this.container = container;

    this.bot = null;
    this.logger = container.get('logger');

    this._commands = {};
    this._listeners = new Map();
    this._prefix = null;
  }

  botReady() {
    this.bot = this.container.get('bot');
  }

  setPrefix(prefix) {
    this._prefix = prefix;
  }

  add(mod, cmd, func, withNoPrefix) {
    if (withNoPrefix) {
      this._listeners.set(cmd, func);
    } else {
      this._commands[cmd] = func;
    }
  }

  handleCommands(msg, errCb) {
    const content = msg.content.trim();

    if (!content.startsWith(this._prefix)) {
      return false;
    }

    this.logger.debug(`handling ${content}`);

    const fullCmd = content.slice(this._prefix.length);
    const args = fullCmd.split(' ').filter(x => x.length);
    const name = args[0].toLowerCase();

    this.logger.debug(`args: ${args}`);

    const func = this._commands[name];
    if (func) {
      this.logger.debug(`found registered command for ${name}`);

      let res = null;
      try {
        res = func(msg, args.slice(1));
      } catch (err) {
        errCb(err);
      }

      return res;
    }

    return false;
  }

  handleListeners(msg, errCb) {
    const content = msg.content.trim();

    this.logger.debug(`Handling ${util.format(content)}`);

    for (const arr of this._listeners) {
      const cmd = arr[0];
      const func = arr[1];
      const contentMatches = cmd.test(content);

      if (contentMatches) {
        this.logger.debug(`${cmd} matches content`);

        try {
          func(msg);
        } catch (err) {
          errCb(err);
        }
      }
    }
  }

  handle(msg, errCb) {
    if (msg.author.id === this.bot.client.User.id) {
      return false;
    }

    this.handleListeners(msg, errCb);
    return this.handleCommands(msg, errCb);
  }
}

module.exports = CommandManager;
{'fraction_non_alphanumeric': 0.15532774153463808, 'fraction_numerical': 0.001863932898415657, 'mean_word_length': 2.492407809110629, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '475178', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1049, 'n_words': 213}