@@ -14,7 +14,7 @@ import { ContextFetchOptions, Listener, ListenerData } from 'src/listener/Listen
1414import { ServiceModule } from 'src/module/ServiceModule' ;
1515import { Parser , ParserData } from 'src/parser/Parser' ;
1616import { Service , ServiceDefinition , ServiceEvent } from 'src/Service' ;
17- import { filterNil , mustFind } from 'src/utils' ;
17+ import { filterNil , mustFind , mustExist } from 'src/utils' ;
1818import { incrementServiceCounter } from 'src/utils/metrics/Service' ;
1919import { StorageLogger , StorageLoggerOptions } from 'src/utils/StorageLogger' ;
2020
@@ -39,11 +39,11 @@ export class Bot extends BaseService<BotData> implements Service {
3939 protected readonly container : Container ;
4040 protected readonly metrics : Registry ;
4141
42- protected storage : Connection ;
42+ protected storage ? : Connection ;
4343
4444 // counters
45- protected cmdCounter : Counter ;
46- protected msgCounter : Counter ;
45+ protected cmdCounter ! : Counter ;
46+ protected msgCounter ! : Counter ;
4747
4848 // services
4949 protected controllers : Array < Controller > ;
@@ -77,11 +77,11 @@ export class Bot extends BaseService<BotData> implements Service {
7777 this . incoming = new Subject ( ) ;
7878 this . outgoing = new Subject ( ) ;
7979
80- bindAll ( this , 'looseError' ) ;
80+ this . startMetrics ( ) ;
8181 }
8282
8383 public getStorage ( ) : Connection {
84- return this . storage ;
84+ return mustExist ( this . storage ) ;
8585 }
8686
8787 public async notify ( event : ServiceEvent ) {
@@ -103,17 +103,13 @@ export class Bot extends BaseService<BotData> implements Service {
103103 */
104104 public async start ( ) {
105105 await super . start ( ) ;
106-
107106 this . logger . info ( 'starting bot' ) ;
108107
109- this . logger . info ( 'setting up streams' ) ;
110- /* tslint:disable:no-unbound-method */
111- this . commands . subscribe ( ( next ) => this . receiveCommand ( next ) . catch ( this . looseError ) ) ;
112- this . incoming . subscribe ( ( next ) => this . receive ( next ) . catch ( this . looseError ) ) ;
113- this . outgoing . subscribe ( ( next ) => this . receiveMessage ( next ) . catch ( this . looseError ) ) ;
114- /* tslint:enable */
108+ const streamError = ( err : Error ) => this . looseError ( err ) ;
109+ this . commands . subscribe ( ( next ) => this . receiveCommand ( next ) . catch ( streamError ) ) ;
110+ this . incoming . subscribe ( ( next ) => this . receive ( next ) . catch ( streamError ) ) ;
111+ this . outgoing . subscribe ( ( next ) => this . receiveMessage ( next ) . catch ( streamError ) ) ;
115112
116- await this . startMetrics ( ) ;
117113 await this . startStorage ( ) ;
118114 await this . startServices ( ) ;
119115
@@ -174,9 +170,10 @@ export class Bot extends BaseService<BotData> implements Service {
174170 * Add a message to the send queue.
175171 */
176172 public async sendMessage ( ...messages : Array < Message > ) : Promise < Array < Message > > {
173+ const storage = mustExist ( this . storage ) ;
177174 const results = [ ] ;
178175 for ( const data of messages ) {
179- const msg = await this . storage . getRepository ( Message ) . save ( data ) ;
176+ const msg = await storage . getRepository ( Message ) . save ( data ) ;
180177 this . logger . debug ( { msg } , 'message saved' ) ;
181178 this . outgoing . next ( msg ) ;
182179 results . push ( msg ) ;
@@ -185,9 +182,10 @@ export class Bot extends BaseService<BotData> implements Service {
185182 }
186183
187184 public async executeCommand ( ...commands : Array < Command > ) : Promise < Array < Command > > {
185+ const storage = mustExist ( this . storage ) ;
188186 const results = [ ] ;
189187 for ( const data of commands ) {
190- const cmd = await this . storage . getRepository ( Command ) . save ( data ) ;
188+ const cmd = await storage . getRepository ( Command ) . save ( data ) ;
191189 this . commands . next ( cmd ) ;
192190 results . push ( cmd ) ;
193191 }
@@ -281,7 +279,7 @@ export class Bot extends BaseService<BotData> implements Service {
281279 return commands ;
282280 }
283281
284- protected async startMetrics ( ) {
282+ protected startMetrics ( ) {
285283 this . logger . info ( 'setting up metrics' ) ;
286284
287285 this . cmdCounter = new Counter ( {
@@ -354,7 +352,7 @@ export class Bot extends BaseService<BotData> implements Service {
354352 * Note: this method is already bound, so it can be passed with `this.looseError`. Using that requires
355353 * `tslint:disable:no-unbound-method` as well.
356354 */
357- protected async looseError ( err : Error ) {
355+ protected looseError ( err : Error ) {
358356 this . logger . error ( err , 'bot stream did not handle error' ) ;
359357 }
360358}
0 commit comments