Skip to content

Commit

Permalink
🐛 FIX: !speak -config
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvdev committed Jan 9, 2024
1 parent 26b87d3 commit 492aca5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/commands/speak.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import getVariable, { TTS, TTS_ACCENT, TTS_INDEX } from '../lib/get_variable'
import tts from '../lib/tts'
import TTSConfigVault from '../lib/tts_config_vault'
import ttsConfigVault from '../lib/new_tts_config_vault'

const ttsEnabled = getVariable(TTS)
const ttsAccent = getVariable(TTS_ACCENT) || 'es-AR'
Expand All @@ -11,10 +11,23 @@ ttsIndex > 0 && (ttsIndex -= 1)
// eslint-disable-next-line no-unused-vars
const [ACCENT_OR_MODIFIER, VARIANT] = [0, 1]

const isAAccent = /^[a-zA-Z]{1,2}-[a-zA-Z]{1,2}$/g
// const isAAccent = /^[a-zA-Z]{1,2}-[a-zA-Z]{1,2}$/g

function isAlphabetCharacter(char) {
return /^[a-zA-Z]$/.test(char)
}

function normalize(accent) {
if (isAAccent.test(accent)) {
console.log(accent)
const chars = accent.split('')
if (
chars.length === 5 &&
chars.reduce(
(prev, char, index) =>
prev && (index === 2 ? char === '-' : isAlphabetCharacter(char)),
true
)
) {
let [prev, post] = accent.split('-')
prev = prev.toLowerCase()
post = post.toUpperCase()
Expand Down Expand Up @@ -42,14 +55,16 @@ function config(message) {
const words = msg.split(' ').map((word) => word.trim())
const acce = normalize(words[ACC])
const vari = Math.ceil(Number(words[VAR])) || 1
if (tts.isAValidVoice(acce) && tts.isAValidVariant(acce, vari)) {
TTSConfigVault.setConfig(userName, acce, !isNaN(vari) ? vari : 1)
const valid = tts.isAValidVoice(acce) && tts.isAValidVariant(acce, vari)
console.log(acce, vari, valid, userName)
if (valid) {
ttsConfigVault.setConfig(userName, acce, !isNaN(vari) ? vari : 1)
}
return true
}

function reset({ userName }) {
TTSConfigVault.resetConfig(userName)
ttsConfigVault.resetConfig(userName)
return true
}

Expand All @@ -69,7 +84,7 @@ function speak(message) {
} else toRead = words.slice(2).join(' ')
} else {
toRead = msg.replace('!speak ', '')
const config = TTSConfigVault.getConfig(userName)
const config = ttsConfigVault.getConfig(userName)
if (config) {
accent = config.accent
variant = config.variant
Expand Down
72 changes: 72 additions & 0 deletions src/lib/new_tts_config_vault.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import tts from './tts.js'

const VAULT = 'TTS'

class TTSConfigVault {
#vault
#ls_outdated
constructor() {
this.#vault = JSON.parse(localStorage.getItem(VAULT))
if (!this.#vault) {
this.#vault = {}
localStorage.setItem(VAULT, '{}')
}
this.#ls_outdated = false
}

getVault() {
return this.#vault
}

#LSVaultListener(event) {
console.log('se ha cambiado en LS')
if (event.key === VAULT) {
if (JSON.stringify(this.#vault) === event.newValue) {
this.#ls_outdated = false
window.removeEventListener('storage', this.#LSVaultListener)
console.log('Memoria y LS son iguales')
} else {
localStorage.setItem(VAULT, JSON.stringify(this.#vault))
console.log('Memoria y LS son diferentes')
}
this.#ls_outdated = false
}
}

#lsIsOutdated() {
console.log('se ha cambiado en memoria', this.#vault)
if (!this.#ls_outdated)
window.addEventListener('storage', this.#LSVaultListener)
this.#ls_outdated = true
localStorage.setItem(VAULT, JSON.stringify(this.#vault))
}

getConfig(userName) {
if (this.getVault()[userName]) {
return this.getVault()[userName]
} else return null
}

setConfig(userName, accent, variant) {
if (tts.isAValidVoice(accent)) {
this.#vault[userName] = {
accent,
variant: tts.isAValidVariant(accent, variant) ? variant : 1
}
this.#lsIsOutdated()
}
}

resetConfig(userName) {
if (this.#vault[userName]) {
this.#vault[userName] = undefined
this.#ls_outdated = true
}

this.#lsIsOutdated()
}
}

const ttsConfigVault = new TTSConfigVault()

export default ttsConfigVault

0 comments on commit 492aca5

Please sign in to comment.