我正在尝试使该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);
}
}
预期结果:机器人将说是或否,而不是未定义,或者是或否。 实际结果:该机器人的输出只是不确定的。
答案 0 :(得分: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(在许多其他编程语言中)有一个名为switch case
的思想,该思想基本上与所有if
语句具有相同的作用,但方式更简洁。查看链接,我认为它将帮助您使代码看起来更具可读性