提交 96b931e1 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

code clean

上级 701a6a37
......@@ -80,26 +80,22 @@ bot
})
}
})
.on('message', async m => {
.on('message', async msg => {
try {
const room = m.room()
console.log(
(room ? `${room}` : '')
+ `${m.from()}:${m}`,
)
console.log(msg.toString())
if (/^(ding|ping|bing|code)$/i.test(m.text()) && !m.self()) {
if (/^(ding|ping|bing|code)$/i.test(msg.text()) && !msg.self()) {
/**
* 1. reply 'dong'
*/
log.info('Bot', 'REPLY: dong')
m.say('dong')
msg.say('dong')
const joinWechaty = `Join Wechaty Developers' Community\n\n` +
`Wechaty is used in many ChatBot projects by hundreds of developers.\n\n` +
`If you want to talk with other developers, just scan the following QR Code in WeChat with secret code: wechaty,\n\n` +
`you can join our Wechaty Developers' Home at once`
await m.say(joinWechaty)
await msg.say(joinWechaty)
/**
* 2. reply qrcode image
......@@ -107,12 +103,12 @@ bot
const fileBox = FileBox.fromLocal(BOT_QR_CODE_IMAGE_FILE)
log.info('Bot', 'REPLY: %s', fileBox)
await m.say(fileBox)
await msg.say(fileBox)
/**
* 3. reply 'scan now!'
*/
await m.say('Scan now, because other Wechaty developers want to talk with you too!\n\n(secret code: wechaty)')
await msg.say('Scan now, because other Wechaty developers want to talk with you too!\n\n(secret code: wechaty)')
}
} catch (e) {
......
......@@ -94,7 +94,7 @@
"dependencies": {
"bl": "^1.2.0",
"brolog": "^1.2.0",
"clone-class": "^0.6.10",
"clone-class": "^0.6.11",
"cuid": "^2.1.1",
"file-box": "^0.2.1",
"hot-import": "^0.2.1",
......
......@@ -77,10 +77,6 @@ export class PuppetMock extends Puppet {
super(options)
}
public toString() {
return `PuppetMock<${this.options.profile.name}>`
}
public ding(data?: any): Promise<string> {
return data
}
......@@ -92,14 +88,15 @@ export class PuppetMock extends Puppet {
// await some tasks...
this.state.on(true)
const user = this.Contact.load('logined_user_id')
const msg = this.Message.createMT('mock_id')
this.user = user
this.userId = 'logined_user_id'
const user = this.Contact.load(this.userId)
this.emit('login', user)
const msg = this.Message.createMT('mock_id')
await msg.ready()
setInterval(() => {
log.verbose('PuppetMock', `start() setInterval() pretending received a new message: ${msg}`)
log.verbose('PuppetMock', `start() setInterval() pretending received a new message: ${msg + ''}`)
this.emit('message', msg)
}, 3000)
......@@ -126,8 +123,8 @@ export class PuppetMock extends Puppet {
throw new Error('logout before login?')
}
this.emit('logout', this.user!) // becore we will throw above by logonoff() when this.user===undefined
this.user = undefined
this.emit('logout', this.userId!) // becore we will throw above by logonoff() when this.user===undefined
this.userId = undefined
// TODO: do the logout job
}
......@@ -224,7 +221,9 @@ export class PuppetMock extends Puppet {
* Room
*
*/
public async roomRawPayload(id: string): Promise<MockRoomRawPayload> {
public async roomRawPayload(
id: string,
): Promise<MockRoomRawPayload> {
log.verbose('PuppetMock', 'roomRawPayload(%s)', id)
const rawPayload: MockRoomRawPayload = {
......@@ -235,7 +234,9 @@ export class PuppetMock extends Puppet {
return rawPayload
}
public async roomRawPayloadParser(rawPayload: MockRoomRawPayload): Promise<RoomPayload> {
public async roomRawPayloadParser(
rawPayload: MockRoomRawPayload,
): Promise<RoomPayload> {
log.verbose('PuppetMock', 'roomRawPayloadParser(%s)', rawPayload)
const payload: RoomPayload = {
......
......@@ -445,7 +445,7 @@ export class PuppetPuppeteer extends Puppet {
throw new Error('forward() Due to webWx restrictions, more than 25MB of files can not be downloaded and can not be forwarded.')
}
newMsg.FromUserName = this.user && this.user.id || ''
newMsg.FromUserName = this.userId || ''
newMsg.isTranspond = true
newMsg.MsgIdBeforeTranspond = rawPayload.MsgIdBeforeTranspond || rawPayload.MsgId
newMsg.MMSourceMsgId = rawPayload.MsgId
......@@ -525,14 +525,10 @@ export class PuppetPuppeteer extends Puppet {
public async login(user: Contact): Promise<void> {
log.warn('PuppetPuppeteer', 'login(%s)', user)
this.user = user
this.userId = user.id
this.emit('login', user)
}
public logonoff(): boolean {
return !!(this.user)
}
/**
* logout from browser, then server will emit `logout` event
*/
......@@ -552,7 +548,7 @@ export class PuppetPuppeteer extends Puppet {
Raven.captureException(e)
throw e
} finally {
this.user = undefined
this.userId = undefined
this.emit('logout', user)
}
}
......
......@@ -96,7 +96,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
protected readonly watchdog: Watchdog
protected user?: Contact
protected userId?: string
/* tslint:disable:variable-name */
public readonly Contact : typeof Contact
......@@ -242,11 +242,12 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public userSelf(): Contact {
log.verbose('Puppet', 'self()')
if (!this.user) {
if (!this.userId) {
throw new Error('not logged in, no userSelf yet.')
}
return this.user
const user = this.Contact.load(this.userId)
return user
}
public async say(textOrFile: string | FileBox) : Promise<void> {
......@@ -277,7 +278,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
* Login / Logout
*/
public logonoff(): boolean {
if (this.user) {
if (this.userId) {
return true
} else {
return false
......
#!/usr/bin/env node
#!/usr/bin/env ts-node
import { Wechaty } from 'wechaty'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册