1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _debugLevels = require('debug-levels');
|
8 |
|
9 | var _debugLevels2 = _interopRequireDefault(_debugLevels);
|
10 |
|
11 | var _botLang = require('bot-lang');
|
12 |
|
13 | var _botLang2 = _interopRequireDefault(_botLang);
|
14 |
|
15 | var _requireDir = require('require-dir');
|
16 |
|
17 | var _requireDir2 = _interopRequireDefault(_requireDir);
|
18 |
|
19 | var _async = require('async');
|
20 |
|
21 | var _async2 = _interopRequireDefault(_async);
|
22 |
|
23 | var _lodash = require('lodash');
|
24 |
|
25 | var _lodash2 = _interopRequireDefault(_lodash);
|
26 |
|
27 | var _util = require('./util');
|
28 |
|
29 | var _util2 = _interopRequireDefault(_util);
|
30 |
|
31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
32 |
|
33 | const debug = (0, _debugLevels2.default)('SS:Message');
|
34 |
|
35 | const plugins = {};
|
36 |
|
37 |
|
38 | const loadPlugins = function loadPlugins(path) {
|
39 | try {
|
40 | const pluginFiles = (0, _requireDir2.default)(path);
|
41 | Object.keys(pluginFiles).forEach(file => {
|
42 |
|
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 |
|
58 | loadPlugins(`${ __dirname }/plugins`);
|
59 |
|
60 |
|
61 |
|
62 | class Message {
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
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 |
|
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 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
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 |
|
122 | const 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 |
|
131 | exports.default = {
|
132 | createMessage,
|
133 | loadPlugins
|
134 | }; |
\ | No newline at end of file |