'Discord, Mention someone when gets a role

Is there a way to mention someone immediately after getting a specific role in a specific channel? For example when I, or any other admin, give someone a specific role the bot mentions them in a specific channel.

Here is my code:

client.on("guildMemberUpdate", (oldMember, newMember) => {
    const channel = client.channels.cache.get("channelid");

    // If the role(s) are present on the old member object but no longer on the new one (i.e role(s) were removed)
    const removedRoles = oldMember.roles.cache.filter((role) => !newMember.roles.cache.has("roleid"));
    if (removedRoles.size > 0) console.log(`The roles ${removedRoles.map((r) => r.name)} were removed from ${oldMember.displayName}.`);
    // If the role(s) are present on the new member object but are not on the old one (i.e role(s) were added)
    const addedRoles = newMember.roles.cache.filter((role) => !oldMember.roles.cache.has("roleid"));
    if (addedRoles.size > 0) {
        if (newMember.roles.cache.some((role) => role.name === "testing")) {
            let embed = new Discord.MessageEmbed().setTitle("♡﹕welcome!").setDescription("lalalala").setColor("#FFB6C1").setThumbnail("https://cdn.discordapp.com/attachments/806974794461216813/817737054745526304/giffy_2.gif");
            channel.send(`Welcome ${oldMember.user}`, embed);
        }
        console.log(`The roles ${addedRoles.map((r) => r.name)} were added to ${oldMember.displayName}.`);
    }
});


Solution 1:[1]

This is assuming your code is exactly as shown in the question, and not using placeholder strings.

Firstly, "roleid" should be role.id without the "" when you're filtering. This will filter it properly, as should channelId where channel is defined, which I'm guessing are placeholders, but just in case.

Secondly, you'll probably want to check using addedRoles.has("roleIdToSend") as opposed to newMember.roles.cache.some(). This will DM them every time they're updated if they have the role, not if they only just got the role on that update.

Lastly, assuming you're using discord.js v13, you'll need to correct your send() method. It should look something like

channel.send({
  content: `Welcome, ${oldMember.toString()}`,
  embeds: [embed]
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iiRealistic_Dev