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

#40 fix examples

上级 aee4448a
...@@ -36,7 +36,7 @@ Please wait... I'm trying to login in... ...@@ -36,7 +36,7 @@ Please wait... I'm trying to login in...
` `
console.log(welcome) console.log(welcome)
const bot = new Wechaty({ profile: Config.DEFAULT_PROFILE }) const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
bot bot
.on('login' , user => log.info('Bot', `${user.name()} logined`)) .on('login' , user => log.info('Bot', `${user.name()} logined`))
...@@ -84,3 +84,4 @@ require('fs').appendFile('message.log', data + '\n\n############################ ...@@ -84,3 +84,4 @@ require('fs').appendFile('message.log', data + '\n\n############################
if (err) { log.error('LogToFile: %s', err) } if (err) { log.error('LogToFile: %s', err) }
}) })
} }
typeof logToFile;
...@@ -6,12 +6,12 @@ ...@@ -6,12 +6,12 @@
* https://github.com/wechaty/wechaty * https://github.com/wechaty/wechaty
* *
*/ */
const { import {
Wechaty Wechaty
, Message , Message
, Config , Config
, log , log
} = require('../') } from '../'
const welcome = ` const welcome = `
=============== Powered by Wechaty =============== =============== Powered by Wechaty ===============
...@@ -38,7 +38,7 @@ Please wait... I'm trying to login in... ...@@ -38,7 +38,7 @@ Please wait... I'm trying to login in...
` `
console.log(welcome) console.log(welcome)
const bot = new Wechaty({ profile: Config.DEFAULT_PROFILE }) const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
bot bot
.on('login' , user => log.info('Bot', `${user.name()} logined`)) .on('login' , user => log.info('Bot', `${user.name()} logined`))
......
...@@ -6,8 +6,11 @@ ...@@ -6,8 +6,11 @@
* https://github.com/wechaty/wechaty * https://github.com/wechaty/wechaty
* *
*/ */
const Wechaty = require('..') import {
const bot = new Wechaty({ profile: 'example-bot.wechaty.json' }) Message
, Wechaty
} from '../'
const bot = Wechaty.instance({ profile: 'example-bot.wechaty.json' })
bot bot
.on('scan', ({url, code}) => { .on('scan', ({url, code}) => {
...@@ -16,7 +19,7 @@ bot ...@@ -16,7 +19,7 @@ bot
.on('message', m => { .on('message', m => {
console.log(`RECV: ${m}`) console.log(`RECV: ${m}`)
if (m.type() === Wechaty.Message.Type.IMAGE) { if (m.type() === Message.TYPE['IMAGE']) {
console.log('IMAGE url: ' + m.get('url')) console.log('IMAGE url: ' + m.get('url'))
const filename = m.id + '.jpg' const filename = m.id + '.jpg'
console.log('IMAGE local filename: ' + filename) console.log('IMAGE local filename: ' + filename)
......
...@@ -6,15 +6,15 @@ ...@@ -6,15 +6,15 @@
* https://github.com/wechaty/wechaty * https://github.com/wechaty/wechaty
* *
*/ */
const Wechaty = require('..') import { Wechaty } from '../'
const bot = new Wechaty(/* no profile here because roger bot is too noisy */) const bot = Wechaty.instance(/* no profile here because roger bot is too noisy */)
bot bot
.on('scan', ({url, code}) => { .on('scan', ({url, code}) => {
console.log(`Use Wechat to Scan QR Code in url to login: ${code}\n${url}`) console.log(`Use Wechat to Scan QR Code in url to login: ${code}\n${url}`)
}) })
.on('message', m => { .on('message', m => {
(!bot.self(m)) && reply('roger') // 1. reply others' msg (!bot.self(m)) && m.say('roger') // 1. reply others' msg
.then(() => console.log(`RECV: ${m}, REPLY: "roger"`)) // 2. log message .then(() => console.log(`RECV: ${m}, REPLY: "roger"`)) // 2. log message
.catch(e => console.error(e)) // 3. catch exception .catch(e => console.error(e)) // 3. catch exception
}) })
......
...@@ -76,7 +76,7 @@ bot ...@@ -76,7 +76,7 @@ bot
log.info('Bot', `${user.name()} logined`) log.info('Bot', `${user.name()} logined`)
log.info('Bot', `setting to manageDingRoom() after 3 seconds ... `) log.info('Bot', `setting to manageDingRoom() after 3 seconds ... `)
setTimeout(_ => manageDingRoom(), 3000) setTimeout(manageDingRoom.bind(this), 3000)
}) })
/** /**
...@@ -230,7 +230,7 @@ function manageDingRoom() { ...@@ -230,7 +230,7 @@ function manageDingRoom() {
/** /**
* Event: Join * Event: Join
*/ */
room.on('join', (invitee, inviter) => room.on('join', (invitee: Contact|Contact[], inviter: Contact) =>
checkRoomJoin.call(this, room, invitee, inviter) checkRoomJoin.call(this, room, invitee, inviter)
) )
...@@ -257,7 +257,10 @@ function manageDingRoom() { ...@@ -257,7 +257,10 @@ function manageDingRoom() {
}) })
} }
function checkRoomJoin(room: Room, invitee: Contact|Contact[], inviter: Contact) { function checkRoomJoin(room: Room, invitee: Contact , inviter: Contact)
function checkRoomJoin(room: Room, invitee: Contact[] , inviter: Contact)
function checkRoomJoin(room: Room, invitee: Contact|Contact[] , inviter: Contact) {
log.info('Bot', 'checkRoomJoin(%s, %s, %s)' log.info('Bot', 'checkRoomJoin(%s, %s, %s)'
, room.topic() , room.topic()
, Array.isArray(invitee) , Array.isArray(invitee)
......
...@@ -10,16 +10,17 @@ ...@@ -10,16 +10,17 @@
* Wechaty - https://github.com/zixia/wechaty * Wechaty - https://github.com/zixia/wechaty
* *
*/ */
const log = require('npmlog')
const co = require('co')
const Tuling123 = require('tuling123-client') const Tuling123 = require('tuling123-client')
const EventEmitter2 = require('eventemitter2')
const { import { EventEmitter } from 'events'
Wechaty
, Config import {
} = require Config
('../') , Room
, Wechaty
, log
} from '../'
//log.level = 'verbose' //log.level = 'verbose'
// log.level = 'silly' // log.level = 'silly'
...@@ -32,9 +33,7 @@ const { ...@@ -32,9 +33,7 @@ const {
const TULING123_API_KEY = '18f25157e0446df58ade098479f74b21' const TULING123_API_KEY = '18f25157e0446df58ade098479f74b21'
const brain = new Tuling123(TULING123_API_KEY) const brain = new Tuling123(TULING123_API_KEY)
const bot = new Wechaty({ const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
profile: Config.DEFAULT_PROFILE
})
console.log(` console.log(`
Welcome to Tuling Wechaty Bot. Welcome to Tuling Wechaty Bot.
...@@ -52,12 +51,13 @@ bot ...@@ -52,12 +51,13 @@ bot
.on('scan', ({url, code}) => { .on('scan', ({url, code}) => {
console.log(`Scan QR Code in url to login: ${code}\n${url}`) console.log(`Scan QR Code in url to login: ${code}\n${url}`)
}) })
.on('message', m => { .on('message', async m => {
if (bot.self(m)) return if (bot.self(m)) return
co(function* () { // co(function* () {
const msg = yield m.ready() try {
const room = Wechaty.Room.load(m.get('room')) const msg = await m.ready()
const room = Room.load(m.get('room'))
if (room && /Wechaty/i.test(room.get('name'))) { if (room && /Wechaty/i.test(room.get('name'))) {
log.info('Bot', 'talk: %s' , msg) log.info('Bot', 'talk: %s' , msg)
...@@ -65,8 +65,10 @@ bot ...@@ -65,8 +65,10 @@ bot
} else { } else {
log.info('Bot', 'recv: %s' , msg) log.info('Bot', 'recv: %s' , msg)
} }
}) // }).catch(e => {
.catch(e => log.error('Bot', 'on message rejected: %s' , e)) } catch (e) {
log.error('Bot', 'on message rejected: %s' , e)
}
}) })
bot.init() bot.init()
...@@ -76,16 +78,20 @@ bot.init() ...@@ -76,16 +78,20 @@ bot.init()
process.exit(-1) process.exit(-1)
}) })
class Talker extends EventEmitter2 { class Talker extends EventEmitter {
constructor(thinker) { private obj: {
log.verbose('Talker()') text: any
time: any
}
private timer: number
constructor(private thinker) {
super() super()
this.thinker = thinker log.verbose('Talker()')
this.obj = { this.obj = {
text: [] text: []
, time: [] , time: []
} }
this.timer = null
} }
save(text) { save(text) {
...@@ -101,7 +107,7 @@ class Talker extends EventEmitter2 { ...@@ -101,7 +107,7 @@ class Talker extends EventEmitter2 {
return text return text
} }
updateTimer(delayTime) { updateTimer(delayTime?) {
delayTime = delayTime || this.delayTime() delayTime = delayTime || this.delayTime()
log.verbose('Talker', 'updateTimer(%s)', delayTime) log.verbose('Talker', 'updateTimer(%s)', delayTime)
......
...@@ -13,14 +13,20 @@ ...@@ -13,14 +13,20 @@
import Config from './config' import Config from './config'
import Contact from './contact' import Contact from './contact'
import Message from './message' import Message from './message'
// import Room from './room' import Room from './room'
import Wechaty from './wechaty'
import log from './brolog-env' import log from './brolog-env'
type EventScope = { type WechatyEventScope = {
say: (content: string, replyTo?: Contact|Contact[]) => void say: (content: string, replyTo?: Contact|Contact[]) => void
} }
type WechatyEventType = 'error' | 'heartbeat'
| 'login' | 'logout'
| 'message' | 'scan' | 'friend'
| 'room-join' | 'room-leave' | 'room-topic'
const EVENT_CONFIG = { const EVENT_CONFIG = {
error: wrapFilehelper error: wrapFilehelper
, friend: wrapContact , friend: wrapContact
...@@ -34,12 +40,12 @@ const EVENT_CONFIG = { ...@@ -34,12 +40,12 @@ const EVENT_CONFIG = {
, scan: null // NULL , scan: null // NULL
} }
class WechatyEvent { class EventScope {
public static list() { public static list() {
return Object.keys(EVENT_CONFIG) return Object.keys(EVENT_CONFIG)
} }
public static wrap(event, callback) { public static wrap(this: Wechaty|Room, event: WechatyEventType, callback: Function) {
log.verbose('WechatyEvent', 'wrap(%s, %s)', event, typeof callback) log.verbose('WechatyEvent', 'wrap(%s, %s)', event, typeof callback)
// if (!(this instanceof Wechaty)) { // if (!(this instanceof Wechaty)) {
...@@ -98,7 +104,7 @@ function wrapContact(callback) { ...@@ -98,7 +104,7 @@ function wrapContact(callback) {
const contact = argList[0] const contact = argList[0]
const eventScope = <EventScope>{} const eventScope = <WechatyEventScope>{}
eventScope.say = (content) => { eventScope.say = (content) => {
const msg = new Message() const msg = new Message()
msg.to(contact) msg.to(contact)
...@@ -114,23 +120,22 @@ function wrapContact(callback) { ...@@ -114,23 +120,22 @@ function wrapContact(callback) {
function wrapRoom(callback) { function wrapRoom(callback) {
log.verbose('WechatyEvent', 'wrapRoom()') log.verbose('WechatyEvent', 'wrapRoom()')
return (...argList) => { return (room: Room, ...argList) => {
log.silly('WechatyEvent', 'wrapRoom() callback') log.silly('WechatyEvent', 'wrapRoom(%s, %s, %s, %s) callback', room.topic(), argList[0], argList[1], argList[2])
let room, contact
let contact
for (let arg of argList) { for (let arg of argList) {
if (!room && isRoom(arg)) { if (!contact && isContact(arg)) {
room = arg
} else if (!contact && isContact(arg)) {
contact = arg contact = arg
} }
} }
if (!room || !contact) { if (!room || !isRoom(room) || !contact) {
throw new Error('room or contact not found') throw new Error('room or contact not found')
} }
const eventScope = <EventScope>{} const eventScope = <WechatyEventScope>{}
eventScope.say = (content, replyTo = null) => { eventScope.say = (content: string, replyTo?: Contact) => {
if (!replyTo) { if (!replyTo) {
replyTo = contact replyTo = contact
} else if (!isContact(replyTo)) { } else if (!isContact(replyTo)) {
...@@ -139,7 +144,7 @@ function wrapRoom(callback) { ...@@ -139,7 +144,7 @@ function wrapRoom(callback) {
return room.say(content, replyTo) return room.say(content, replyTo)
} }
return callback.apply(eventScope, argList) return callback.apply(eventScope, [room, ...argList])
} }
} }
...@@ -162,7 +167,7 @@ function wrapMessage(callback) { ...@@ -162,7 +167,7 @@ function wrapMessage(callback) {
// const receiver = msg.to() // const receiver = msg.to()
const room = msg.room() const room = msg.room()
const eventScope = <EventScope>{} const eventScope = <WechatyEventScope>{}
eventScope.say = (content, replyTo) => { eventScope.say = (content, replyTo) => {
log.silly('WechatyEvent', 'wrapMessage() say("%s", "%s")', content, replyTo) log.silly('WechatyEvent', 'wrapMessage() say("%s", "%s")', content, replyTo)
...@@ -187,7 +192,7 @@ function wrapFilehelper(callback) { ...@@ -187,7 +192,7 @@ function wrapFilehelper(callback) {
return (...argList) => { return (...argList) => {
log.silly('WechatyEvent', 'wrapFilehelper() callback') log.silly('WechatyEvent', 'wrapFilehelper() callback')
const eventScope = <EventScope>{} const eventScope = <WechatyEventScope>{}
eventScope.say = (content) => { eventScope.say = (content) => {
log.silly('WechatyEvent', 'wrapFilehelper() say(%s)', content) log.silly('WechatyEvent', 'wrapFilehelper() say(%s)', content)
const msg = new Message() const msg = new Message()
...@@ -202,4 +207,4 @@ function wrapFilehelper(callback) { ...@@ -202,4 +207,4 @@ function wrapFilehelper(callback) {
} }
// module.exports = WechatyEvent.default = WechatyEvent // module.exports = WechatyEvent.default = WechatyEvent
export default WechatyEvent export default EventScope
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
const retryPromise = require('retry-promise').default const retryPromise = require('retry-promise').default
import Contact from '../contact' import Contact from '../contact'
// import Message from '../message' import Message from '../message'
import log from '../brolog-env' import log from '../brolog-env'
import FriendRequest from './friend-request' import FriendRequest from './friend-request'
...@@ -53,12 +53,14 @@ const regexConfig = { ...@@ -53,12 +53,14 @@ const regexConfig = {
, roomTopic: /^"?(.+?)"? changed the group name to "(.+)"$/ , roomTopic: /^"?(.+?)"? changed the group name to "(.+)"$/
} }
function fireFriendRequest(m) { async function fireFriendRequest(m) {
const info = m.rawObj.RecommendInfo const info = m.rawObj.RecommendInfo
log.verbose('PuppetWebFirer', 'fireFriendRequest(%s)', info) log.verbose('PuppetWebFirer', 'fireFriendRequest(%s)', info)
const request = new FriendRequest() const request = new FriendRequest()
request.receive(info) request.receive(info)
await request.contact.ready()
this.emit('friend', request.contact, request) this.emit('friend', request.contact, request)
} }
...@@ -74,7 +76,7 @@ function checkFriendConfirm(content) { ...@@ -74,7 +76,7 @@ function checkFriendConfirm(content) {
} }
} }
function fireFriendConfirm(m) { async function fireFriendConfirm(m) {
const content = m.content() const content = m.content()
log.silly('PuppetWebFirer', 'fireFriendConfirm(%s)', content) log.silly('PuppetWebFirer', 'fireFriendConfirm(%s)', content)
...@@ -85,6 +87,7 @@ function fireFriendConfirm(m) { ...@@ -85,6 +87,7 @@ function fireFriendConfirm(m) {
const contact = Contact.load(m.get('from')) const contact = Contact.load(m.get('from'))
request.confirm(contact) request.confirm(contact)
await contact.ready()
this.emit('friend', contact) this.emit('friend', contact)
} }
...@@ -98,15 +101,16 @@ function fireFriendConfirm(m) { ...@@ -98,15 +101,16 @@ function fireFriendConfirm(m) {
* "李卓桓.PreAngel" invited "Bruce LEE" to the group chat * "李卓桓.PreAngel" invited "Bruce LEE" to the group chat
* "凌" invited "庆次、小桔妹" to the group chat * "凌" invited "庆次、小桔妹" to the group chat
*/ */
function checkRoomJoin(content): [string|string[], string] | boolean { function checkRoomJoin(content: string): [string[], string] {
log.verbose('PuppetWebFirer', 'checkRoomJoin()') log.verbose('PuppetWebFirer', 'checkRoomJoin()')
const re = regexConfig.roomJoin const re = regexConfig.roomJoin
const found = content.match(re) const found = content.match(re)
if (!found) { if (!found) {
return false throw new Error('checkRoomJoin() not found')
} }
const [, inviter, inviteeStr] = found const [, inviter, inviteeStr] = found
// "凌" invited "庆次、小桔妹" to the group chat // "凌" invited "庆次、小桔妹" to the group chat
...@@ -115,19 +119,25 @@ function checkRoomJoin(content): [string|string[], string] | boolean { ...@@ -115,19 +119,25 @@ function checkRoomJoin(content): [string|string[], string] | boolean {
return [inviteeList, inviter] // put invitee at first place return [inviteeList, inviter] // put invitee at first place
} }
async function fireRoomJoin(m): Promise<void> { async function fireRoomJoin(m: Message): Promise<void> {
log.verbose('PuppetWebFirer', 'fireRoomJoin()') log.verbose('PuppetWebFirer', 'fireRoomJoin()')
const room = m.room() const room = m.room()
const content = m.content() const content = m.content()
let result = checkRoomJoin(content) let inviteeList: string[], inviter: string
if (!result) { try {
[inviteeList, inviter] = checkRoomJoin(content)
} catch (e) { // not a room join message
return return
} }
const [inviteeList, inviter] = <[string[], string]>result log.silly('PuppetWebFirer', 'fireRoomJoin() inviteeList: %s, inviter: %s'
, inviteeList.join(',')
, inviter
)
let inviterContact, inviteeContactList = [] let inviterContact: Contact
let inviteeContactList: Contact[] = []
// co.call(this, function* () { // co.call(this, function* () {
try { try {
...@@ -147,18 +157,17 @@ async function fireRoomJoin(m): Promise<void> { ...@@ -147,18 +157,17 @@ async function fireRoomJoin(m): Promise<void> {
return room.refresh() return room.refresh()
.then(_ => { .then(_ => {
log.silly('PuppetWebFirer', 'inviteeList: %s, inviter: %s'
, inviteeList.join(',')
, inviter
)
let iDone, allDone = true let iDone, allDone = true
for (let i in inviteeList) { for (let i in inviteeList) {
iDone = inviteeContactList[i] instanceof Contact iDone = inviteeContactList[i] instanceof Contact
if (!iDone) { if (!iDone) {
inviteeContactList[i] = room.member(inviteeList[i]) let c = room.member(inviteeList[i])
|| (allDone = false) if (c) {
inviteeContactList[i] = c
} else {
allDone = false
}
} }
} }
...@@ -168,20 +177,20 @@ async function fireRoomJoin(m): Promise<void> { ...@@ -168,20 +177,20 @@ async function fireRoomJoin(m): Promise<void> {
if (allDone && inviterContact) { if (allDone && inviterContact) {
log.silly('PuppetWebFirer', 'fireRoomJoin() resolve() inviteeContactList: %s, inviterContact: %s' log.silly('PuppetWebFirer', 'fireRoomJoin() resolve() inviteeContactList: %s, inviterContact: %s'
, inviteeContactList.join(',') , inviteeContactList.map((c: Contact) => c.name()).join(',')
, inviterContact , inviterContact.name()
) )
return Promise.resolve() return Promise.resolve()
} else { } else {
log.silly('PuppetWebFirer', 'fireRoomJoin() reject() inviteeContactList: %s, inviterContact: %s' log.silly('PuppetWebFirer', 'fireRoomJoin() reject() inviteeContactList: %s, inviterContact: %s'
, inviteeContactList.join(',') , inviteeContactList.map((c: Contact) => c.name()).join(',')
, inviterContact , inviterContact.name()
) )
return Promise.reject('not found(yet)') return Promise.reject('not found(yet)')
} }
}) })
.catch(e => { .catch(e => {
log.error('PuppetWebFirer', 'fireRoomJoin9() retryPromise() room.refresh() rejected: %s', e.stack) log.error('PuppetWebFirer', 'fireRoomJoin() retryPromise() room.refresh() rejected: %s', e.stack)
throw e throw e
}) })
}) })
...@@ -198,6 +207,7 @@ async function fireRoomJoin(m): Promise<void> { ...@@ -198,6 +207,7 @@ async function fireRoomJoin(m): Promise<void> {
await Promise.all(inviteeContactList.map(c => c.ready())) await Promise.all(inviteeContactList.map(c => c.ready()))
await inviterContact.ready() await inviterContact.ready()
await room.ready()
if (inviteeContactList.length === 1) { if (inviteeContactList.length === 1) {
this.emit('room-join', room , inviteeContactList[0], inviterContact) this.emit('room-join', room , inviteeContactList[0], inviterContact)
...@@ -215,12 +225,12 @@ async function fireRoomJoin(m): Promise<void> { ...@@ -215,12 +225,12 @@ async function fireRoomJoin(m): Promise<void> {
return return
} }
function checkRoomLeave(content) { function checkRoomLeave(content: string): string {
const re = regexConfig.roomLeave const re = regexConfig.roomLeave
const found = content.match(re) const found = content.match(re)
if (!found) { if (!found) {
return false return null
} }
const [, leaver] = found const [, leaver] = found
return leaver return leaver
...@@ -283,32 +293,32 @@ async function fireRoomLeave(m) { ...@@ -283,32 +293,32 @@ async function fireRoomLeave(m) {
return return
} }
leaverContact.ready() await leaverContact.ready()
.then(_ => { await room.ready()
this.emit('room-leave', room, leaverContact) this.emit('room-leave', room, leaverContact)
room.emit('leave' , leaverContact) room.emit('leave' , leaverContact)
room.refresh() await room.refresh()
})
} }
function checkRoomTopic(content): [string, string] | boolean { function checkRoomTopic(content): [string, string] {
const re = regexConfig.roomTopic const re = regexConfig.roomTopic
const found = content.match(re) const found = content.match(re)
if (!found) { if (!found) {
return false throw new Error('checkRoomTopic() not found')
} }
const [, changer, topic] = found const [, changer, topic] = found
return [topic, changer] return [topic, changer]
} }
async function fireRoomTopic(m) { async function fireRoomTopic(m) {
const result = checkRoomTopic(m.content()) let topic, changer
if (!result) { try {
[topic, changer] = checkRoomTopic(m.content())
} catch (e) { // not found
return return
} }
const [topic, changer] = <[string, string]>result
const room = m.room() const room = m.room()
const oldTopic = room.topic() const oldTopic = room.topic()
...@@ -327,6 +337,7 @@ async function fireRoomTopic(m) { ...@@ -327,6 +337,7 @@ async function fireRoomTopic(m) {
// co.call(this, function* () { // co.call(this, function* () {
try { try {
await changerContact.ready() await changerContact.ready()
await room.ready()
this.emit('room-topic', room, topic, oldTopic, changerContact) this.emit('room-topic', room, topic, oldTopic, changerContact)
room.emit('topic' , topic, oldTopic, changerContact) room.emit('topic' , topic, oldTopic, changerContact)
room.refresh() room.refresh()
......
...@@ -10,13 +10,13 @@ ...@@ -10,13 +10,13 @@
*/ */
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import Config from './config' import Config from './config'
import Contact from './contact' import Contact from './contact'
import Message from './message' import Message from './message'
import UtilLib from './util-lib' import UtilLib from './util-lib'
import WechatyEvent from './wechaty-event' import EventScope from './event-scope'
import log from './brolog-env' import log from './brolog-env'
type RoomObj = { type RoomObj = {
id: string id: string
...@@ -109,17 +109,19 @@ class Room extends EventEmitter { ...@@ -109,17 +109,19 @@ class Room extends EventEmitter {
}) })
} }
public on(event: string, listener: Function) { public on( event: 'join' | 'leave' | 'topic'
, listener: Function
) {
log.verbose('Room', 'on(%s, %s)', event, typeof listener) log.verbose('Room', 'on(%s, %s)', event, typeof listener)
/** /**
* every room event must can be mapped to a global event. * every room event must can be mapped to a global event.
* such as: `join` to `room-join` * such as: `join` to `room-join`
*/ */
const wrapCallback = WechatyEvent.wrap.call(this, 'room-' + event, listener) const callbackWithScope = EventScope.wrap.call(this, 'room-' + event, listener)
// bind(this1, this2): the second this is for simulate the global room-* event // bind(this1, this2): the second this is for simulate the global room-* event
super.on(event, wrapCallback.bind(this, this)) super.on(event, callbackWithScope.bind(this, this))
return this return this
} }
...@@ -297,7 +299,10 @@ class Room extends EventEmitter { ...@@ -297,7 +299,10 @@ class Room extends EventEmitter {
} }
} }
public member(name): Contact { /**
* NickName / DisplayName / RemarkName of member
*/
public member(name: string): Contact {
log.verbose('Room', 'member(%s)', name) log.verbose('Room', 'member(%s)', name)
if (!this.obj || !this.obj.memberList) { if (!this.obj || !this.obj.memberList) {
......
...@@ -23,7 +23,7 @@ import Message from './message' ...@@ -23,7 +23,7 @@ import Message from './message'
import Puppet from './puppet' import Puppet from './puppet'
import PuppetWeb from './puppet-web/index' import PuppetWeb from './puppet-web/index'
import UtilLib from './util-lib' import UtilLib from './util-lib'
import WechatyEvent from './wechaty-event' import EventScope from './event-scope'
import log from './brolog-env' import log from './brolog-env'
...@@ -155,7 +155,7 @@ class Wechaty extends EventEmitter { ...@@ -155,7 +155,7 @@ class Wechaty extends EventEmitter {
public on(event: string, listener: Function) { public on(event: string, listener: Function) {
log.verbose('Wechaty', 'on(%s, %s)', event, typeof listener) log.verbose('Wechaty', 'on(%s, %s)', event, typeof listener)
const wrapListener = WechatyEvent.wrap.call(this, event, listener) const wrapListener = EventScope.wrap.call(this, event, listener)
super.on(event, wrapListener) super.on(event, wrapListener)
return this return this
...@@ -174,7 +174,7 @@ class Wechaty extends EventEmitter { ...@@ -174,7 +174,7 @@ class Wechaty extends EventEmitter {
throw new Error('Puppet unsupport(yet): ' + this.setting.type) throw new Error('Puppet unsupport(yet): ' + this.setting.type)
} }
WechatyEvent.list().map(e => { EventScope.list().map(e => {
// https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/ // https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/
// We’ve lost () around the argument list when there’s just one argument (rest arguments are an exception, eg (...args) => ...) // We’ve lost () around the argument list when there’s just one argument (rest arguments are an exception, eg (...args) => ...)
puppet.on(e, (...args) => { puppet.on(e, (...args) => {
......
...@@ -23,8 +23,8 @@ ...@@ -23,8 +23,8 @@
] ]
, "include": [ , "include": [
"src/**/*.ts" "src/**/*.ts"
// , "bin/*.ts" , "bin/*.ts"
, "example/room-bot.ts" , "example/*.ts"
// , "test/*.ts" // , "test/*.ts"
] ]
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册