From a6c6ad9646efce05760d226d776a1b79cf664681 Mon Sep 17 00:00:00 2001 From: xjuanfxx <126913334+xjuanfxx@users.noreply.github.com> Date: Mon, 28 Oct 2024 03:36:41 -0700 Subject: [PATCH 1/5] Update TTSPlayer.js Links / URLs are omitted --- src/classes/tts/TTSPlayer.js | 45 +++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/classes/tts/TTSPlayer.js b/src/classes/tts/TTSPlayer.js index cf2c330d..b2e001b1 100644 --- a/src/classes/tts/TTSPlayer.js +++ b/src/classes/tts/TTSPlayer.js @@ -41,22 +41,27 @@ class TTSPlayer { } async say(sentence, providerName, extras = {}) { - const provider = this.providerManager.getProvider(providerName); - const payload = await provider.createPayload(sentence, extras); + // URL Detector + const urlRegex = /(https?:\/\/[^\s]+)/g; - if (Array.isArray(payload)) { - payload.forEach((p) => this.queue.enqueue(p)); - } else { - this.queue.enqueue(payload); - } + // Replace URL with SUS + let sanitizedSentence = sentence.replace(urlRegex, 'ඞ'); - this.startDisconnectScheduler(); + const provider = this.providerManager.getProvider(providerName); + const payload = await provider.createPayload(sanitizedSentence, extras); - if (!this.speaking) { - this.play(); - } + if (Array.isArray(payload)) { + payload.forEach((p) => this.queue.enqueue(p)); + } else { + this.queue.enqueue(payload); } + this.startDisconnectScheduler(); + + if (!this.speaking) { + this.play(); + } +} async play() { if (this.queue.isEmpty()) { return; @@ -75,6 +80,24 @@ class TTSPlayer { })); } + skip() { + this.stopDisconnectScheduler(); + + if (this.queue.length > 0) { + this.queue.shift(); + } + + this.speaking = false; + this.voice.player.stop(true); + + if (this.queue.length > 0) { + const nextMessage = this.queue[0]; + this.say(nextMessage.text, nextMessage.provider, nextMessage.extras); + } else { + this.voice.disconnect(); + } + } + stop() { const { channel } = this.guild.me.voice; From 28f570080bf688191aaeaeac3850633f26ce7c4d Mon Sep 17 00:00:00 2001 From: xjuanfxx <126913334+xjuanfxx@users.noreply.github.com> Date: Mon, 28 Oct 2024 03:58:15 -0700 Subject: [PATCH 2/5] Update TTSPlayer.js --- src/classes/tts/TTSPlayer.js | 215 ++++++++++++++++++----------------- 1 file changed, 108 insertions(+), 107 deletions(-) diff --git a/src/classes/tts/TTSPlayer.js b/src/classes/tts/TTSPlayer.js index b2e001b1..4c633bd1 100644 --- a/src/classes/tts/TTSPlayer.js +++ b/src/classes/tts/TTSPlayer.js @@ -4,134 +4,135 @@ const Queue = require('../Queue'); const VoiceManager = require('../VoiceManager'); class TTSPlayer { - constructor(client, guild, disconnectScheduler) { - this.client = client; - this.guild = guild; - this.disconnectScheduler = disconnectScheduler; - this.providerManager = client.providerManager; - - this.queue = new Queue(); - this.speaking = false; - this.voice = new VoiceManager(guild); - - this.initializePlayer(); - this.initializeScheduler(); - } - - initializePlayer() { - this.voice.player.on('error', (error) => { - logger.error(error); - this.speaking = false; - this.play(); - }); - - this.voice.player.on('idle', () => { - if (this.speaking) { + constructor(client, guild, disconnectScheduler) { + this.client = client; + this.guild = guild; + this.disconnectScheduler = disconnectScheduler; + this.providerManager = client.providerManager; + + this.queue = new Queue(); this.speaking = false; - this.play(); - } - }); - } - - initializeScheduler() { - this.disconnectScheduler.set(() => { - const channel = this.stop(); - logger.warn(`Left ${channel.name} from ${this.guild.name} due to inactivity.`); - }); - } - - async say(sentence, providerName, extras = {}) { - // URL Detector - const urlRegex = /(https?:\/\/[^\s]+)/g; - - // Replace URL with SUS - let sanitizedSentence = sentence.replace(urlRegex, 'ඞ'); - - const provider = this.providerManager.getProvider(providerName); - const payload = await provider.createPayload(sanitizedSentence, extras); - - if (Array.isArray(payload)) { - payload.forEach((p) => this.queue.enqueue(p)); - } else { - this.queue.enqueue(payload); - } - - this.startDisconnectScheduler(); - - if (!this.speaking) { - this.play(); - } -} - async play() { - if (this.queue.isEmpty()) { - return; + this.voice = new VoiceManager(guild); + + this.initializePlayer(); + this.initializeScheduler(); + } + + initializePlayer() { + this.voice.player.on('error', (error) => { + logger.error(error); + this.speaking = false; + this.play(); + }); + + this.voice.player.on('idle', () => { + if (this.speaking) { + this.speaking = false; + this.play(); + } + }); } - const payload = this.queue.dequeue(); - const provider = this.providerManager.getProvider(payload.providerName); + initializeScheduler() { + this.disconnectScheduler.set(() => { + const channel = this.stop(); + logger.warn(`Left ${channel.name} from ${this.guild.name} due to inactivity.`); + }); + } - logger.info(provider.getPlayLogMessage(payload, this.guild)); + async say(sentence, providerName, extras = {}) { + // URL Detector + const urlRegex = /(https?:\/\/[^\s]+)/g; - this.speaking = true; - this.voice.play(createAudioResource(payload.resource, { - metadata: { - title: payload.sentence - } - })); - } + // Replace URL with SUS + const sanitizedSentence = sentence.replace(urlRegex, 'ඞ'); - skip() { - this.stopDisconnectScheduler(); + const provider = this.providerManager.getProvider(providerName); + const payload = await provider.createPayload(sanitizedSentence, extras); - if (this.queue.length > 0) { - this.queue.shift(); - } + if (Array.isArray(payload)) { + payload.forEach((p) => this.queue.enqueue(p)); + } else { + this.queue.enqueue(payload); + } - this.speaking = false; - this.voice.player.stop(true); + this.startDisconnectScheduler(); - if (this.queue.length > 0) { - const nextMessage = this.queue[0]; - this.say(nextMessage.text, nextMessage.provider, nextMessage.extras); - } else { - this.voice.disconnect(); + if (!this.speaking) { + this.play(); + } } - } - - stop() { - const { channel } = this.guild.me.voice; - this.stopDisconnectScheduler(); + async play() { + if (this.queue.isEmpty()) { + return; + } + + const payload = this.queue.dequeue(); + const provider = this.providerManager.getProvider(payload.providerName); + + logger.info(provider.getPlayLogMessage(payload, this.guild)); - this.queue.clear(); - this.speaking = false; - this.voice.disconnect(); - this.voice.player.stop(true); + this.speaking = true; + this.voice.play(createAudioResource(payload.resource, { + metadata: { + title: payload.sentence + } + })); + } + + skip() { + this.stopDisconnectScheduler(); - return channel || { name: 'null' }; - } + if (this.queue.length > 0) { + this.queue.shift(); + } - startDisconnectScheduler() { - if (!this.disconnectScheduler) { - return; + this.speaking = false; + this.voice.player.stop(true); + + if (this.queue.length > 0) { + const nextMessage = this.queue[0]; + this.say(nextMessage.text, nextMessage.provider, nextMessage.extras); + } else { + this.voice.disconnect(); + } } - if (this.disconnectScheduler.isAlive()) { - this.disconnectScheduler.refresh(); - } else { - this.disconnectScheduler.start(this); + stop() { + const { channel } = this.guild.me.voice; + + this.stopDisconnectScheduler(); + + this.queue.clear(); + this.speaking = false; + this.voice.disconnect(); + this.voice.player.stop(true); + + return channel || { name: 'null' }; } - } - stopDisconnectScheduler() { - if (!this.disconnectScheduler) { - return; + startDisconnectScheduler() { + if (!this.disconnectScheduler) { + return; + } + + if (this.disconnectScheduler.isAlive()) { + this.disconnectScheduler.refresh(); + } else { + this.disconnectScheduler.start(this); + } } - if (this.disconnectScheduler.isAlive()) { - this.disconnectScheduler.stop(); + stopDisconnectScheduler() { + if (!this.disconnectScheduler) { + return; + } + + if (this.disconnectScheduler.isAlive()) { + this.disconnectScheduler.stop(); + } } - } } module.exports = TTSPlayer; From fd2cc78040ce2f3e430df4eb445c10eb3847c52f Mon Sep 17 00:00:00 2001 From: xjuanfxx <126913334+xjuanfxx@users.noreply.github.com> Date: Mon, 28 Oct 2024 04:03:22 -0700 Subject: [PATCH 3/5] Update TTSPlayer.js --- src/classes/tts/TTSPlayer.js | 206 +++++++++++++++++------------------ 1 file changed, 103 insertions(+), 103 deletions(-) diff --git a/src/classes/tts/TTSPlayer.js b/src/classes/tts/TTSPlayer.js index 4c633bd1..99b8b41e 100644 --- a/src/classes/tts/TTSPlayer.js +++ b/src/classes/tts/TTSPlayer.js @@ -4,135 +4,135 @@ const Queue = require('../Queue'); const VoiceManager = require('../VoiceManager'); class TTSPlayer { - constructor(client, guild, disconnectScheduler) { - this.client = client; - this.guild = guild; - this.disconnectScheduler = disconnectScheduler; - this.providerManager = client.providerManager; - - this.queue = new Queue(); + constructor(client, guild, disconnectScheduler) { + this.client = client; + this.guild = guild; + this.disconnectScheduler = disconnectScheduler; + this.providerManager = client.providerManager; + + this.queue = new Queue(); + this.speaking = false; + this.voice = new VoiceManager(guild); + + this.initializePlayer(); + this.initializeScheduler(); + } + + initializePlayer() { + this.voice.player.on('error', (error) => { + logger.error(error); + this.speaking = false; + this.play(); + }); + + this.voice.player.on('idle', () => { + if (this.speaking) { this.speaking = false; - this.voice = new VoiceManager(guild); - - this.initializePlayer(); - this.initializeScheduler(); + this.play(); + } + }); + } + + initializeScheduler() { + this.disconnectScheduler.set(() => { + const channel = this.stop(); + logger.warn(`Left ${channel.name} from ${this.guild.name} due to inactivity.`); + }); + } + + async say(sentence, providerName, extras = {}) { + // URL Detector + const urlRegex = /(https?:\/\/[^\s]+)/g; + + // Replace URL with SUS + const sanitizedSentence = sentence.replace(urlRegex, 'ඞ'); + + const provider = this.providerManager.getProvider(providerName); + const payload = await provider.createPayload(sanitizedSentence, extras); + + if (Array.isArray(payload)) { + payload.forEach((p) => this.queue.enqueue(p)); + } else { + this.queue.enqueue(payload); } - initializePlayer() { - this.voice.player.on('error', (error) => { - logger.error(error); - this.speaking = false; - this.play(); - }); - - this.voice.player.on('idle', () => { - if (this.speaking) { - this.speaking = false; - this.play(); - } - }); - } + this.startDisconnectScheduler(); - initializeScheduler() { - this.disconnectScheduler.set(() => { - const channel = this.stop(); - logger.warn(`Left ${channel.name} from ${this.guild.name} due to inactivity.`); - }); + if (!this.speaking) { + this.play(); } + } - async say(sentence, providerName, extras = {}) { - // URL Detector - const urlRegex = /(https?:\/\/[^\s]+)/g; + async play() { + if (this.queue.isEmpty()) { + return; + } - // Replace URL with SUS - const sanitizedSentence = sentence.replace(urlRegex, 'ඞ'); + const payload = this.queue.dequeue(); + const provider = this.providerManager.getProvider(payload.providerName); - const provider = this.providerManager.getProvider(providerName); - const payload = await provider.createPayload(sanitizedSentence, extras); + logger.info(provider.getPlayLogMessage(payload, this.guild)); - if (Array.isArray(payload)) { - payload.forEach((p) => this.queue.enqueue(p)); - } else { - this.queue.enqueue(payload); - } + this.speaking = true; + this.voice.play(createAudioResource(payload.resource, { + metadata: { + title: payload.sentence + } + })); + } - this.startDisconnectScheduler(); + skip() { + this.stopDisconnectScheduler(); - if (!this.speaking) { - this.play(); - } + if (this.queue.length > 0) { + this.queue.shift(); } - async play() { - if (this.queue.isEmpty()) { - return; - } + this.speaking = false; + this.voice.player.stop(true); - const payload = this.queue.dequeue(); - const provider = this.providerManager.getProvider(payload.providerName); - - logger.info(provider.getPlayLogMessage(payload, this.guild)); - - this.speaking = true; - this.voice.play(createAudioResource(payload.resource, { - metadata: { - title: payload.sentence - } - })); + if (this.queue.length > 0) { + const nextMessage = this.queue[0]; + this.say(nextMessage.text, nextMessage.provider, nextMessage.extras); + } else { + this.voice.disconnect(); } + } - skip() { - this.stopDisconnectScheduler(); + stop() { + const { channel } = this.guild.me.voice; - if (this.queue.length > 0) { - this.queue.shift(); - } + this.stopDisconnectScheduler(); - this.speaking = false; - this.voice.player.stop(true); - - if (this.queue.length > 0) { - const nextMessage = this.queue[0]; - this.say(nextMessage.text, nextMessage.provider, nextMessage.extras); - } else { - this.voice.disconnect(); - } - } - - stop() { - const { channel } = this.guild.me.voice; - - this.stopDisconnectScheduler(); + this.queue.clear(); + this.speaking = false; + this.voice.disconnect(); + this.voice.player.stop(true); - this.queue.clear(); - this.speaking = false; - this.voice.disconnect(); - this.voice.player.stop(true); + return channel || { name: 'null' }; + } - return channel || { name: 'null' }; + startDisconnectScheduler() { + if (!this.disconnectScheduler) { + return; } - startDisconnectScheduler() { - if (!this.disconnectScheduler) { - return; - } - - if (this.disconnectScheduler.isAlive()) { - this.disconnectScheduler.refresh(); - } else { - this.disconnectScheduler.start(this); - } + if (this.disconnectScheduler.isAlive()) { + this.disconnectScheduler.refresh(); + } else { + this.disconnectScheduler.start(this); } + } - stopDisconnectScheduler() { - if (!this.disconnectScheduler) { - return; - } + stopDisconnectScheduler() { + if (!this.disconnectScheduler) { + return; + } - if (this.disconnectScheduler.isAlive()) { - this.disconnectScheduler.stop(); - } + if (this.disconnectScheduler.isAlive()) { + this.disconnectScheduler.stop(); } + } } module.exports = TTSPlayer; From e535068d77ba206843002cec7a722c8e3c269bdd Mon Sep 17 00:00:00 2001 From: xjuanfxx <126913334+xjuanfxx@users.noreply.github.com> Date: Mon, 28 Oct 2024 04:13:37 -0700 Subject: [PATCH 4/5] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e13544ad..fbb4685d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord-tts-bot", - "version": "4.1.2", + "version": "4.1.3", "description": "A simple Discord TTS Bot that uses the Google Translate TTS API.", "main": "./src/app.js", "scripts": { From 91fd1f5304d6bb54ea7875e4051d3461f9f1ecd5 Mon Sep 17 00:00:00 2001 From: xjuanfxx <126913334+xjuanfxx@users.noreply.github.com> Date: Mon, 28 Oct 2024 04:13:50 -0700 Subject: [PATCH 5/5] Update package-lock.json --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index d165d8a7..8be7d94a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "discord-tts-bot", - "version": "4.1.2", + "version": "4.1.3", "lockfileVersion": 2, "requires": true, "packages": {