UNPKG

3.97 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 plugins = {};
36
37// To load user plugins, just run Message.loadPlugins
38const loadPlugins = function loadPlugins(path) {
39 try {
40 const pluginFiles = (0, _requireDir2.default)(path);
41 Object.keys(pluginFiles).forEach(file => {
42 // For transpiled ES6 plugins with default export
43 if (pluginFiles[file].default) {
44 pluginFiles[file] = pluginFiles[file].default;
45 }
46
47 Object.keys(pluginFiles[file]).forEach(func => {
48 debug.verbose('Loading plugin:', path, func);
49 plugins[func] = pluginFiles[file][func];
50 });
51 });
52 } catch (e) {
53 console.error(`Could not load plugins from ${ path }: ${ e }`);
54 }
55};
56
57// Load built-in plugins
58loadPlugins(`${ __dirname }/plugins`);
59
60// The message could be generated by a reply or raw input
61// If it is a reply, we want to save the ID so we can filter them out if said again
62class Message {
63 /**
64 * Creates a new Message object.
65 * @param {String} message - The cleaned message.
66 * @param {Object} options - The parameters.
67 * @param {String} options.original - The original message text.
68 * @param {Object} options.factSystem - The fact system to use.
69 * @param {String} [options.replyId] - If the message is based on a reply.
70 * @param {String} [options.clearConversation] - If you want to clear the conversation.
71 */
72 constructor(message, options, callback) {
73 debug.verbose(`Creating message from string: ${ message }`);
74
75 this.id = _util2.default.genId();
76 this.plugins = options.plugins || {};
77
78 // If this message is based on a Reply.
79 if (options.replyId) {
80 this.replyId = options.replyId;
81 }
82
83 if (options.clearConversation) {
84 this.clearConversation = options.clearConversation;
85 }
86
87 this.factSystem = options.factSystem;
88 this.createdAt = new Date();
89
90 /**
91 * We have a series of transforms that are applied to the input
92 * `original` is the message `EXACTLY AS WRITTEN` by the user
93 *
94 * `raw` leans on bot-land and expands contractions, and fixes spelling
95 * - We also remove frivilous words, and convert to us-english.
96 *
97 * `clean` has been stripped of all punctuation and left with a word token form.
98 */
99 this.original = message;
100 this.raw = _botLang2.default.replace.all(message).trim();
101 this.clean = _util2.default.cleanMessage(this.raw).trim();
102
103 debug.verbose('Message before cleaning: ', message);
104 debug.verbose('Message RAW: ', this.raw);
105 debug.verbose('Message CLEAN: ', this.clean);
106
107 const scope = _lodash2.default.merge({}, options.scope);
108 scope.message = this;
109
110 const eachPluginItor = (functionName, next) => {
111 const functionArgs = [];
112 functionArgs.push(err => next(err));
113 functionName.apply(scope, functionArgs);
114 };
115
116 _async2.default.each(plugins, eachPluginItor, () => {
117 callback(null, this);
118 });
119 }
120}
121
122const createMessage = function createMessage(message, options, callback) {
123 if (!message) {
124 debug.verbose('Message received was empty, callback immediately');
125 return callback(null, {});
126 }
127
128 return new Message(message, options, callback);
129};
130
131exports.default = {
132 createMessage,
133 loadPlugins
134};
\No newline at end of file