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

#40 code clean

上级 4733439c
#!/usr/bin/env node
/**
*
* Wechaty bot use a ApiAi.com brain
......
#!/usr/bin/env node
/**
*
* Wechaty - Wechat for Bot
......
#!/usr/bin/env node
/**
*
* Wechaty - Wechat for Bot
......
#!/usr/bin/env node
/**
*
* Wechaty - Wechat for Bot
......
#!/usr/bin/env node
/**
*
* Wechaty - Wechat for Bot
......
#!/usr/bin/env node
/**
#!/usr/bin/env node
*
* Wechaty - Wechat for Bot
*
......@@ -11,16 +11,13 @@
* ' leaver: 艾静<img class="emoji emojiae" text="_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />JOY
* - BUG2: leave event not right: sometimes can not found member (any more, because they left)
*/
const co = require('co')
const {
import {
Wechaty
, Config
, Room
, Contact
, Message
, log
} = require('../')
} from '../'
/**
*
......@@ -60,7 +57,7 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = new Wechaty({ profile: Config.DEFAULT_PROFILE })
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
bot
.on('scan', ({url, code}) => {
......@@ -260,17 +257,17 @@ function manageDingRoom() {
})
}
function checkRoomJoin(room, invitee, inviter) {
function checkRoomJoin(room: Room, invitee: Contact|Contact[], inviter: Contact) {
log.info('Bot', 'checkRoomJoin(%s, %s, %s)'
, room.topic()
, invitee.map
, Array.isArray(invitee)
? invitee.map(c => c.name()).join(', ')
: invitee.name()
, inviter.name()
)
try {
let to, content
// let to, content
if (inviter.id !== bot.user().id) {
room.say('RULE1: Invitation is limited to me, the owner only. Please do not invit people without notify me.'
......@@ -292,21 +289,27 @@ function checkRoomJoin(room, invitee, inviter) {
room.topic('ding - warn ' + inviter.name())
setTimeout(_ => {
invitee.map
Array.isArray(invitee)
? invitee.forEach(c => room.del(c))
: room.del(invitee)
}, 10000)
} else {
reply('Welcome to my room! :)')
room.say('Welcome to my room! :)')
// sendMessage(bot, {
// room
// , content: `@${invitee.name()} Welcome to my room! :)`
// , to: invitee.id
// })
room.topic('ding - welcome ' + invitee.name())
let welcomeTopic
if (Array.isArray(invitee)) {
welcomeTopic = invitee.map(c => c.name()).join(', ')
} else {
welcomeTopic = invitee.name()
}
room.topic('ding - welcome ' + welcomeTopic)
}
} catch (e) {
......@@ -348,7 +351,7 @@ function putInRoom(contact, room) {
}
}
function getOutRoom(contact, room) {
function getOutRoom(contact: Contact, room: Room) {
log.info('Bot', 'getOutRoom(%s, %s)', contact, room)
try {
......@@ -357,7 +360,7 @@ function getOutRoom(contact, room) {
// , room: room.id
// , to: contact.id
// })
reply('You said "ding" in my room, I will remove you out.')
room.say('You said "ding" in my room, I will remove you out.')
room.del(contact)
} catch (e) {
log.error('Bot', 'getOutRoom() exception: ' + e.stack)
......
import Wechaty from './src/wechaty'
import Config from './src/config'
import Message from './src/message'
import Contact from './src/contact'
import Room from './src/room'
import IoClient from './src/io-client'
import Message from './src/message'
import Puppet from './src/puppet'
import PuppetWeb from './src/puppet-web'
import IoClient from './src/io-client'
import log from './src/brolog-env'
import Room from './src/room'
import UtilLib from './src/util-lib'
import Wechaty from './src/wechaty'
// Object.assign(Wechaty, {
// default: Wechaty
// , Wechaty
// , Config
// , Message
// , Contact
// , Room
// , Puppet
// , PuppetWeb
// , IoClient
// , UtilLib
// , version: require('./package.json').version
// , log // for convenionce use npmlog with environment variable LEVEL
// })
// module.exports = Wechaty
import log from './src/brolog-env'
const version = require('./package.json').version
......
......@@ -15,10 +15,12 @@
"CMD_CHROMIUM": "/wechaty/bin/xvfb-chromium"
},
"scripts": {
"node": "ts-node",
"ts-node": "ts-node",
"build": "tsc",
"clean": "rm -fr dist/*",
"lint": "npm run eslint && npm run tslint",
"eslint": "eslint \"{src,test,bin}/**/*.js\"",
"tslint": "tslint \"{src,test,bin}/**/*.ts\" && tsc --noEmit \"src/**/*.ts\"",
"eslint": "eslint \"{bin,example,src,test}/**/*.js\"",
"tslint": "tslint \"{bin,example,src,test}/**/*.ts\" && tsc --noEmit",
"sloc": "sloc . --details --format cli-table --keys total,source,comment --exclude \"node_modules|doc\" && sloc . --exclude \"node_modules|doc\"",
"pretest": "npm run lint",
"test": "npm run test:phantomjs && npm run test:chrome",
......@@ -83,7 +85,6 @@
"body-parser": "^1.15.2",
"brolog": "^0.3.7",
"chromedriver": "^2.23.1",
"co": "^4.6.0",
"express": "^4.13.4",
"is-ci": "^1.0.9",
"phantomjs-prebuilt": "^2.1.12",
......
......@@ -31,7 +31,7 @@ CMD_CHROMIUM: string
head: HeadType
apihost: string
validApiHost: (string) => boolean
validApiHost: (host: string) => boolean
httpPort: number
......
......@@ -6,9 +6,6 @@
* https://github.com/wechaty/wechaty
*
*/
// const co = require('co')
// import Config from './config'
import Contact from './contact'
import Room from './room'
import UtilLib from './util-lib'
......@@ -150,7 +147,7 @@ class Message {
return this.obj.from ? Contact.load(this.obj.from) : null
}
public to(contact?: Contact|Room|string) {
public to(contact?: Contact|Room|string): Contact {
if (contact) {
if (contact instanceof Contact || contact instanceof Room) {
this.obj.to = contact.id
......@@ -170,7 +167,7 @@ class Message {
return this.obj.content
}
public room(room?) {
public room(room?: Room): Room {
if (room) {
if (room instanceof Room) {
this.obj.room = room.id
......
......@@ -117,7 +117,7 @@ class Browser extends EventEmitter {
}
}
public open(url = 'https://wx.qq.com'): Promise<void> {
public open(url: string = 'https://wx.qq.com'): Promise<void> {
log.verbose('PuppetWebBrowser', `open(${url})`)
// TODO: set a timer to guard driver.get timeout, then retry 3 times 201607
......@@ -175,6 +175,8 @@ class Browser extends EventEmitter {
}
private getChromeDriver() {
log.verbose('PuppetWebBrowser', 'getChromeDriver()')
const options = {
args: ['--no-sandbox'] // issue #26 for run inside docker
, binary: null
......
......@@ -123,7 +123,7 @@ class Room extends EventEmitter {
return this
}
public say(content, replyTo = null): Promise<any> {
public say(content: string, replyTo?: Contact|Contact[]): Promise<any> {
log.verbose('Room', 'say(%s, %s)', content, replyTo)
const m = new Message()
......@@ -137,7 +137,7 @@ class Room extends EventEmitter {
}
let mentionList
if (replyTo.map) {
if (Array.isArray(replyTo)) {
m.to(replyTo[0])
mentionList = replyTo.map(c => '@' + c.name()).join(' ')
} else {
......@@ -249,7 +249,7 @@ class Room extends EventEmitter {
// WechatyBro.glue.chatroomFactory.quit("@@1c066dfcab4ef467cd0a8da8bec90880035aa46526c44f504a83172a9086a5f7"
}
public topic(newTopic: string): string {
public topic(newTopic?: string): string {
if (newTopic) {
log.verbose('Room', 'topic(%s)', newTopic)
}
......@@ -317,7 +317,7 @@ class Room extends EventEmitter {
}
}
public static create(contactList, topic): Promise<Room> {
public static create(contactList: Contact[], topic?: string): Promise<Room> {
log.verbose('Room', 'create(%s, %s)', contactList.join(','), topic)
if (!contactList || !(typeof contactList === 'array')) {
......
......@@ -97,7 +97,7 @@ class UtilLib {
* const DEFAULT_IANA_RANGE = {min: 49152, max: 65535}
*
*/
public static getPort(port:number): Promise<number> {
public static getPort(port: number): Promise<number> {
log.verbose('UtilLib', 'getPort(%d)', port)
let tryPort = nextPort(port || 38788)
......
......@@ -146,12 +146,10 @@ function wrapRoom(callback) {
function wrapMessage(callback) {
log.verbose('WechatyEvent', 'wrapMessage()')
console.log('############### Message type: ')
console.log(typeof Message)
return (...argList) => {
log.silly('WechatyEvent', 'wrapMessage() callback')
console.log('############### wrapped on message callback')
// console.log('############### wrapped on message callback')
// console.log(typeof Message)
// console.log(argList)
if (!(argList[0] instanceof Message)) {
......
{
"compilerOptions": {
"target": "es6"
, "module": "amd"
, "module": "commonjs"
, "moduleResolution": "node"
, "outDir": "dist"
, "outFile": "dist/wechaty.lib.js"
// , "outFile": "dist/wechaty.lib.js"
, "sourceMap": true
, "noUnusedLocals": true
......@@ -24,7 +24,7 @@
, "include": [
"src/**/*.ts"
// , "bin/*.ts"
// , "example/*.ts"
, "example/room-bot.ts"
// , "test/*.ts"
]
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册