首页 > 解决方案 > 如何记录用户输入以在以后嵌入 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 预算部分中发布预算,但它只是说未定义预算

标签: javascriptdiscorddiscord.js

解决方案


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!');
  });

推荐阅读