我已经建立了一个不和谐,但是使用javascript,我有一个命令,我希望能够搜索youtube视频,并在语音频道中播放第一个结果。
我正在使用discordjs和discord-youtup-api库。
此代码查找要搜索的命令。args数组是搜索查询
else if (command === 'search') {
isReady = false;
if (message.channel.type !== 'text') return;
const { voiceChannel } = message.member;
if (!voiceChannel) {
return message.reply('please join a voice channel first!');
}
voiceChannel.join().then(connection => {
const stream = ytdl(searchYouTubeAsync(args), { filter: 'audioonly' });
const dispatcher = connection.playStream(stream);
dispatcher.on('end', () => voiceChannel.leave());
isReady = true;
})
};
这就是使用youtube api搜索视频并返回其网址的功能。
async function searchYouTubeAsync(args) {
var video = await youtube.searchVideos(args.toString().replace(/,/g,' '));
console.log(video.url);
console.log(typeof String(video.url));
return String(video.url);
}
尝试该命令时,我确实收到以下错误消息。
(node:13141) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type object
at Url.parse (url.js:146:11)
at Object.urlParse [as parse] (url.js:140:13)
at Object.exports.getURLVideoID (/Users/alexanderhoerl/Developer/discord-music-bot/node_modules/ytdl-core/lib/util.js:248:20)
at Object.exports.getVideoID (/Users/alexanderhoerl/Developer/discord-music-bot/node_modules/ytdl-core/lib/util.js:279:20)
at getInfo (/Users/alexanderhoerl/Developer/discord-music-bot/node_modules/ytdl-core/lib/info.js:46:17)
at ytdl (/Users/alexanderhoerl/Developer/discord-music-bot/node_modules/ytdl-core/lib/index.js:17:3)
at voiceChannel.join.then.connection (/Users/alexanderhoerl/Developer/discord-music-bot/index.js:89:24)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:13141) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:13141) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我猜问题在于,音乐机器人试图在searchYouTube函数找到链接之前加载流,因此没有提供有效的url。虽然我不知道如何解决这个问题,因为函数需要异步才能等待youtube搜索结果。
我几乎可以肯定现在这是不真实的。然而,以下是解决方案:
const stream = ytdl(searchYouTubeAsync(args).toString(), { filter: 'audioonly' });
xxx。当我和ytdl有同样的问题时,toString()让我很沮丧。
您可以在async
函数中运行:
voiceChannel.join().then(async connection => {
let url = await searchYouTubeAsync(args);
let stream = ytdl(url, { filter: 'audioonly' });
let dispatcher = connection.playStream(stream);
dispatcher.on('end', () => voiceChannel.leave());
isReady = true;
})
我正在尝试做一个简单的应用程序,加载数据并对其执行一个操作.所以我的想法是做这个异步。 我有3个数据源,我想异步加载它们。例如data1.xml、data2.xml和data3.xml所有文件加载起来都相当大,所以需要一些时间(这就是为什么我想要异步的原因)。 例如,我创建了一个包含3个文本框的窗口,这些文本框都绑定到一个特定的属性(Text1、Text2、Text3)和一个按钮。当我点击按钮时,
问题内容: 我正在将其用作搜索的一部分,但必须使用+30000个项。 问题在于,搜索的初始部分是在搜索之前完成的,因此触发和功能将继续进行。我应该怎么做才能阻止这种情况? 问题答案: 所以,我已经重新编写了它,以便在这里帮助任何需要它的人。
问题内容: 我正在尝试使用新的异步功能,希望解决我的问题以后能对其他人有所帮助。这是我的代码正在工作: 问题是,我的while循环运行得太快,脚本每秒向Google API发送太多请求。因此,我想构建一个睡眠函数以延迟请求。因此,我也可以使用此功能来延迟其他请求。如果还有其他方法可以延迟请求,请告诉我。 无论如何,这是我的新代码不起作用。请求的响应在setTimeout中返回给匿名异步函数,但是我
我用webpack建立了一个浏览器umd库。 我正在侦听输入文件的onchange事件。当有人提交图像/文件时,它会将其转换为base64。我试图让它尽可能看起来不那么明显,所以我使用了promises和wait/asynchttps://blog.shovonhasan.com/using-promises-with-filereader/. 但是,有一个问题-当我调用convertToBase
我已经在不和谐机器人上工作了几天。 起初只是简单的命令等,但慢慢地,我也致力于音乐机器人的主题。 我也使用YouTube数据API来实现这一点,到目前为止一切都正常。不过,我现在想合并一个Youtube搜索命令或将其构建到另一个(Play命令)中。我已经有半个搜索命令了。 到目前为止你可以做$play(歌名) 将选择找到的第一个轨迹。 然而,我希望能够看到前10个搜索结果,然后在它们之间进行选择。
问题内容: 据我了解,在ES7 /ES2016中,将多个in放在代码中的工作方式类似于带有promise的链接,这意味着它们将一个接一个地执行而不是并行执行。因此,例如,我们有以下代码: 我是否正确理解仅在完成时才会调用?并行调用它们的最优雅方式是什么? 我想在Node中使用它,所以也许有一个异步库解决方案? 编辑:我对这个问题提供的解决方案不满意:减速是由于异步生成器中非并行等待Promise的