diff --git a/example/ding-dong-bot.ts b/example/ding-dong-bot.ts index d253cc49beb9e0343b90adc9e7d1b41f72d0ab8d..4005045280ad1313f639b86b974d06aaaaf80da5 100644 --- a/example/ding-dong-bot.ts +++ b/example/ding-dong-bot.ts @@ -69,11 +69,7 @@ bot .on('logout' , user => log.info('Bot', `${user.name()} logouted`)) .on('login' , user => { log.info('Bot', `${user.name()} logined`) - bot.say('Wechaty login') -}) -.on('error' , e => { - log.info('Bot', 'error: %s', e) - bot.say('Wechaty error: ' + e.message) + bot.say('Wechaty login').catch(console.error) }) .on('scan', (url, code) => { if (!/201|200/.test(String(code))) { @@ -111,12 +107,20 @@ bot bot.start() .catch(e => { log.error('Bot', 'init() fail: %s', e) - bot.quit() + bot.stop() process.exit(-1) }) +bot.on('error', async e => { + log.error('Bot', 'error: %s', e) + await bot.say('Wechaty error: ' + e.message).catch(console.error) + await bot.stop() +}) + finis((code, signal) => { const exitMsg = `Wechaty exit ${code} because of ${signal} ` console.log(exitMsg) - bot.say(exitMsg) + if (bot.logonoff()) { + bot.say(exitMsg).catch(console.error) + } }) diff --git a/src/puppet-web/bridge.ts b/src/puppet-web/bridge.ts index 12f78177ce236f68a7aeb99e8589c555226a76ea..7441887fb3d24a0378725a3978f9bd0ec628f0b0 100644 --- a/src/puppet-web/bridge.ts +++ b/src/puppet-web/bridge.ts @@ -287,7 +287,7 @@ export class Bridge extends EventEmitter { log.silly('PuppetWebBridge', 'quit() browser.close()-ed') } catch (e) { log.warn('PuppetWebBridge', 'quit() exception: %s', e) - this.emit('error', e) + // this.emit('error', e) } finally { this.state.off(true) } diff --git a/src/puppet-web/puppet-web.ts b/src/puppet-web/puppet-web.ts index c65fc6bfc2f15b55d1c7b2b7fc7fc95b5c6092d9..50542d433da2458a2f0bb15e9c1b5ff0460ff841 100644 --- a/src/puppet-web/puppet-web.ts +++ b/src/puppet-web/puppet-web.ts @@ -263,6 +263,7 @@ export class PuppetWeb extends Puppet { }) this.bridge.on('ding' , Event.onDing.bind(this)) + this.bridge.on('error' , e => this.emit('error', e)) this.bridge.on('log' , Event.onLog.bind(this)) this.bridge.on('login' , Event.onLogin.bind(this)) this.bridge.on('logout' , Event.onLogout.bind(this)) @@ -301,6 +302,11 @@ export class PuppetWeb extends Puppet { } public logined(): boolean { + log.warn('PuppetWeb', 'logined() DEPRECATED. use logonoff() instead.') + return this.logonoff() + } + + public logonoff(): boolean { return !!(this.user) } @@ -722,7 +728,7 @@ export class PuppetWeb extends Puppet { * send to `filehelper` for notice / log */ public async say(content: string): Promise { - if (!this.logined()) { + if (!this.logonoff()) { throw new Error('can not say before login') } diff --git a/src/puppet.ts b/src/puppet.ts index 869732f088069c75f132ac8ef524e1bf8c45c0c3..75e236149fda59f6e05edd5c45f00214e7f2f904 100644 --- a/src/puppet.ts +++ b/src/puppet.ts @@ -110,14 +110,24 @@ export abstract class Puppet extends EventEmitter implements Sayable { public abstract self() : Contact + /** + * Message + */ public abstract forward(message: MediaMessage, contact: Contact | Room) : Promise public abstract say(content: string) : Promise public abstract send(message: Message | MediaMessage) : Promise + /** + * Login / Logout + */ + public abstract logonoff() : boolean public abstract reset(reason?: string) : void public abstract logout() : Promise public abstract quit() : Promise + /** + * Misc + */ public abstract ding() : Promise /** diff --git a/src/wechaty.ts b/src/wechaty.ts index 329c87b03d122b82b40e6df63fe6002474a11ddc..ff73a5ed64b93a5899e7326feb145aec285f3300 100644 --- a/src/wechaty.ts +++ b/src/wechaty.ts @@ -523,6 +523,24 @@ export class Wechaty extends EventEmitter implements Sayable { return } + /** + * Get the logon / logoff state + * + * @returns {boolean} + * @example + * if (bot.logonoff()) { + * console.log('Bot logined') + * } else { + * console.log('Bot not logined') + * } + */ + public logonoff(): Boolean { + if (!this.puppet) { + return false + } + return this.puppet.logonoff() + } + /** * Get current user * diff --git a/test/puppet-web/puppet-web.spec.ts b/test/puppet-web/puppet-web.spec.ts index b2f69ca6b4559102870a2c1b25ca7f0a913e94a6..e3688d66b0e2555691d8f3a453b7721ec41785cb 100755 --- a/test/puppet-web/puppet-web.spec.ts +++ b/test/puppet-web/puppet-web.spec.ts @@ -64,13 +64,13 @@ test('login/logout events', sinonTest(async function (t: test.Test) { await pw.init() t.pass('should be inited') - t.is(pw.logined() , false , 'should be not logined') + t.is(pw.logonoff() , false , 'should be not logined') const EXPECTED_CHIPER = 'loginFired' const loginPromise = new Promise(r => pw.once('login', _ => r(EXPECTED_CHIPER))) pw.bridge.emit('login', 'TestPuppetWeb') t.is(await loginPromise, EXPECTED_CHIPER, 'should fired login event') - t.is(pw.logined(), true , 'should be logined') + t.is(pw.logonoff(), true , 'should be logined') t.ok((pw.bridge.getUserName as any).called, 'bridge.getUserName should be called') t.ok((pw.getContact as any).called, 'pw.getContact should be called') @@ -81,7 +81,7 @@ test('login/logout events', sinonTest(async function (t: test.Test) { const logoutPromise = new Promise((res, rej) => pw.once('logout', _ => res('logoutFired'))) pw.bridge.emit('logout') t.is(await logoutPromise, 'logoutFired', 'should fire logout event') - t.is(pw.logined(), false, 'should be logouted') + t.is(pw.logonoff(), false, 'should be logouted') await pw.quit() profile.destroy()