如何使文本到语音队列node.js
对于像你们中的某些人这样的专业人士来说,这可能是一件非常简单的事情,我希望您能帮助我,感谢您的时间,谢谢。
我有这个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
队列
软件包您“按”execute
逻辑包裹在功能中。然后将由队列执行该功能。这里棘手的部分是您需要结束调度程序的完成
事件处理程序内的执行。您可以引入一个新的承诺,然后将从活动处理程序内部解决。Here's a rough sketch of the solution:
Take this solution with a grain of salt.我不知道您要定位哪种版本的discord.js。另请注意,超时和可能未处理其他错误条件。
With
queue
package you "push" yourexecute
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'sfinish
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:
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.