Skip to content

Commit

Permalink
Update TTSPlayer.js
Browse files Browse the repository at this point in the history
  • Loading branch information
xjuanfxx authored Oct 28, 2024
1 parent 28f5700 commit fd2cc78
Showing 1 changed file with 103 additions and 103 deletions.
206 changes: 103 additions & 103 deletions src/classes/tts/TTSPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit fd2cc78

Please sign in to comment.