Issues (208)

lib/Alfred.js (1 issue)

Severity
1
"use strict";
2
3
const logger = require('./logger');
4
const NestedError = require('nested-error-stacks');
5
6
7
/**
8
 * Represent the bot which can speak and listen to you
9
 */
10
class Alfred {
11
    /**
12
     * @public
13
     *
14
     * @param {Client}        client
15
     * @param {PluginManager} pluginManager
16
     */
17
    constructor(client, pluginManager) {
18
        this.logger = logger;
19
        this.client = client;
20
        this.pluginManager = pluginManager;
21
        this.initialized = false;
22
    }
23
24
    /**
25
     * @public
26
     *
27
     * @param {string} textToSpeech
28
     *
29
     * @returns {Promise.<null|Error>}
30
     */
31
    speak(textToSpeech) {
32
        if (this.initialized === false) {
33
            return Promise.reject(new Error('Alfred not initialized'));
34
        }
35
36
        return this.pluginManager.decorateTts(textToSpeech)
37
            .then(textToSpeech => this.client.speak(textToSpeech))
38
            .then(() => null)
39
            .catch(error => {
40
                const newError = new NestedError('Alfred::speak error', error);
41
                this.logError(newError);
42
43
                return Promise.reject(newError);
44
            })
45
        ;
46
    }
47
48
    /**
49
     * @public
50
     * Politely wake up Alfred before asking him something.
51
     *
52
     * He will wake up each of his leprechauns
53
     *
54
     * @returns {Promise.<null|Error>}
55
     */
56
    wakeUp() {
57
        logger.starting('Alfred');
58
        let haveInvalidPlugins = false;
0 ignored issues
show
The variable haveInvalidPlugins seems to be never used. Consider removing it.
Loading history...
59
        return this.pluginManager.init()
60
            .then(() => this.client.listen())
61
            .then(() => {
62
                this.initialized = true;
63
            })
64
            .then(() => this.speak('A votre écoute'))
65
            .then(() => logger.started('Alfred'))
66
            .then(() => null)
67
            .catch(error => {
68
                const newError = new NestedError('Alfred::wakeUp error', error);
69
                this.logError(newError);
70
71
                return Promise.reject(newError);
72
            })
73
        ;
74
    }
75
76
    sleep() {
77
        if (this.initialized === false) {
78
            return Promise.resolve();
79
        }
80
81
        const updateState = () => {
82
            this.initialized = false;
83
        };
84
        logger.stopping('Alfred');
85
86
        return this.speak('A votre service')
87
            .then(() => this.pluginManager.dispose())
88
            .then(() => this.client.stopListening())
89
            .then(updateState)
90
            .then(() => logger.stopped('Alfred'))
91
            .catch(error => {
92
                const newError = new NestedError('Alfred::sleep error', error);
93
94
                this.logError(newError);
95
                updateState();
96
97
                return Promise.reject(newError);
98
            })
99
        ;
100
    }
101
102
    /**
103
     * @private
104
     * @param {Error} error
105
     *
106
     * @returns {Error}
107
     */
108
    logError(error) {
109
        this.logger.error(`${error.message}`);
110
    }
111
}
112
113
module.exports = Alfred;
114