-1

I am making a commission bot so people open a ticket and then choose what category it is for but then I want it to ask for a budget wait for a response then store that input budget to be used in an embed to post to freelancers.

I've already tried storing as a constant and then calling it later but it doesn't want to work as I'm storing it in a different function.

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.`)

I want it to post the budget in the .addField budget section but instead, it just says the budget is not defined

  • 1
    `const budget` is defined in a different scope see https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Mateusz Odelga Jul 24 '19 at 19:12
  • If you are callling upon the budget const later on in your code, from outside your Function block, that will cause it to not work. Like Mateusz said, const is only useable in the same scope. You might want to try using a Global Variable. Global variables (you can define one with var) can be called from outside the scope, or several different scopes. I highly suggest doing something like Free Code Camp's Javascript lessons. They have 300 hours of course work that actually instills the knowledge you need to know, unlike just watching somebody else code on a Video. – Erik Russell Jul 24 '19 at 21:23

2 Answers2

1

You are defining the const budget in a scope different than the global scope (see this page for scope).

This answer explains how the declaration, variable and scope work together.

Here, you're making budget available only in the awaitMessages.then scope, ie

.then(messages => {
  msg.channel.send(`You've entered: ${messages.first().content}`);
  const budget = messages.first().content;
  // the const is only know there
})

However, the then block will return a value. Because there isn't anymore chained promise (except if there's an error because it will trigger the chained catch). Learn more on promise here.

What will be useful is, once the promise is resolved, msg.channel.awaitMessages will return a value.

You can then do 2 things:

  • await the response of msg.channel.awaitMessages, assign it to a variable and use it later
  • chain another promise

awaiting:

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.`)
// ...
}

chaining:

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!');
  });
JackRed
  • 1,188
  • 2
  • 12
  • 28
0
let response = await  msg.channel.awaitMessages(filter, { time: 60000, maxMatches: 1, errors: ['time'] })
        .then(messages => {
            msg.channel.send(`You've entered: ${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", `${response.first().content}`)
        .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.`)

Maybe it will fix the problem, because you can't access that block where the const budget is located.

Cursed
  • 988
  • 1
  • 8
  • 24