{"size": 1003, "ext": "js", "max_stars_repo_path": "commands/sendMessage.js", "max_stars_repo_name": "robdy/porygon-bot", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "commands/sendMessage.js", "max_issues_repo_name": "robdy/porygon-bot", "max_issues_repo_issues_event_min_datetime": "2018-05-28T12:58:49.000Z", "max_issues_repo_issues_event_max_datetime": "2019-05-14T14:52:30.000Z", "max_forks_repo_path": "commands/sendMessage.js", "max_forks_repo_name": "robdy/porygon-bot", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 41.7916666667, "max_line_length": 151, "alphanum_fraction": 0.6111665005}
'use strict';
const db = require('../services/db');

module.exports = {
  db_required: true,
  message_regex: /^[\.!](?:msg|tell|ask)\s+([^ ]+)\s+([^\s].*)$/,
  allow: ({isMod, isAuthenticated}) => isMod && isAuthenticated,
  response ({bot, message_match: [, recipient, text], author_match: [author], channel}) {
    // no sending to the bot
    if (recipient.toLowerCase() === bot.nick.toLowerCase()) {
      return {response_type: 'action', message: `slaps ${author}.`};
    }
    return db.conn.query('SELECT * FROM User U JOIN Alias A ON U.UserID = A.UserID WHERE A.Alias = ?', [`${recipient}`]).get(0).then(recipientInfo => {
      if (!recipientInfo) {
        return 'There was an error. Do I even know that person?';
      }
      return db.conn.query(
        'INSERT INTO Message (TargetID, SenderName, MessageText, Location) VALUES (?, ?, ?, ?)',
        [recipientInfo.UserID, author, text, channel]
      ).return(`Saved message for ${recipientInfo.MainNick}. Beep boop.`);
    });
  }
};
