簡體   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