peertube-plugin-livechat/bots/lib/bot/handlers/demo.ts

78 lines
2.7 KiB
TypeScript
Raw Normal View History

import type { XMPPUser } from '../types'
2021-12-08 14:51:29 +00:00
import { logger } from '../../logger'
import { BotHandler } from './base'
2021-12-08 15:12:47 +00:00
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 {
2021-12-08 14:51:29 +00:00
protected readonly lastHellos: Map<string, Date> = new Map()
2021-12-08 15:12:47 +00:00
protected randomCount: number = 0
protected randomTimeout: NodeJS.Timeout | undefined
2021-12-08 14:51:29 +00:00
protected init (): void {
const room = this.room
room.on('room_join', (user: XMPPUser) => {
if (user.isMe) {
return
}
if (!room.isOnline()) {
return
}
2021-12-08 14:51:29 +00:00
const lastHello = this.lastHellos.get(user.nick)
const now = new Date()
let msg: string
if (lastHello) {
logger.debug(`The user ${user.nick} was already seen at ${lastHello.toString()}`)
if ((now.getTime() - lastHello.getTime()) < 3600 * 1000) { // no more than one hello per hour
logger.info(`The user ${user.nick} was seen to recently, no message to send.`)
return
}
logger.info(`The user ${user.nick} was seen a long time ago, sending a message.`)
msg = `Hello ${user.nick}! Happy to see you again.`
} else {
logger.info(`The user ${user.nick} is here for the first time. Sending a message.`)
msg = `Hello ${user.nick}! I'm the DemoBot, I'm here to demonstrate the chatroom.`
}
this.lastHellos.set(user.nick, now)
room.sendGroupchat(msg).catch(() => {})
})
2021-12-08 15:12:47 +00:00
2021-12-08 15:54:22 +00:00
room.on('room_message', (msg: string, user?: XMPPUser, mentionned?: boolean) => {
if (!user || user.isMe) { return }
if (!room.isOnline()) { return }
if (!mentionned) { return }
room.sendGroupchat(`Yep @${user.nick}?`).catch(() => {})
})
2021-12-08 15:12:47 +00:00
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()
2021-12-08 15:59:37 +00:00
logger.debug(`Online user count in room: ${onlineUserCount}`)
2021-12-08 15:12:47 +00:00
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(() => {})
}
}