亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何記錄用戶輸入以在以后嵌入 discordjs 時使用

如何記錄用戶輸入以在以后嵌入 discordjs 時使用

哈士奇WWW 2021-08-20 18:12:09
我正在制作一個傭金機器人,所以人們打開一張票,然后選擇它的類別,但然后我希望它要求預算等待回復,然后存儲該輸入預算以用于嵌入以發布給自由職業者。我已經嘗試將其存儲為常量然后稍后調用它,但它不想工作,因為我將它存儲在不同的函數中。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 預算部分發布預算,但它只是說預算未定義
查看完整描述

2 回答

?
慕的地10843

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

  });


查看完整回答
反對 回復 2021-08-20
  • 2 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號