Skip to content

Commit

Permalink
feat(bot): ✨ feat
Browse files Browse the repository at this point in the history
IdleState - Set a idle time to wait a user answer, after this send a auto message of inactivity

No have breaking changes
  • Loading branch information
binhodev committed Sep 13, 2023
1 parent aedd4b1 commit 589f96e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/bot/context/idleState.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { EventEmitter } = require('node:events')

class IdleState extends EventEmitter {
timer = null
startTime = 0
endTime = 0

setIdleTime = (timeInSeconds) => {
this.startTime = timeInSeconds
return this.reset()
}

startTimer = () => {
this.timer = setInterval(() => {
const currentTime = new Date().getTime()

if (currentTime > this.endTime) {
this.stop()
this.emit('idle')
} else if (this.debug) {
return this.emit('debug', () => console.info(this.debugTime()))
} else {
return
}
}, 1000)
}

start = () => {
this.stop()
if (!this.timer) {
this.reset()
this.startTimer()
}
}

reset = () => {
const currentTime = new Date().getTime()
this.endTime = currentTime + this.startTime * 1000
}

stop = () => {
if (this.timer) {
clearInterval(this.timer)
this.timer = null
}
}

debugTime = () => {
const currentTime = new Date().getTime()
return `Tiempo restante: ${((this.endTime - currentTime) / 1000).toFixed(0)} segundos`
}
}

module.exports = IdleState
3 changes: 3 additions & 0 deletions packages/bot/core/core.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { LIST_REGEX } = require('../io/events')
const SingleState = require('../context/state.class')
const GlobalState = require('../context/globalState.class')
const { generateTime } = require('../utils/hash')
const IdleState = require('../context/idleState.class')

const logger = new Console({
stdout: createWriteStream(`${process.cwd()}/core.class.log`),
Expand All @@ -19,6 +20,7 @@ const loggerQueue = new Console({

const StateHandler = new SingleState()
const GlobalStateHandler = new GlobalState()
const idle = new IdleState()

/**
* [ ] Escuchar eventos del provider asegurarte que los provider emitan eventos
Expand Down Expand Up @@ -325,6 +327,7 @@ class CoreClass {
state,
globalState,
extensions,
idleState: idle,
fallBack: fallBack(flags),
flowDynamic: flowDynamic(flags),
endFlow: endFlow(flags),
Expand Down

0 comments on commit 589f96e

Please sign in to comment.