繁体   English   中英

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

[英]How to make Text to speech queue Node.js

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

我有这个 TTS discord 机器人,它可以工作! 但我不知道如何对额外传入的 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);
  }
}

使用queue包,您可以“推送”包装在函数中的execute逻辑。 然后该函数将由队列执行。 这里棘手的部分是您需要在调度程序的finish事件处理程序中结束执行。 您可以引入一个新的承诺,然后从事件处理程序内部解决。

这是解决方案的粗略草图:

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);
    });
  },
};

将此溶液与一粒盐一起服用。 我没有弄清楚你的目标是什么版本的 Discord.js。 另请注意,不处理超时和可能的其他错误情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM