0

I'm trying to make it so that the output of this Discord embed won't be "undefined" for the Bot, Mute, and Deafen part of the embed.

I tried to change some var to "let" or "const" I've tampered with the aboutuser portion to change it to something different. I've messed with the if portion of the code.

Here's the code.

async run(message, args){
    if (message.channel instanceof discord.DMChannel) return message.channel.send('This command cannot be executed here.')
    else
    var serv = message.guild

    if (serv.explicitContentFilter == 0) {
        var eFC = "Don't Scan Any messages";
    }
    if (serv.explicitContentFilter == 1) {
        var eFC = "Scan for users without a role.";
    }
    if (serv.explicitContentFilter == 2) {
        var eFC = "Scan every message";
    }
///////////////////////////////////////////////////////////////////////////////////////////////////
    if (serv.verificationLevel == 4) {
        var verL = "Intense (Verified Account & Verified Phone linked)";
    } 

    if (serv.verificationLevel == 3) {
        var verL = "Secure (Verified Account & Guild member for 10+ minutes)";
    } 
    if (serv.verificationLevel == 2) {
        var verL = "Medium (Verified Account for 5 minutes+)";
    }    
    if (serv.verificationLevel == 1) {
        var verL = "Low (Verified Account)";
    } 
    if (serv.verificationLevel == 0) {
        var verL = "None (No Restriction)";
    }  
//////////////

if (serv.region == `brazil`) {
    var regio = "Brazil";
}  

if (serv.region == `eu-central`) {
    var regio = "Central Europe";
}  

if (serv.region == `hongkong`) {
    var regio = "Hong Kong";
}  

if (serv.region == `japan`) {
    var regio = "Japan";
}  

if (serv.region == `russia`) {
    var regio = "Russia";
}  

if (serv.region == `singapore`) {
    var regio = "Singapore";
}  

if (serv.region == `southafrica`) {
    var regio = "South Africa";
}  

if (serv.region == `sydney`) {
    var regio = "Sydney";
} 

if (serv.region == `us-central`) {
    var regio = "Central US";
}  

if (serv.region == `us-east`) {
    var regio = "East US";
}  

if (serv.region == `us-south`) {
    var regio = "South US";
}  

if (serv.region == `us-west`) {
    var regio = "West US";
}  

if (serv.region == `eu-west`) {
    var regio = "West Europe";
}  
//
if (serv.defaultMessageNotifications == `ALL`) {
    var defn = "Send all Messages";
}  

if (serv.defaultMessageNotifications == `MENTIONS`) {
    var defn = "Only @everyone";
} 

    var myInfo = new discord.RichEmbed()
        .setAuthor(`${serv.name}'s guild info`,`${message.guild.iconURL}`)
        .addField(`AFK Channel`,`${serv.afkChannel}`,true)
        .addField(`AFK Timeout`,`${serv.afkTimeout}s`,true)
        .addField(`Channels`,`${serv.channels.size}`,true)
        .addField(`Creation of Guild`,`${serv.createdAt}`,true)
        .addField(`Default Notification`, defn,true)
        .addField(`Explicit Content Filter Level`, eFC,true)
        .addField(`Guild ID`,`${serv.id}`,true)
        .addField(`How much members`,`${serv.memberCount}`,true)
        .addField(`Owner`,`${serv.owner}`,true)
        .addField(`Region`, regio,true)
        .addField('Roles', `Please do s!roles to find server roles!`, true)
        /* serv.roles.map(r => `${r}`).join(' | ') */
        .addField(`Verification Level`, verL,true)
        .setColor(0x511fdd)
        .setFooter('Aboutserver command')
        .setThumbnail(`${message.guild.iconURL}`)
        message.channel.sendEmbed(myInfo);

}

}

expected result : The bot will say Yes or No instead of undefined, or true or false. actual result : The bot's output is just undefined.

SomePerson
  • 1,171
  • 4
  • 16
  • 45

1 Answers1

1

There are a couple of things happening here, but let's focus on the main issue; how you have declared your variables.

To put it simply, variables can only be accessed within the scope in which they are declared in (The scope is all the code between the {}).

I'll explain it with a short example based on your code. In your if statements you declare your variables, meaning they can be used within that if statements' scope. You later want to use those same variables outside of the if statement and in your embed. Because those variables don't exist in that scope, they are undefined.

...
// At this point there is no variable 'eFC' available.

if (serv.explicitContentFilter == 0) {
    // Here you create the variable 'eFC' but it can only be used inside this scope, meaning it cannot be accessed outside the 'if' statement.
    var eFC = "Don't Scan Any messages";
}
if (serv.explicitContentFilter == 1) {
    // Here you create another variable with the same name, but it would end up being a different variable.
    var eFC = "Scan for users without a role.";
}

// Here there is still no variable 'eFC' available to us.
...

The simple solution is: declare your variables in another scope and assign the values later. Below you can see an example:

...
// Here we create a new variable called 'eFC' which can be used within this scope
var eFC;

if (serv.explicitContentFilter == 0) {
    // Here we assign a value to the previously defined variable
    eFC = "Don't Scan Any messages";
}
if (serv.explicitContentFilter == 1) {
    // Here we assign a value to the previously defined variable
    eFC = "Scan for users without a role.";
}

// Here we can use the variable 'eFC' which will have a value
console.log(eFC);
...

If you do this for all the variables which you use, the code should work fine.

Lastly I want to leave you with some extra help. I see you have created many, many, many if statements to check for e.g. the server region or the server verification level. Javascript (among many other programming languages) has a think called a switch case which basically does the same thing you have with all those if statements, but in a more neater way. Check out the link, I think it will help you make your code look a bit more readable

T. Dirks
  • 3,566
  • 1
  • 19
  • 34