From 9f8da818f70b912786a9d0d901b98919e86cb2c3 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Thu, 14 Jun 2018 20:04:43 +0800 Subject: [PATCH] code clean --- examples/room-bot.ts | 7 ++++++- src/contact.ts | 4 ++-- src/message.ts | 26 ++++++++++++++++++-------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/examples/room-bot.ts b/examples/room-bot.ts index 37d41015..77caecb6 100644 --- a/examples/room-bot.ts +++ b/examples/room-bot.ts @@ -34,7 +34,7 @@ * vvvvvvvvv * vvvvvvvvv */ -const HELPER_CONTACT_NAME = 'Huan LI' +const HELPER_CONTACT_NAME = '李佳芮' /** * ^^^^^^^^^ @@ -162,6 +162,11 @@ bot * Global Event: message */ .on('message', async function(msg) { + if (msg.age() > 3 * 60) { + log.info('Bot', 'on(message) skip age(%d) > 3 * 60 seconds: %s', msg.age(), msg) + return + } + const room = msg.room() const from = msg.from() const text = msg.text() diff --git a/src/contact.ts b/src/contact.ts index f442bb09..0746a56e 100644 --- a/src/contact.ts +++ b/src/contact.ts @@ -72,8 +72,8 @@ export class Contact extends Accessory implements Sayable { * About the Generic: https://stackoverflow.com/q/43003970/1123955 */ public static load( - this : T, - id : string, + this : T, + id : string, ): T['prototype'] { if (!this.pool) { log.verbose('Contact', 'load(%s) init pool', id) diff --git a/src/message.ts b/src/message.ts index f7a1c425..819bc152 100644 --- a/src/message.ts +++ b/src/message.ts @@ -290,9 +290,11 @@ export class Message extends Accessory implements Sayable { textOrContactOrFile : string | Contact | FileBox, mention? : Contact | Contact[], ): Promise { - log.verbose('Message', 'say(%s, %s)', + log.verbose('Message', 'say(%s%s)', textOrContactOrFile.toString(), - mention, + mention + ? ', ' + mention + : '', ) // const user = this.puppet.userSelf() @@ -416,8 +418,8 @@ export class Message extends Accessory implements Sayable { * const contactList = message.mentioned() * console.log(contactList) */ - public async mentioned(): Promise { - log.verbose('Message', 'mentioned()') + public async mention(): Promise { + log.verbose('Message', 'mention()') const room = this.room() if (this.type() !== MessageType.Text || !room ) { @@ -432,7 +434,7 @@ export class Message extends Accessory implements Sayable { if (atList.length === 0) return [] // Using `filter(e => e.indexOf('@') > -1)` to filter the string without `@` - const rawMentionedList = atList + const rawMentionList = atList .filter(str => str.includes('@')) .map(str => multipleAt(str)) @@ -455,11 +457,11 @@ export class Message extends Accessory implements Sayable { let mentionNameList: string[] = [] // Flatten Array // see http://stackoverflow.com/a/10865042/1123955 - mentionNameList = mentionNameList.concat.apply([], rawMentionedList) + mentionNameList = mentionNameList.concat.apply([], rawMentionList) // filter blank string mentionNameList = mentionNameList.filter(s => !!s) - log.verbose('Message', 'mentioned() text = "%s", mentionNameList = "%s"', + log.verbose('Message', 'mention() text = "%s", mentionNameList = "%s"', this.text(), JSON.stringify(mentionNameList), ) @@ -474,11 +476,19 @@ export class Message extends Accessory implements Sayable { contactList = contactList.concat.apply([], contactListNested) if (contactList.length === 0) { - log.warn('Message', `message.mentioned() can not found member using room.member() from mentionList, metion string: ${JSON.stringify(mentionNameList)}`) + log.silly('Message', `message.mention() can not found member using room.member() from mentionList, metion string: ${JSON.stringify(mentionNameList)}`) } return contactList } + /** + * @deprecated: use mention() instead + */ + public async mentioned(): Promise { + log.warn('Message', 'mentioned() DEPRECATED. use mention() instead.') + return this.mention() + } + /** * @private */ -- GitLab