如何使文本到语音队列node.js

发布于 2025-02-01 22:41:28 字数 1972 浏览 2 评论 0原文

对于像你们中的某些人这样的专业人士来说,这可能是一件非常简单的事情,我希望您能帮助我,感谢您的时间,谢谢。

我有这个TTS Discord Bot,它可以工作!但是我不知道如何排队额外的传入TTS请求。

当当前TTS播放并提交新请求时,当前TTS将停止并开始执行下一个请求,而无需让当前的TTS完成。

我要做的是排队所有请求,因此每个启动每次播放后都会播放。

有人告诉我使用这个软件包,但我无法弄清楚。

我是一个知识非常有限的菜鸟,所以有人可以添加队列所需的额外行吗?还是提供好指南?

很抱歉太挑剔了,我知道我不应该要求太多,但是我已经在处理这个问题已经好几个星期了,我很拼命。

这是我的代码:

const { getAudioUrl } = require('google-tts-api');

module.exports = {
  name: 'say',
  aliases: ['s'],
  cooldown: 3,
  description: "tts",

  execute: async (message, args, cmd, client, Discord) => {
    console.log('Say command executed');

    if (!args[0]) 
      return message.channel.send('you gotta include a message!');
    
    const string = args.join(' ');

    if (string.length > 200) 
      return message.channel.send('the message cant be more than 200 letters!');
    
    const voiceChannel = message.member.voice.channel;

    if (!voiceChannel) 
      return message.reply('You have to be in a voice channel to send a message!');

    const audioURL = getAudioUrl(string, {
      lang: 'en',
      slow: false,
      host: 'https://translate.google.com',
      timeout: 10000,
    });

    try {
      message.channel.startTyping();

      setTimeout(function () {
        message.channel.send('Speaking your msg...');
        message.channel.stopTyping();
        console.log('Now starting to talk');
      }, 1000);

      voiceChannel.join().then(connection => {
        const dispatcher = connection.play(audioURL);
        dispatcher.on('finish', () => {
          console.log('Done talking');
        });
      });
    }
    catch (e) {
      message.channel.send('Bot error, please try again or try later');
      console.error(e);
    }

    setTimeout(function () {
      voiceChannel.leave();
    }, 240000);
  }
}

This might be a very simple thing to do for the pros like some of you, I hope you can help me, I will really appreciate your time, thanks.

I have this TTS discord bot, and it works! But I can't figure out how to queue extra incoming TTS requests.

When current TTS is playing and a new request is submitted, current TTS will stop and start executing next request without letting the current TTS finish.

What I want to do is queue all requests so every single one plays after each finishes.

Some one told me to use this package but I just can't figure it out.

I'm a noob with very limited knowledge, so can someone please add the extra lines that are needed for queues? Or provide a good guide?

I'm sorry for being too picky I know I shouldn't ask for too much, but I've been dealing with this issue for weeks now and I'm desperate.

Here is my code:

const { getAudioUrl } = require('google-tts-api');

