diff --git a/examples/advanced/room-bot.js b/examples/advanced/room-bot.js index 8c3653e..cb54ce2 100644 --- a/examples/advanced/room-bot.js +++ b/examples/advanced/room-bot.js @@ -303,7 +303,15 @@ async function manageDingRoom() { /** * Event: Leave */ - room.on('leave', (leaverList, remover) => { +room.on('leave', async function(leaverList) { + + log.info('Bot', 'EVENT: room-leave - Room "%s" lost member "%s"', + await room.topic(), + const members = await room.memberAll() + if (members.length < 3) { + await room.quit() + } +}) log.info('Bot', 'Room EVENT: leave - "%s" leave(remover "%s"), byebye', leaverList.join(','), remover || 'unknown') }) @@ -399,31 +407,36 @@ async function createDingRoom(contact) { log.info('Bot', 'createDingRoom("%s")', contact) try { - const helperContact = await getHelperContact() - - if (!helperContact) { - log.warn('Bot', 'getHelperContact() found nobody') - await contact.say(`You don't have a friend called "${HELPER_CONTACT_NAME}", - because create a new room at least need 3 contacts, please set [HELPER_CONTACT_NAME] in the code first!`) + if (!contact.room().has(contact)) { + log.warn('Bot', 'The group has less than 3 members') + await contact.say(`The group has less than 3 members, please add more members before starting the game.`) return } - log.info('Bot', 'getHelperContact() ok. got: "%s"', helperContact.name()) + const members = await contact.room().memberAll() + const randomMembers = [] + while(randomMembers.length < 2) { + const randomIndex = Math.floor(Math.random() * members.length) + const randomMember = members[randomIndex] + if(randomMember != contact && !randomMembers.includes(randomMember)) { + randomMembers.push(randomMember) + } + } - const contactList = [contact, helperContact] + const contactList = [contact, ...randomMembers] log.verbose('Bot', 'contactList: "%s"', contactList.join(',')) - await contact.say(`There isn't ding room. I'm trying to create a room with "${helperContact.name()}" and you`) - const room = await bot.Room.create(contactList, 'ding') - log.info('Bot', 'createDingRoom() new ding room created: "%s"', room) + await contact.say(`Creating a new group with you and ${randomMembers.map(member => member.name()).join(', ')}`) + const room = await bot.Room.create(contactList, 'Coffee Chat') + log.info('Bot', 'createDingRoom() new Coffee Chat room created: "%s"', room) - await room.topic('ding - created') - await room.say('ding - created') + await room.topic('Coffee Chat - created') +await room.say(`Welcome to the coffee chat group! This group is requested by ${contact.name()}, and your random partners are ${randomMembers.map(member => member.name()).join(', ')}. I hope you guys have a good ice-breaking time!`); return room } catch (e) { - log.error('Bot', 'getHelperContact() exception:', e.stack) + log.error('Bot', 'createDingRoom() exception:', e.stack) throw e } -} +} \ No newline at end of file diff --git a/examples/professional/hot-import-bot/listeners/on-message.js b/examples/professional/hot-import-bot/listeners/on-message.js index 8d499d6..f219b7b 100644 --- a/examples/professional/hot-import-bot/listeners/on-message.js +++ b/examples/professional/hot-import-bot/listeners/on-message.js @@ -1,29 +1,2 @@ -/** - * Wechaty - https://github.com/chatie/wechaty - * - * @copyright 2016-2018 Huan LI - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -async function onMessage (message) { - /** - * We can get the Wechaty bot instance from this: - * `const wechaty = this` - * Or use `this` directly: - * `console.info(this.userSelf())` - */ - console.log(`Received message: ${message}`) -} - -module.exports = onMessage +/** * Wechaty - https://github.com/chatie/wechaty * * @copyright 2016-2018 Huan LI * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ async function onMessage (message) { /** * We can get the Wechaty bot instance from this: * `const wechaty = this` * Or use `this` directly: * `console.info(this.userSelf())` */ if (message === '/☕️') { const group = await this.Room.find({ topic: 'The Big Community' }); if (group && group.memberList().length >= 3) { // trigger ice-breaking game } else { // send message to group that game can't be started } } console.log(`Received message: ${message}`) } +module.exports = onMessage \ No newline at end of file