我看到有人问过与此类似的问题,在解决了这些问题之后,我仍然得出与开始时相同的结论。在下面,我列出了我的代码,一个使用我创建的示例bot对其进行测试的链接以及需要使用自托管实例对其进行测试的模块。
我的问题是,连接到指定的语音通道后,漫游器会立即离开,而没有任何错误或异常的迹象。我不确定我的代码或所用的一个库是否有问题。我收到的唯一控制台输出如下:
Bot Ready For Use
joined channel
left channel
谢谢!
我的代码
var voiceChannel = message.member.voiceChannel;
if (!message.member.voiceChannel) return message.channel.send(`You do realize you have to be in a voice channel to do that, right ${message.author.username}?`)
if (!message.member.voiceConnection) voiceChannel.join().then(async connection => {
let streamURL = args.slice(1).join(" ")
let streamTitle = "";
let streamThumb = "";
let streamAuth = "";
let streamAuthThumb = "";
if (streamURL.includes("https://www.youtube.com") || streamURL.includes("https://youtu.be/") && !streamURL.includes(' ')) {
let results = await youtube.getVideo(streamURL)
let {
body
} = await snekfetch.get(`https://www.googleapis.com/youtube/v3/channels?part=snippet&id=${results.channel.id}&fields=items%2Fsnippet%2Fthumbnails&key=${ytapikey}`).query({
limit: 800
})
streamTitle = results.title
streamThumb = results.thumbnails.medium.url
streamAuth = results.channel.title
streamAuthThumb = body.items[0].snippet.thumbnails.medium.url
} else if (!streamURL.includes("https://www.youtube.com") || !streamURL.includes("https://youtu.be/")) {
let results = await youtube.searchVideos(streamURL)
let {
body
} = await snekfetch.get(`https://www.googleapis.com/youtube/v3/channels?part=snippet&id=${results[0].channel.id}&fields=items%2Fsnippet%2Fthumbnails&key=${ytapikey}`).query({
limit: 800
})
streamURL = results[0].url
streamTitle = results[0].title
streamThumb = results[0].thumbnails.medium.url
streamAuth = results[0].channel.title
streamAuthThumb = body.items[0].snippet.thumbnails.medium.url
} else {
return message.reply("I can only play videos from YouTube (#NotSponsored).")
}
console.log("joined channel");
const stream = ytdl('https://www.youtube.com/watch?v=gOMhN-hfMtY', {
filter: 'audioonly'
});
const dispatcher = connection.playStream(stream, {
seek: 0,
volume: 1
});
dispatcher.on("end", end => {
console.log("left channel");
voiceChannel.leave();
});
let musicEmbed = new Discord.RichEmbed()
.setAuthor(streamAuth, streamAuthThumb)
.setTitle(`Now Playing:`)
.addField(`${streamAuth}`, `${streamTitle}`)
.setImage(streamThumb)
.setColor(embedRed)
.setFooter(`${streamAuth} - ${streamTitle} (${streamURL}`)
message.channel.send(musicEmbed)
}).catch(console.error);
break
模块列表
discord.js
simple-youtube-api
node-opus
ffmpeg
ffbinaries
ffmpeg-binaries
opusscript
snekfetch
node-fetch
ytdl-core
再次感谢!