0

我正在努力做到这一点,以便这个 Discord 嵌入的输出对于嵌入的一部分不会是“未定义的” Bot, Mute, and Deafen

我试图将一些 var 更改为“let”或“const”,我篡改了该aboutuser部分以将其更改为不同的东西。我弄乱if了代码的一部分。

这是代码。

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

}

}

预期结果:机器人会说是或否,而不是未定义、真或假。实际结果:机器人的输出只是未定义的。

4

1 回答 1

1

这里发生了几件事,但让我们关注主要问题;你是如何声明你的变量的。

简单来说,变量只能在声明它们的作用域内被访问(作用域就是 之间的所有代码{})。

我将根据你的代码用一个简短的例子来解释它。在您的if语句中声明您的变量,这意味着它们可以在该if语句的范围内使用。您稍后希望在if语句之外和嵌入中使用这些相同的变量。因为这些变量在该范围内不存在,所以它们是未定义的。

...
// 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.
...

简单的解决方案是:在另一个范围内声明您的变量并稍后分配值。您可以在下面看到一个示例:

...
// 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语句来检查例如服务器区域或服务器验证级别。Javascript(在许多其他编程语言中)有一种叫做 a 的思想switch case,它基本上与所有这些语句做同样的事情if,但以一种更简洁的方式。查看链接,我认为它会帮助您使您的代码看起来更具可读性

于 2019-06-08T09:32:10.457 回答