I want my bot to give a member a role if they have the servers invite in their status. Does anyone know how I could do that? maybe you could fetch the members info or something? I tried searching on google but I found nothing. Thank you so much:)
-
2What you'll need to check is `
.presence.activities`. Try checking the docs for that [here](https://discord.js.org/#/docs/main/stable/class/Presence?scrollTo=activities) and make some code before asking here. – Squiddleton Jul 03 '21 at 18:05
1 Answers
Getting the custom status
As Squiddleton said, you can use the .activities
property of Presence
to get their a user's custom status. This property is an array of Activity
s and you can get the custom status by finding the activity whose .type
is CUSTOM_STATUS
.
The activity of a custom status looks something like this:
Activity {
name: 'Custom Status',
type: 'CUSTOM_STATUS',
url: null,
details: null,
state: 'this is the status message',
applicationID: null,
timestamps: null,
party: null,
assets: null,
syncID: undefined,
flags: ActivityFlags { bitfield: 0 },
emoji: null,
createdTimestamp: 1625478958735
}
As you can see, the status message is stored in the state
property.
/**
* The custom status message of `member`,
* or `undefined` if the member does not have a custom status.
* @type {string | undefined}
*/
const customStatus = member.presence.activites
.find(activity => activity.type === 'CUSTOM_STATUS')
?.state
Checking if the status includes the invite
You can use the String.prototype.includes
method to test if a string contains another string:
const inviteLink = 'https://discord.gg/blahblah'
if (customStatus) {
/**
* Whether `customStatus` has the invite link `inviteLink`.
* @type {boolean}
*/
const hasInviteLink = customStatus.includes(inviteLink)
}
If you wanted, you could take this a step further and test if the custom status contains any invite from the server using a combination of Guild#fetchInvites
and the inviteCreate
client event.
Adding the role
Now all you need to do is add the role for that member:
if (hasInviteLink) {
member.roles.add(theRoleYouWantToAdd)
// Don't forget to handle errors!
.catch(console.error)
}
theRoleYouWantToAdd
can be a Role
or the ID of the role.
Where do I put this code?
Discord.js has a presenceUpdate
event, which fires when a member's presence (such as their custom status) updates. Note that you need to enable the GUILD_PRESENCES
intent to receive this event (see this answer for more information).
The final code might look something like this:
const roleID = // ...
const inviteLink = // ...
client.on('presenceUpdate', (_oldPresence, newPresence) => {
const member = newPresence.member
if (member) {
// Ignore members who already have the role
if (!member.roles.cache.has(roleID)) {
const customStatus = newPresence.activites
.find(activity => activity.type === 'CUSTOM_STATUS')
?.state
if (customStatus) {
if (customStatus.includes(inviteLink)) {
member.roles.add(roleID)
.catch(console.error)
}
}
}
}
})

- 12,700
- 2
- 32
- 59
-
wow thank you so much. I didnt need it all coded for me but still thank you very much:) – Jan Jul 05 '21 at 16:57
-
-
@Jan Hmm, according to the docs `member.presence.activities` should never be `undefined`. Where is this error occurring? – Lauren Yim Jul 06 '21 at 02:52