0

Here is my code:

bot.on('message', async message => {

  function httpGet(url, callback) {
    // this function gets the contents of the URL. Once the
    // content is present it runs the callback function.
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            callback(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", url, false );
    xmlhttp.send();    
}

httpGet("http://www.bannedwordlist.com/lists/swearWords.txt", function(textFile){
    // this calls the httpGet function with the URL of your text
    // file. It then runs a function that turns the file into an
    // array.
    let blacklisted = textFile.slice(1, -1).split("],[");
});


  //2 looking for words
  let foundInText = false;
  for (var i in blacklisted) { // loops through the blacklisted list
    if (message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
  }
  // checks casesensitive words

  //3 deletes and send message
    if (foundInText) {
      message.delete();
      message.channel.sendMessage("Hey! Please don't use bad words on our server!")
  }
});

Node gives the error 'blacklisted is not defined', when I define it in the code (let blacklisted =... etc.) If I defined it, why is it saying it isn't.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
  • blacklisted is declared inside the httpGet function. Declare blacklisted outside of it and you should be fine. The reason being, since you declared it inside the function, its scope is only inside that function and does not exist outside of it. – Omi in a hellcat Nov 14 '19 at 17:03
  • [Why is using “for…in” with array iteration a bad idea?](https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea) – Andreas Nov 14 '19 at 17:09

2 Answers2

2

You defined blacklisted in the scope of another function, which isn't accessible outside of it. You need to define it as a global var if you want to access it in other functions:

let blacklisted = []; 
httpGet("http://www.bannedwordlist.com/lists/swearWords.txt", function(textFile){
    // this calls the httpGet function with the URL of your text
    // file. It then runs a function that turns the file into an
    // array.
    blacklisted = textFile.slice(1, -1).split("],[");
});
Clarity
  • 10,730
  • 2
  • 25
  • 35
  • Thanks for the help. I get no not defined errors now, but when I use one of the words in the text file on my discord server, the bot doesn't remove them or send a message. There are no errors. I understand that I may just have to use console.log to find my error, but I was wondering if you could spot what was wrong. I can make a new question if need be, as some people don't know discord.js and stuff, but it would be great if you could help. Thanks, Eddie – Eddie's Tech Nov 14 '19 at 17:16
  • Probably better to make a new questions since I'm not too familiar with discord API myself. – Clarity Nov 14 '19 at 17:19
-1

Try doing your let outside of the httpGet.

mediaguru
  • 1,807
  • 18
  • 24