gpt4 book ai didi

javascript - 无法在 DiscordJS 中通过不同作用域传递变量

转载 作者:行者123 更新时间:2023-12-02 22:01:30 24 4
gpt4 key购买 nike

好吧,我的问题是,在第一组 console.log(streamXXXX) 中,其中 XXXX 是各种变量,当我读取它们的值时,它们都按应有的方式读取,而在第二组他们读作未定义。这是范围问题吗?也许是异步问题?我尝试在每次发出网络请求时添加等待,但似乎没有任何效果,最有趣的部分之一是没有错误?

无论如何,下面列出了我的代码,以及使用我创建的示例机器人在 Repl 中测试它的链接。下面是该程序运行所需的库列表。谢谢!

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) message.member.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(' ')) {
youtube.getVideo(streamURL)
.then(async results => {
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

console.log(streamURL)
console.log(streamTitle)
console.log(streamThumb)
console.log(streamAuth)
console.log(streamAuthThumb)
})
.catch(console.error)
} else if (!streamURL.includes("https://www.youtube.com") || !streamURL.includes("https://youtu.be/")) {
youtube.searchVideos(streamURL)
.then(async results => {
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.default.medium.url
streamAuth = results[0].channel.title
streamAuthThumb = body.items[0].snippet.thumbnails.medium.url
}).catch(console.error)
} else {
return message.reply("I can only play videos from YouTube (#NotSponsored).")
}

console.log(streamURL)
console.log(streamTitle)
console.log(streamThumb)
console.log(streamAuth)
console.log(streamAuthThumb)

const stream = ytdl(streamURL, {
filter: 'audioonly'
})
const dispatcher = connection.playStream(stream)
dispatcher.on("end", end => {
return
})

let musicEmbed = new Discord.RichEmbed()
.setAuthor(streamAuth, streamAuthThumb)
.setTitle(`Now Playing: ${streamTitle}`)
.setImage(streamThumb)
.setColor(embedRed)
.setFooter(`${streamAuth} - ${streamTitle} (${streamURL}`)

await message.channel.send(musicEmbed)
})

Link to test out the program on a sample bot I made

您需要测试的模块:

discord.js
simple-youtube-api
node-opus
ffmpeg
ffbinaries
ffmpeg-binaries
opusscript
snekfetch
node-fetch
ytdl-core

再次感谢!

最佳答案

您的输出未定义的原因是由于promise的工作方式以及您构建代码的方式:

let streamTitle = "";

// 1. Promise created
youtube.getVideo(streamURL)
// 2. Promise still pending, skip for now
.then(async results => {
// 4. Promise fulfilled
console.log(results.title); // 5. Logged actual title
});

console.log(streamTitle); // 3. Logged ""

您已经有了针对 snekfetch 请求的正确方法,只需将其也应用到 YT 请求即可:

let streamTitle = "";

const results = await youtube.getVideo(streamURL);

streamTitle = results.title;

console.log(streamTitle); // Desired output

关于javascript - 无法在 DiscordJS 中通过不同作用域传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59864044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com