diff --git a/example/room-bot.ts b/example/room-bot.ts index 56d05d4d56ce17be7ad7ec4c0eb9feb7b68b7216..eab84e01a84bb4b5ad6d493bcee524225a255783 100644 --- a/example/room-bot.ts +++ b/example/room-bot.ts @@ -11,13 +11,6 @@ * ' leaver: 艾静JOY * - BUG2: leave event not right: sometimes can not found member (any more, because they left) */ -import { - Config - , Contact - , Room - , Wechaty - , log -} from '../' /** * @@ -31,6 +24,32 @@ import { */ const HELPER_CONTACT_NAME = 'Bruce LEE' + + + + + + + + + + + + + + + + + + +import { + Config + , Contact + , Room + , Wechaty + , log +} from '../' + const welcome = ` =============== Powered by Wechaty =============== -------- https://github.com/wechaty/wechaty -------- @@ -72,10 +91,16 @@ bot * do initialization inside this event. * (better to set a timeout, for browser need time to download other data) */ -.on('login' , user => { - log.info('Bot', `${user.name()} logined`) +.on('login' , function (this, user) { + let msg = `${user.name()} logined` + + log.info('Bot', msg) + this.say(msg) + + msg = `setting to manageDingRoom() after 3 seconds ... ` + log.info('Bot', msg) + this.say(msg) - log.info('Bot', `setting to manageDingRoom() after 3 seconds ... `) setTimeout(manageDingRoom.bind(this), 3000) }) @@ -120,7 +145,7 @@ bot /** * Global Event: message */ -.on('message', (message) => { +.on('message', function (this, message) { const room = message.room() const sender = message.from() const content = message.content() @@ -287,15 +312,6 @@ function checkRoomJoin(room: Room, invitee: Contact|Contact[] , inviter: Contact , invitee ) - // const atList = invitee.map - // ? invitee.map(c => '@' + c.name()).join(' ') - // : '@' + invitee.name() - - // sendMessage(bot, { - // room - // , content: `${atList} Please contact me: by send "ding" to me, I will re-send you a invitation. Now I will remove you out, sorry.` - // , to: invitee.map ? invitee[0].id : invitee.id - // }) room.topic('ding - warn ' + inviter.name()) setTimeout(_ => { @@ -308,11 +324,6 @@ function checkRoomJoin(room: Room, invitee: Contact|Contact[] , inviter: Contact room.say('Welcome to my room! :)') - // sendMessage(bot, { - // room - // , content: `@${invitee.name()} Welcome to my room! :)` - // , to: invitee.id - // }) let welcomeTopic if (Array.isArray(invitee)) { welcomeTopic = invitee.map(c => c.name()).join(', ') @@ -328,20 +339,6 @@ function checkRoomJoin(room: Room, invitee: Contact|Contact[] , inviter: Contact } -// function sendMessage(bot, { -// content -// , to -// , room = null -// }) { -// log.info('Bot', 'sendMessage(%s, {content: %s, to: %s, room: %s})', bot, content, to, room) - -// const msg = new Message() -// msg.content(content) -// msg.room(room) -// msg.to(to) -// bot.send(msg) -// } - function putInRoom(contact, room) { log.info('Bot', 'putInRoom(%s, %s)', contact.name(), room.topic()) @@ -351,11 +348,6 @@ function putInRoom(contact, room) { log.error('Bot', 'room.add() exception: %s', e.stack) }) setTimeout(_ => room.say('Welcome ', contact), 1000) - // setTimeout(_ => sendMessage(bot, { - // content: 'Welcome ' + contact.name() - // , room - // , to: contact - // }), 1000) } catch (e) { log.error('Bot', 'putInRoom() exception: ' + e.stack) } @@ -365,11 +357,6 @@ function getOutRoom(contact: Contact, room: Room) { log.info('Bot', 'getOutRoom(%s, %s)', contact, room) try { - // sendMessage(bot, { - // content: `@${contact.name()} You said "ding" in my room, I will remove you out.` - // , room: room.id - // , to: contact.id - // }) room.say('You said "ding" in my room, I will remove you out.') room.del(contact) } catch (e) { @@ -405,13 +392,7 @@ function createDingRoom(contact) { log.info('Bot', 'createDingRoom() new ding room created: %s', room) room.topic('ding - created') - room.say('ding - created') - // sendMessage(bot, { - // content: '' - // , to: room - // , room - // }) return room }) diff --git a/index.ts b/index.ts index 2dccb9205dc0a5f35bfa4d05519fa6bbeb07337a..deb1a3a1194102ba66e77ac8ee97d077d290d1e9 100644 --- a/index.ts +++ b/index.ts @@ -1,12 +1,12 @@ -import Config from './src/config' -import Contact from './src/contact' -import IoClient from './src/io-client' -import Message from './src/message' -import Puppet from './src/puppet' -import PuppetWeb from './src/puppet-web/' -import Room from './src/room' -import UtilLib from './src/util-lib' -import Wechaty from './src/wechaty' +import Config from './src/config' +import Contact from './src/contact' +import IoClient from './src/io-client' +import Message from './src/message' +import Puppet from './src/puppet' +import PuppetWeb from './src/puppet-web/' +import Room from './src/room' +import UtilLib from './src/util-lib' +import Wechaty from './src/wechaty' import log from './src/brolog-env' diff --git a/src/brolog-env.ts b/src/brolog-env.ts index b836874630a085f001f2d424b016eaa52d0eef8e..d0d60ca59e594f1d91f3617edcaef24a9d77c9bd 100644 --- a/src/brolog-env.ts +++ b/src/brolog-env.ts @@ -5,19 +5,23 @@ * */ // const log = require('npmlog') -import log from 'brolog' +import Brolog from 'brolog' const level = process.env['WECHATY_LOG'] const levelRegexStr = 'silly|verbose|info|warn|error|silent' const levelRegex = new RegExp(levelRegexStr, 'i') if (levelRegex.test(level)) { // log.level = level.toLowerCase() - log.level(level) - log.verbose('Brolog', 'WECHATY_LOG set level to %s', level) + Brolog.level(level) + Brolog.verbose('Brolog', 'WECHATY_LOG set level to %s', level) } else if (level) { - log.warn('Brolog', 'env WECHATY_LOG(%s) must be one of silly|verbose|info|warn|error|silent', level) + Brolog.warn('Brolog', 'env WECHATY_LOG(%s) must be one of silly|verbose|info|warn|error|silent', level) } -// module.exports = log.default = log.log = log -export default log +export { + Brolog + , Brolog as log +} + +export default Brolog diff --git a/src/config.ts b/src/config.ts index 1f9333aa667aefc8fc4af489d1525ecfaa8abbdf..af568fe357d6a3386a4d2a6baf7f7fdb7cb4b0c6 100644 --- a/src/config.ts +++ b/src/config.ts @@ -127,17 +127,6 @@ export type WatchdogFood = { | 'SCAN' } -export type WechatyEventName = 'error' - | 'friend' - | 'heartbeat' - | 'login' - | 'logout' - | 'message' - | 'room-join' - | 'room-leave' - | 'room-topic' - | 'scan' - export type ScanInfo = { url: string code: number