Access variable in another function, javascript

Asked

Viewed 45 times

-1

Hello, I’m developing a Discord Bot and I’m trying to make a command that needs to access a variable created in another function.

if(msg.content.startsWith('z!setarcanaldenunciasrecebidas')){
    msg.delete();
    var denunciasRecebidas = msg.content.replace('z!setarcanaldenunciasrecebidas','')
    const Embed2 = new Discord.MessageEmbed()
    .setColor('#ff0000')
    .setDescription('*Canal de denúncias recebidas setado com sucesso!*')
    .setTitle(`**Canal Setado ${msg.author.username}! **`)
    if(denunciasRecebidas < 1){
    msg.channel.send('Você precisa mandar o ID do canal de denuncias recebidas!')
}else{
msg.reply(Embed2)
}

}
if(msg.content.startsWith('z!denunciar')){
  msg.delete();
  let denuncia = msg.content.replace('z!denunciar','')
  let canalDenuncia = msg.gild.channels.cache.find(ch => cd.id === denunciasRecebidas)
const Embed = new Discord.MessageEmbed()
.setColor('#ff0000')
.setTitle('Denuncia feita!')
.setDescription('`Sua denuncia foi enviada aos moderadores e administradores do servidor!`')
.setThumbnail('https://images-ext-2.discordapp.net/external/S8KRhIAgTc9g6pXD2VzfnpOrd2243PA_Tv1M6wLhF30/https/images.emojiterra.com/mozilla/512px/274c.png?width=441&height=441')
const Embed1 = new Discord.MessageEmbed()
.setColor('#ff0000')
.setDescription('' +denuncia)
.setTitle('Conteúdo da Denuncia:')
.setFooter('✅- Usuário punido | - Em averiguação | ❌- Usuário Não Punido')
.setAuthor (`Autor da denuncia: ${msg.author.username}`)

if(denuncia < 1){
  msg.channel.send('Você precisa escrever o autor e o motivo da denuncia após o comando!')
 }
 else {

     msg.reply(Embed)
     canalDenuncia.send(Embed1).then(messageReaction => {
messageReaction.react('✅')
messageReaction.react(':')
messageReaction.react('❌')
     })
   }
}

I need to access the variable denunciationReceived in the other if to report, but it is not a global variable as I do?

1 answer

0

A global variable that has global scope : all scripts and functions on a web page can access it. And even if you are inside or outside the function, where you prefer to put it, you assign a value to the data variable but do not declare it (statements: var Let const) staying only reports)

If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable and so all scripts and functions on a web page can access it .

REMEMBERING CAUTION WHEN DOING THIS BECAUSE NO OTHER VARIABLE CAN GET THE NAME OF A GLOBAL VARIABLE, IN THE CASE I MENTIONED THERE THE VARIABLE DATA AND SOME EXAMPLES OF YOUR CASE ARE THE VARIABLES LINES AND BTNVUALIZE AND TABLE YOU NAMED OUT OF FUNCTIONS.

Instead of:

    var denunciasRecebidas = msg.content.replace('z!setarcanaldenunciasrecebidas','')

Place

    denunciasRecebidas = msg.content.replace('z!setarcanaldenunciasrecebidas','')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.