Discord bot youtube 搜索,等待异步函数的回答
Discord bot youtube search, wait for answer of asynchronous function
我建立了一个 discord 但使用 javascript 我有一个命令,我希望能够在其中搜索 youtube 视频并在语音频道中播放第一个结果。
我正在使用 discordjs and discord-youtube-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 搜索视频 return 其 url.
的功能
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 函数找到 link 之前加载流,因此没有提供有效的 url。虽然我不知道如何解决这个问题,因为该函数需要异步才能等待 youtube 搜索结果。
您可以 运行 在 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;
})
我现在几乎可以肯定这是不真实的。但是,这是解决方案:
const stream = ytdl(searchYouTubeAsync(args).toString(), { filter: 'audioonly' });
xxx.toString() 当我在 ytdl 遇到同样的问题时帮助了我。
我建立了一个 discord 但使用 javascript 我有一个命令,我希望能够在其中搜索 youtube 视频并在语音频道中播放第一个结果。
我正在使用 discordjs and discord-youtube-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 搜索视频 return 其 url.
的功能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 函数找到 link 之前加载流,因此没有提供有效的 url。虽然我不知道如何解决这个问题,因为该函数需要异步才能等待 youtube 搜索结果。
您可以 运行 在 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;
})
我现在几乎可以肯定这是不真实的。但是,这是解决方案:
const stream = ytdl(searchYouTubeAsync(args).toString(), { filter: 'audioonly' });
xxx.toString() 当我在 ytdl 遇到同样的问题时帮助了我。