如何记录用户输入以在以后嵌入 discordjs 时使用

我正在制作一个佣金机器人,所以人们打开一张票,然后选择它的类别,但然后我希望它要求预算等待回复,然后存储该输入预算以用于嵌入以发布给自由职业者。


我已经尝试将其存储为常量然后稍后调用它,但它不想工作,因为我将它存储在不同的函数中。


msg.channel.awaitMessages(filter, { time: 60000, maxMatches: 1, errors: ['time'] })

        .then(messages => {

            msg.channel.send(`You've entered: ${messages.first().content}`);

            const budget = 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.`)

我希望它在 .addField 预算部分发布预算,但它只是说预算未定义


哈士奇WWW
浏览 154回答 2
2回答

慕的地10843

您是const budget在不同于全局范围的范围内定义 的(有关范围,请参阅此页面)。这个答案解释了声明、变量和范围如何协同工作。在这里,您budget仅在awaitMessages.then范围内可用,即.then(messages => {&nbsp; msg.channel.send(`You've entered: ${messages.first().content}`);&nbsp; const budget = messages.first().content;&nbsp; // 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'] })&nbsp; .then(messages => {&nbsp; &nbsp; msg.channel.send(`You've entered: ${messages.first().content}`);&nbsp; &nbsp; return messages.first().content;&nbsp; })&nbsp; .catch(() => {&nbsp; &nbsp; msg.channel.send('You did not enter any input!');&nbsp; });});if (messageReaction.emoji.name === reactions.one) {&nbsp; let web = new Discord.RichEmbed()&nbsp; &nbsp; .setDescription("Press the check to claim the comission")&nbsp; &nbsp; .setColor("#15f153")&nbsp; &nbsp; .addField("Client", `${message.author} with ID: ${message.author.id}`)&nbsp; &nbsp; .addField("Budget", `${budget}`)&nbsp; &nbsp; .addField("Time", message.createdAt)&nbsp; &nbsp; .addField("Requested Freelancer",`<@&603466765594525716>`)&nbsp;let tickets = message.guild.channels.find('name', "tickets")&nbsp;if(!tickets) return message.channel.send(`${message.author} Can't find tickets channel.`)// ...}链接:msg.channel.awaitMessages(filter, { time: 60000, maxMatches: 1, errors: ['time'] })&nbsp; .then(messages => {&nbsp; &nbsp; msg.channel.send(`You've entered: ${messages.first().content}`);&nbsp; &nbsp; return messages.first().content;&nbsp; })&nbsp; .then((budget) => {&nbsp; &nbsp; if (messageReaction.emoji.name === reactions.one) {&nbsp; &nbsp; &nbsp; let web = new Discord.RichEmbed()&nbsp; &nbsp; &nbsp; &nbsp; .setDescription("Press the check to claim the comission")&nbsp; &nbsp; &nbsp; &nbsp; .setColor("#15f153")&nbsp; &nbsp; &nbsp; &nbsp; .addField("Client", `${message.author} with ID: ${message.author.id}`)&nbsp; &nbsp; &nbsp; &nbsp; .addField("Budget", `${budget}`)&nbsp; &nbsp; &nbsp; &nbsp; .addField("Time", message.createdAt)&nbsp; &nbsp; &nbsp; &nbsp; .addField("Requested Freelancer",`<@&603466765594525716>`)&nbsp; &nbsp; &nbsp; let tickets = message.guild.channels.find('name', "tickets")&nbsp; &nbsp; &nbsp; if(!tickets) return message.channel.send(`${message.author} Can't find tickets channel.`)&nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }&nbsp; })&nbsp; .catch(() => {&nbsp; &nbsp; msg.channel.send('You did not enter any input!');&nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript