DemoBot: random messages.
This commit is contained in:
parent
b27455b08d
commit
4efc507b2c
@ -63,6 +63,7 @@ class BotComponent {
|
||||
public async disconnect (): Promise<any> {
|
||||
for (const [roomId, room] of this.rooms) {
|
||||
logger.debug(`Leaving room ${roomId}...`)
|
||||
await room.detachHandlers()
|
||||
await room.part()
|
||||
}
|
||||
await this.xmpp?.stop()
|
||||
|
@ -8,4 +8,5 @@ export abstract class BotHandler {
|
||||
}
|
||||
|
||||
protected abstract init (): void
|
||||
public abstract stop (): void
|
||||
}
|
||||
|
@ -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<string, Date> = 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(() => {})
|
||||
}
|
||||
}
|
||||
|
@ -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<void> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user