1

I have been making a Discord.JS bot and I have an issue with my !userinfo command. There are quite a few issues I am facing but all of them because I don't know how to handle Promises in DiscordJS correctly. No site has been helping me. I have included comments under the lines giving me issues.

            const memberemb = message.guild.members.fetch(user);
        Promise.resolve(memberemb).then(function () {
            console.log(memberemb.roles);
            const embed = new Discord.MessageEmbed()
                .setColor("RANDOM")
                .setThumbnail(message.author.avatarURL)
                .addField(`${user.tag}`, `${user}`, true)
                .addField("ID:", `${user.id}`, true)
                .addField("Nickname:", `${useralso.nickname ? `${useralso.nickname}` : 'None'}`, true)
                .addField("Status:", `${user.presence.status}`, true)
                // always shows offline in status
                .addField("Game:", `${user.presence.game ? user.presence.game.name : 'None'}`, true)
                //idk if it shows games correctly
                .addField("Joined The Server On:", `${moment.utc(memberemb.joinedAt).format("dddd, MMMM Do YYYY")}`, true)
                //shows date of today not of the joined on date
                .addField("Account Created On:", `${moment.utc(user.createdAt).format("dddd, MMMM Do YYYY")}`, true)
                .addField("Roles:", memberemb.roles ? memberemb.roles.map(roles => `${roles}`).join(', ') : "None", true)
                //shows none as roles
                .setFooter(`Replying to ${message.author.username}#${message.author.discriminator}`)
            message.channel.send({ embed });
        });
        break;

Please explain how to fix this and also rewrite the code correctly(please) so I can use it for my bot
Thank you

iammithani
  • 108
  • 2
  • 10
  • Do you have [privileged intents](https://stackoverflow.com/questions/64559390/none-of-my-discord-js-guildmember-events-are-emitting-my-user-caches-are-basica) enabled? – Lioness100 Nov 20 '20 at 12:33
  • yes i switched it on. now everything works correctly but @everyone is shown in the roles too idk why – iammithani Nov 20 '20 at 15:25

1 Answers1

0

For your issues with presence check out this or this other post. Game will probably be wrong without fixing the that first.

With moment.js, you need to do moment(currenttime) before changing the time format.

.addField("Joined The Server On:", `${moment(user.joinedAt).format('DD-MM-YYYY')}`, true)
.addField("Account Created On:", `${moment(user.createdAt).format('DD-MM-YYYY')}`, true)

that should works. (See more in moment.js documentation)

Regarding to your roles, memberemb.roles is undefined.

you will want to get the value from the promise, I put info inside the function to do that

Promise.resolve(memberemb).then(function (info) {

then within the embed, you can use the info.roles.caches.map

.addField("Roles:", info.roles.cache ? info.roles.cache.map(roles => `${roles}`).join(', ') : "None", true)

See more on how promise works here

Bulaxy
  • 111
  • 8
  • Thank you it woked but, it shows @everyone in the roles too which I don't wanna show Please tell the fix – iammithani Nov 20 '20 at 13:36
  • @MuhammadUsmanMithani use this instead, which filter when role is @everyone `info.roles.cache.filter(role=> role.name !=='@everyone').map(roles => `${roles}`).join(', ')` – Bulaxy Nov 20 '20 at 20:30