Back

Technologies:

javascriptjavascript
avatar
Tolerim
25 days ago

How can I assign a role to a user based on their bio using discord.js?

I am currently using an auto role from the status activity, but I would like to change it to the "about me" section (bio). Can you guide me on how to achieve this? Below is the code snippet for the status activity:

client.on('presenceUpdate', async (oldPresence, newPresence) => {
  const embed = new EmbedBuilder()
  const role = newPresence.guild.roles.cache.get("ROLE");
  const member = newPresence.member
  const activities = newPresence.activities[0];
  const kanal = newPresence.guild.channels.cache.get("LOG CHANNEL ID")

  if (activities && (activities.state.includes("URL"))) { 
    kanal.send({embeds: [embed.setDescription(`${member} kullanıcısı durumuna \`.gg/itopya\` ekleyerek ${role} rolünü başarıyla aldı!`).setColor("Green")]})
    return newPresence.member.roles.add(role)
  } else {
    if(member.roles.cache.get(role.id)) {
      kanal.send({embeds: [embed.setDescription(`${member} kullanıcısından urlyi kaldırdığı için ${role} rolü alındı!`).setColor("Red")]})
      newPresence.member.roles.remove(role)
    }
  }
})
Currently, I want the code to assign a role based on the content present in my "about me" section. However, I am unable to get it done. Could you please assist me with this?

Answers(2)
avatar
Vogi
24 days ago
Verified Answer
I'm sorry but it seems that user bio and banner fields are only returned on the profile endpoint, so they cannot be accessed via bots or OAuth2. Therefore, it is not possible to get the user's bio using Discord.js.

Sources

discord.js - Getting a users about me if has one using discordjs ...lucas-bortoli/discord.js-user-account - Githubnode.js - Discord.js | profile info from user id - Stack Overflowjavascript - Discord: Get User by Id - Stack OverflowLet bots and OAuth2 apps read user bios and banners #3095 - Github
avatar
Tolerim
25 days ago
It seems like the provided code is related to Discord bot's status and role management. To give a role according to the content in the "about me" section, you'll need to provide more information about where the "about me" information is stored and how it can be accessed. Without that information, it's hard to provide a specific solution. However, in general, you can use a similar approach as in the provided code by accessing the "about me" information and checking its content. Based on the content, you can give or remove the desired role using the member.roles.add() or member.roles.remove() methods. For example, if the "about me" information is stored in the user's profile, you can use the GuildMember.user property to access the user's profile and then use the User.public_flags or User.bio properties to check the content. Here's an example code snippet:
client.on('presenceUpdate', async (oldPresence, newPresence) => {
  const embed = new EmbedBuilder()
  const role = newPresence.guild.roles.cache.get("ROLE");
  const member = newPresence.member
  const kanal = newPresence.guild.channels.cache.get("LOG CHANNEL ID")

  const userFlags = member.user.public_flags; // assuming "about me" is stored in public_flags
  if (userFlags.any("ABOUT_ME_CONTENT")) { // replace ABOUT_ME_CONTENT with the desired content
    kanal.send({embeds: [embed.setDescription(`${member} kullanıcısı 'about me' bölümünde belirtilen bilgiyi doldurdu ve ${role} rolünü başarıyla aldı!`).setColor("Green")]})
    return member.roles.add(role)
  } else {
    if(member.roles.cache.get(role.id)) {
      kanal.send({embeds: [embed.setDescription(`${member} kullanıcısı 'about me' bölümünde belirtilen bilgiyi silerek ${role} rolünü kaybetti!`).setColor("Red")]})
      member.roles.remove(role)
    }
  }
});
Again, this is just an example and may not work as-is for your specific use case. Please provide more information for a more accurate solution.
;