2 回答

TA貢獻1785條經驗 獲得超8個贊
您是const budget在不同于全局范圍的范圍內定義 的(有關范圍,請參閱此頁面)。
這個答案解釋了聲明、變量和范圍如何協同工作。
在這里,您budget僅在awaitMessages.then范圍內可用,即
.then(messages => {
msg.channel.send(`You've entered: ${messages.first().content}`);
const budget = messages.first().content;
// the const is only know there
})
但是,該then塊將返回一個值。因為不再有鏈式承諾(除非有錯誤,因為它會觸發鏈式catch)。在此處了解有關 promise 的更多信息。
有用的是,一旦承諾被解決,msg.channel.awaitMessages將返回一個值。
然后你可以做兩件事:
等待 的響應msg.channel.awaitMessages,將其分配給變量并稍后使用
鏈接另一個承諾
等待:
let budget = await msg.channel.awaitMessages(filter, { time: 60000, maxMatches: 1, errors: ['time'] })
.then(messages => {
msg.channel.send(`You've entered: ${messages.first().content}`);
return messages.first().content;
})
.catch(() => {
msg.channel.send('You did not enter any input!');
});
});
if (messageReaction.emoji.name === reactions.one) {
let web = new Discord.RichEmbed()
.setDescription("Press the check to claim the comission")
.setColor("#15f153")
.addField("Client", `${message.author} with ID: ${message.author.id}`)
.addField("Budget", `${budget}`)
.addField("Time", message.createdAt)
.addField("Requested Freelancer",`<@&603466765594525716>`)
let tickets = message.guild.channels.find('name', "tickets")
if(!tickets) return message.channel.send(`${message.author} Can't find tickets channel.`)
// ...
}
鏈接:
msg.channel.awaitMessages(filter, { time: 60000, maxMatches: 1, errors: ['time'] })
.then(messages => {
msg.channel.send(`You've entered: ${messages.first().content}`);
return messages.first().content;
})
.then((budget) => {
if (messageReaction.emoji.name === reactions.one) {
let web = new Discord.RichEmbed()
.setDescription("Press the check to claim the comission")
.setColor("#15f153")
.addField("Client", `${message.author} with ID: ${message.author.id}`)
.addField("Budget", `${budget}`)
.addField("Time", message.createdAt)
.addField("Requested Freelancer",`<@&603466765594525716>`)
let tickets = message.guild.channels.find('name', "tickets")
if(!tickets) return message.channel.send(`${message.author} Can't find tickets channel.`)
// ...
}
})
.catch(() => {
msg.channel.send('You did not enter any input!');
});
添加回答
舉報