diff --git a/bots/lib/bot/component.ts b/bots/lib/bot/component.ts index 5984a1ff..fddd3d6b 100644 --- a/bots/lib/bot/component.ts +++ b/bots/lib/bot/component.ts @@ -63,6 +63,7 @@ class BotComponent { public async disconnect (): Promise { for (const [roomId, room] of this.rooms) { logger.debug(`Leaving room ${roomId}...`) + await room.detachHandlers() await room.part() } await this.xmpp?.stop() diff --git a/bots/lib/bot/handlers/base.ts b/bots/lib/bot/handlers/base.ts index db4c1ed8..290988cd 100644 --- a/bots/lib/bot/handlers/base.ts +++ b/bots/lib/bot/handlers/base.ts @@ -8,4 +8,5 @@ export abstract class BotHandler { } protected abstract init (): void + public abstract stop (): void } diff --git a/bots/lib/bot/handlers/demo.ts b/bots/lib/bot/handlers/demo.ts index a290c0da..a625ae5e 100644 --- a/bots/lib/bot/handlers/demo.ts +++ b/bots/lib/bot/handlers/demo.ts @@ -2,8 +2,19 @@ import type { XMPPUser } from '../types' import { logger } from '../../logger' import { BotHandler } from './base' +const RANDOM_MESSAGES: string[] = [ + '🎵🎶', + '🎵🎶 I\'m just a bot, I\'m just a bot in the world. 🎵🎶', + 'You can see who is connected by opening the right panel.', + 'This is a random message.', + 'Oh, yet another random message.', + 'You can mention a user using a @ in front of a user\'s nick. Try to mention me.' +] + export class BotHandlerDemo extends BotHandler { protected readonly lastHellos: Map = new Map() + protected randomCount: number = 0 + protected randomTimeout: NodeJS.Timeout | undefined protected init (): void { const room = this.room @@ -32,5 +43,26 @@ export class BotHandlerDemo extends BotHandler { this.lastHellos.set(user.nick, now) room.sendGroupchat(msg).catch(() => {}) }) + + this.randomTimeout = setInterval(() => { + this.sendRandomMessage() + }, 10 * 1000) + } + + public stop (): void { + if (this.randomTimeout) { + clearInterval(this.randomTimeout) + } + } + + protected sendRandomMessage (): void { + const room = this.room + if (!room.isOnline()) { return } + // checking if there is someone to listen... + const onlineUserCount = this.room.onlineUserCount() + if (onlineUserCount < 2) { return } + const cpt = this.randomCount++ + logger.info(`Emitting the random message number ${cpt}.`) + this.room.sendGroupchat(RANDOM_MESSAGES[cpt % RANDOM_MESSAGES.length]).catch(() => {}) } } diff --git a/bots/lib/bot/room.ts b/bots/lib/bot/room.ts index bf6d3ee1..dead0e54 100644 --- a/bots/lib/bot/room.ts +++ b/bots/lib/bot/room.ts @@ -31,6 +31,14 @@ export class BotRoom extends EventEmitter { return this.state === 'online' } + public onlineUserCount (): number { + let count = 0 + this.roster.forEach(user => { + if (user.state === 'online') { count++ } + }) + return count + } + public async join (nick: string): Promise { this.userJID = new JID(this.roomJID.getLocal(), this.roomJID.getDomain(), nick) logger.debug(`Emitting a presence for room ${this.roomJID.toString()}...`) @@ -127,4 +135,10 @@ export class BotRoom extends EventEmitter { public attachHandler (handler: BotHandler): void { this.handlers.push(handler) } + + public detachHandlers (): void { + for (const handler of this.handlers) { + handler.stop() + } + } }