I have been working on making a custom bot for discord and have recently run into a weird issue.
The command I am making is an option for users to create their own custom roles. They will go through a process to enter their custom role name, color, and image/icon.
The problem happens with the image/icon, I have created a check to see if the image/icon URL is null, "undefined" or undefined. But for some reason even if the image/icon URL is not null/"undefined"/undefined it still executes the code if it is.
Here’s the code:
collector4?.on('collect', async m => {
var IconURL;
const memberNew = guild!.members.fetch(i.user.id);
const CustomRoleIcon = m.attachments.forEach(attachment => { IconURL = attachment.url })
const CustomRoleIconSKIP = m.content
if (CustomRoleIconSKIP.includes("SKIP")) { // This part is not important, jump to the "else" statement
// <non important code here>
} else {
const CustomRoleSelect4 = new MessageEmbed()
.setColor("PURPLE")
.setThumbnail("https://cdn.discordapp.com/attachments/963152321926287440/963925181120782446/00f6bd63-da56-4a82-9e22-8483e3069a28.png")
.setTitle(`The Reputation Shop | Custom Role (Buy)`)
.setDescription(`Name: **${CustomRoleName}**\nColor: ***Has to be set by Moderator due to Code nature***\nIcon: **${IconURL}**\nPrice: **20 Reputation**\n***Proceed?***`)
.setFooter({
text: `${i.user.tag} | Developed by Inconvenient.#1234`,
iconURL: `${i.user.avatarURL() || "https://cdn.discordapp.com/attachments/963152321926287440/963925181120782446/00f6bd63-da56-4a82-9e22-8483e3069a28.png"}`
})
await i.editReply({
embeds: [CustomRoleSelect4],
components: [CancelConfirmForNonSkip],
})
FullRoleData = new Map([
["Name", `${CustomRoleName}`],
["Color", `${CustomRoleColor.toUpperCase()}`],
["URL", `${IconURL}`], // when using console.log(IconURL) it will print the URL as expected.
])
}
})
======== Second Part of the Code: ========
try {
if (FullRoleData.get("URL") === null || "undefined" || undefined) { // null/undefined check
i.channel?.send(`${"<@" + i.user.id + ">"}, Custom Role purchase failed! \`\`"Invalid Icon URL: Forgot to attach image or mispelled SKIP?"\`\``)
await i.deleteReply()
return
}
The problem is that the code above has been triggered, even when using console.log(FullRoleData.get("URL")) it prints an URL Link, what exactly is happening?
It was also working fine yesterday but today it broke for some strange reason.
If you want the full code It can be found here:
I hope I have explained it well.
>Solution :
if (FullRoleData.get("URL") === null || "undefined" || undefined)
Here you are only checking if the URL of FullRoleData is null. The undefined conditions are not part of the comparison and if you go to the console and do Boolean("undefined") you will get true.
So the result would be if (false || true || false), and of course the code inside will be executed.
If you want to compare a variable to many variables, a common thing to do is if ([null, "undefined", undefined].includes(FullRoleData.get("URL))
In this particular case, I’m not sure why you are checking if it is equal to "undefined", usually if you want to know if a variable is null or undefined and the variable won’t be a boolean a simple if (!FullRoleData.get("URL")) would be enough