Home > database >  Discord.JS 13 slash command display a targets users specific user role in and embed,
Discord.JS 13 slash command display a targets users specific user role in and embed,

Time:10-04

I am trying to make a slash command that displays a users specific roles when the command is issued.

run: async (client, interaction) => {
try{
    const { member, channelId, guildId, applicationId, 
            commandName, deferred, replied, ephemeral, 
            options, id, createdTimestamp , roles, cache
    } = interaction; 
    const { guild } = member;
    let UserOption = options.getUser("which_user");
    if(!UserOption) UserOption = member.user;
    try{
        await guild.members.fetch();
        const member = guild.members.cache.get(UserOption.id);
        const roles = member.roles;
        const userFlags = UserOption.flags.toArray();
        const activity = UserOption.presence?.activities[0];
        //create the EMBED
        const embeduserinfo = new MessageEmbed()
        embeduserinfo.setThumbnail(member.user.displayAvatarURL({ dynamic: true, size: 512 }))
        embeduserinfo.setAuthor("Information about:   "   member.user.username   "#"   member.user.discriminator, member.user.displayAvatarURL({ dynamic: true }), "https://discord.gg/FQGXbypRf8")
        embeduserinfo.addField('**❱ Username:**',`<@${member.user.id}>\n\`${member.user.tag}\``,true)
        //embeduserinfo.addField('**❱ ID:**',`\`${member.id}\``,true)
        embeduserinfo.addField('**❱ Avatar:**',`[\`Link to avatar\`](${member.user.displayAvatarURL({ format: "png" })})`,true)
        embeduserinfo.addField('**❱ Joined Discord:**', "\`" moment(member.user.createdTimestamp).format("DD/MM/YYYY")   "\`\n"   "`"  moment(member.user.createdTimestamp).format("hh:mm:ss")   "\`",true)
        embeduserinfo.addField('**❱ Joined MetroVan:**', "\`" moment(member.joinedTimestamp).format("DD/MM/YYYY")   "\`\n"   "`"  moment(member.joinedTimestamp).format("hh:mm:ss")  "\`",true)
        //embeduserinfo.addField(`❱ [${roles.cache.size}] Roles: `, roles.cache.size < 25 ? Array.from(roles.cache.values()).sort((a, b) => b.rawPosition - a.rawPosition).map(role => `<@&${role.id}>`).join(', ') : roles.cache.size > 25 ? trimArray(roles.cache) : 'None')
        if (roles.cache.find(r => r.id === "893330818624282656"))
        {
            embeduserinfo.addField("test")
        }
        embeduserinfo.setColor(ee.color)
        embeduserinfo.setFooter(ee.footertext, ee.footericon)
        //send the EMBED
        interaction.reply({embeds: [embeduserinfo], ephemeral: false})
      }catch (e){
        console.log(e)
      }
} catch (e) {
    console.log(String(e.stack).bgRed)
}

} }

if targeted member has role 893330818624282656 display this embed.addField

        if (roles.cache.find(r => r.id === "893330818624282656"))
    {
        embeduserinfo.addField("test")
    }

Step by step goal

  • User 1 uses slash command to view users 2 profile

  • User 1 executes slash command /profile @user2

  • An embedd is sent to the channel the command is executed in with ephemeral: true (changed in code once programming is completed)

  • User 2 will have one of three roles.

  • 123456789 / 123456788 / 123456787

  • DMS OPEN / DMS ASK / DMS CLOSED

Embed will display one of the 3 roles as

  • DMS ARE OPEN
  • YOU MUST ASKED TO DM IN #CHANNEL
  • DMS ARE CLOSED

CodePudding user response:

This is the problem

embeduserinfo.addField("test")

test is the name of the field but there is no value

If you want to add an empty value that won’t throw an error, do this:

embeduserinfo.addField("test", "** **")

Or if you want to make it an empty name, just reverse the parameters. Otherwise you should just make it the value it should have.

  • Related