module.exports = {
  name: 'say',
  aliases: ['s'],
  cooldown: 3,
  description: "tts",

  execute: async (message, args, cmd, client, Discord) => {
    console.log('Say command executed');

    if (!args[0]) 
      return message.channel.send('you gotta include a message!');
    
    const string = args.join(' ');

    if (string.length > 200) 
      return message.channel.send('the message cant be more than 200 letters!');
    
    const voiceChannel = message.member.voice.channel;

    if (!voiceChannel) 
      return message.reply('You have to be in a voice channel to send a message!');

    const audioURL = getAudioUrl(string, {
      lang: 'en',
      slow: false,
      host: 'https://translate.google.com',
      timeout: 10000,
    });

    try {
      message.channel.startTyping();

      setTimeout(function () {
        message.channel.send('Speaking your msg...');
        message.channel.stopTyping();
        console.log('Now starting to talk');
      }, 1000);

      voiceChannel.join().then(connection => {
        const dispatcher = connection.play(audioURL);
        dispatcher.on('finish', () => {
          console.log('Done talking');
        });
      });
    }
    catch (e) {
      message.channel.send('Bot error, please try again or try later');
      console.error(e);
    }

    setTimeout(function () {
      voiceChannel.leave();
    }, 240000);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2025-02-08 22:41:28

使用队列软件包您“按” execute逻辑包裹在功能中。然后将由队列执行该功能。这里棘手的部分是您需要结束调度程序的完成事件处理程序内的执行。您可以引入一个新的承诺,然后将从活动处理程序内部解决。

Here's a rough sketch of the solution:

const { getAudioUrl } = require('google-tts-api');
const queue = require('queue');

// Create the queue where we will push our jobs
const q = queue({ concurrency: 1, autostart: true });

module.exports = {
  name: 'say',
  aliases: ['s'],
  cooldown: 3,
  description: 'tts',

  execute: (message, args, cmd, client, Discord) => {
    // Enqueue task
    q.push(async () => {
      console.log('Say command executed');

      if (!args[0]) {
        return message.channel.send('you gotta include a message!');
      }

      const string = args.join(' ');

      if (string.length > 200) {
        return message.channel.send(
          'the message cant be more than 200 letters!'
        );
      }

      const voiceChannel = message.member.voice.channel;

      if (!voiceChannel) {
        return message.reply(
          'You have to be in a voice channel to send a message!'
        );
      }

      const audioURL = getAudioUrl(string, {
        lang: 'en',
        slow: false,
        host: 'https://translate.google.com',
        timeout: 10000,
      });

      try {
        message.channel.startTyping();

        setTimeout(function () {
          message.channel.send('Speaking your msg...');
          message.channel.stopTyping();
          console.log('Now starting to talk');
        }, 1000);

        const connection = await voiceChannel.join();
        const dispatcher = connection.play(audioURL);
        // Here we need to handle the promise resolution from the nested event handler
        return Promise.new((resolve, reject) => {
          dispatcher.on('finish', () => {
            console.log('Done talking');
            // voiceChannel.leave();
            resolve();
          });
          dispatcher.on('error', (err) => {
            console.log('Some error in dispatcher happenned!', err);
            reject(err);
          });
        });
      } catch (e) {
        message.channel.send('Bot error, please try again or try later');
        console.error(e);
      }

      // This code won't be called
      setTimeout(function () {
        voiceChannel.leave();
      }, 240000);
    });
  },
};

Take this solution with a grain of salt.我不知道您要定位哪种版本的discord.js。另请注意,超时和可能未处理其他错误条件。

With queue package you "push" your execute logic wrapped in a function. The function will be then executed by the queue. The tricky part here is that you need to end the execution inside the dispatcher's finish event handler. You can introduce a new promise which will be then resolved from inside the event handler.

Here's a rough sketch of the solution:

const { getAudioUrl } = require('google-tts-api');
const queue = require('queue');

// Create the queue where we will push our jobs
const q = queue({ concurrency: 1, autostart: true });

module.exports = {
  name: 'say',
  aliases: ['s'],
  cooldown: 3,
  description: 'tts',

  execute: (message, args, cmd, client, Discord) => {
    // Enqueue task
    q.push(async () => {
      console.log('Say command executed');

      if (!args[0]) {
        return message.channel.send('you gotta include a message!');
      }

      const string = args.join(' ');

      if (string.length > 200) {
        return message.channel.send(
          'the message cant be more than 200 letters!'
        );
      }

      const voiceChannel = message.member.voice.channel;

      if (!voiceChannel) {
        return message.reply(
          'You have to be in a voice channel to send a message!'
        );
      }

      const audioURL = getAudioUrl(string, {
        lang: 'en',
        slow: false,
        host: 'https://translate.google.com',
        timeout: 10000,
      });

      try {
        message.channel.startTyping();

        setTimeout(function () {
          message.channel.send('Speaking your msg...');
          message.channel.stopTyping();
          console.log('Now starting to talk');
        }, 1000);

        const connection = await voiceChannel.join();
        const dispatcher = connection.play(audioURL);
        // Here we need to handle the promise resolution from the nested event handler
        return Promise.new((resolve, reject) => {
          dispatcher.on('finish', () => {
            console.log('Done talking');
            // voiceChannel.leave();
            resolve();
          });
          dispatcher.on('error', (err) => {
            console.log('Some error in dispatcher happenned!', err);
            reject(err);
          });
        });
      } catch (e) {
        message.channel.send('Bot error, please try again or try later');
        console.error(e);
      }

      // This code won't be called
      setTimeout(function () {
        voiceChannel.leave();
      }, 240000);
    });
  },
};

Take this solution with a grain of salt. I didn't figure out what version of Discord.js you are targeting. Also note that timeouts and possible other error conditions are not handled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文