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

code clean

上级 658b2030
......@@ -65,7 +65,7 @@ if (token) {
bot
.on('scan', (qrcode, status) => {
generate(qrcode, { small: true })
console.log(`${status}\n[${qrcode}] Scan QR Code of the url to login:`)
console.log(`${status}: ${qrcode} - Scan QR Code of the url to login:`)
})
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
.on('error' , e => log.info('Bot', 'error: %s', e))
......
......@@ -16,8 +16,8 @@
* limitations under the License.
*
*/
import WebSocket from 'ws'
import StateSwitch from 'state-switch'
import WebSocket from 'ws'
import { StateSwitch } from 'state-switch'
import {
Message,
......@@ -43,27 +43,34 @@ export interface IoOptions {
}
export const IO_EVENT_DICT = {
botie: 'tbw',
error: 'tbw',
heartbeat: 'tbw',
login: 'tbw',
logout: 'tbw',
message: 'tbw',
update: 'tbw',
raw: 'tbw',
reset: 'tbw',
scan: 'tbw',
sys: 'tbw',
shutdown: 'tbw',
botie : 'tbw',
error : 'tbw',
heartbeat : 'tbw',
login : 'tbw',
logout : 'tbw',
message : 'tbw',
update : 'tbw',
raw : 'tbw',
reset : 'tbw',
scan : 'tbw',
sys : 'tbw',
shutdown : 'tbw',
}
type IoEventName = keyof typeof IO_EVENT_DICT
interface IoEvent {
interface IoEventScan {
name: 'scan',
paylaod: PuppetScanEvent,
}
interface IoEventAny {
name: IoEventName,
payload: any,
}
type IoEvent = IoEventScan | IoEventAny
export class Io {
private readonly cuid : string
private readonly protocol : string
......@@ -113,10 +120,11 @@ export class Io {
await this.initEventHook()
this.ws = this.initWebSocket()
this.options.wechaty.on('scan', (qrcode, status) => {
this.scanPayload = Object.assign(this.scanPayload || {}, {
this.scanPayload = {
...this.scanPayload,
qrcode,
status,
})
}
})
this.state.on(true)
......@@ -300,21 +308,16 @@ export class Io {
case 'update':
log.verbose('Io', 'on(update): %s', ioEvent.payload)
try {
const userId = this.options.wechaty.puppet.selfId()
if (userId) {
const loginEvent: IoEvent = {
name : 'login',
payload : {
id: userId,
name: this.options.wechaty.Contact.load(userId).name(),
},
}
await this.send(loginEvent)
const wechaty = this.options.wechaty
if (wechaty.logonoff()) {
const loginEvent: IoEvent = {
name : 'login',
payload : {
id : wechaty.userSelf().id,
name : wechaty.userSelf().name(),
},
}
} catch (e) {
// not login
await this.send(loginEvent)
}
if (this.scanPayload) {
......@@ -406,7 +409,7 @@ export class Io {
const ws = this.ws
if (ioEvent) {
log.silly('Io', 'send(%s: %s)', ioEvent.name, ioEvent.payload)
log.silly('Io', 'send(%s)', JSON.stringify(ioEvent))
this.eventBuffer.push(ioEvent)
} else { log.silly('Io', 'send()') }
......
......@@ -365,11 +365,11 @@ export class Message extends Accessory implements Sayable {
* @deprecated use toFile() instead
*/
public async file(): Promise<FileBox> {
log.warn('Message', 'file() DEPRECATED. use toFile() instead.')
return this.toFile()
log.warn('Message', 'file() DEPRECATED. use toFileBox() instead.')
return this.toFileBox()
}
public async toFile(): Promise<FileBox> {
public async toFileBox(): Promise<FileBox> {
if (this.type() === Message.Type.Text) {
throw new Error('text message no file')
}
......
......@@ -218,7 +218,7 @@ export class PuppetPadchat extends Puppet {
manager.on('logout', () => this.logout())
manager.on('reset', async reason => {
log.warn('PuppetPadchat', 'startManager() manager.on(destroy) for %s. Restarting PuppetPadchat ... ', reason)
log.warn('PuppetPadchat', 'startManager() manager.on(reset) for %s. Restarting PuppetPadchat ... ', reason)
await this.reset(reason)
})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册