UNPKG

4.11 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _debugLevels = require('debug-levels');
8
9var _debugLevels2 = _interopRequireDefault(_debugLevels);
10
11var _botLang = require('bot-lang');
12
13var _botLang2 = _interopRequireDefault(_botLang);
14
15var _requireDir = require('require-dir');
16
17var _requireDir2 = _interopRequireDefault(_requireDir);
18
19var _async = require('async');
20
21var _async2 = _interopRequireDefault(_async);
22
23var _lodash = require('lodash');
24
25var _lodash2 = _interopRequireDefault(_lodash);
26
27var _util = require('./util');
28
29var _util2 = _interopRequireDefault(_util);
30
31function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
33const debug = (0, _debugLevels2.default)('SS:Message');
34
35const loadPlugins = function loadPlugins(path) {
36 const plugins = {};
37 try {
38 const pluginFiles = (0, _requireDir2.default)(path);
39 Object.keys(pluginFiles).forEach(file => {
40 // For transpiled ES6 plugins with default export
41 if (pluginFiles[file].default) {
42 pluginFiles[file] = pluginFiles[file].default;
43 }
44
45 Object.keys(pluginFiles[file]).forEach(func => {
46 debug.verbose('Loading plugin:', path, func);
47 plugins[func] = pluginFiles[file][func];
48 });
49 });
50 } catch (e) {
51 console.error(`Could not load plugins from ${ path }: ${ e }`);
52 }
53
54 return plugins;
55};
56// The message could be generated by a reply or raw input
57// If it is a reply, we want to save the ID so we can filter them out if said again
58class Message {
59 /**
60 * Creates a new Message object.
61 * @param {String} message - The cleaned message.
62 * @param {Object} options - The parameters.
63 * @param {String} options.original - The original message text.
64 * @param {Object} options.factSystem - The fact system to use.
65 * @param {String} [options.replyId] - If the message is based on a reply.
66 * @param {String} [options.clearConversation] - If you want to clear the conversation.
67 */
68 constructor(message, options, callback) {
69 debug.verbose(`Creating message from string: ${ message }`);
70
71 this.id = _util2.default.genId();
72 this.plugins = options.plugins || {};
73
74 // If this message is based on a Reply.
75 if (options.replyId) {
76 this.replyId = options.replyId;
77 }
78
79 if (options.clearConversation) {
80 this.clearConversation = options.clearConversation;
81 }
82
83 this.factSystem = options.factSystem;
84 this.createdAt = new Date();
85
86 /**
87 * We have a series of transforms that are applied to the input
88 * `original` is the message `EXACTLY AS WRITTEN` by the user
89 *
90 * `raw` leans on bot-land and expands contractions, and fixes spelling
91 * - We also remove frivilous words, and convert to us-english.
92 *
93 * `clean` has been stripped of all punctuation and left with a word token form.
94 */
95 this.original = message;
96 this.raw = _botLang2.default.replace.all(message).trim();
97 this.clean = _util2.default.cleanMessage(this.raw).trim();
98
99 debug.verbose('Message before cleaning: ', message);
100 debug.verbose('Message RAW: ', this.raw);
101 debug.verbose('Message CLEAN: ', this.clean);
102
103 const scope = _lodash2.default.merge({}, options.scope);
104 scope.message = this;
105
106 const self = this;
107 const eachPluginItor = (functionName, next) => {
108 const functionArgs = [];
109 functionArgs.push(err => next(err, null));
110 functionName.apply(scope, functionArgs);
111 };
112
113 _async2.default.each(this.plugins, eachPluginItor, () => {
114 callback(null, self);
115 });
116 }
117
118 static createMessage(message, options, callback) {
119 if (!message) {
120 debug.verbose('Message received was empty, callback immediately');
121 return callback(null, {});
122 }
123
124 // Built-in plugins
125 options.plugins = loadPlugins(`${ __dirname }/plugins`);
126
127 // For user plugins
128 if (options.pluginsPath) {
129 options.plugins = _lodash2.default.merge(loadPlugins(options.pluginsPath, options.plugins));
130 }
131
132 return new Message(message, options, callback);
133 }
134}
135
136exports.default = Message;
\No newline at end of file