Vanity

How do fix my code to work, it only checks members who have joined instead of all members:
16 Replies
d.js toolkit
d.js toolkit3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
Sincerely, alxx ᴬᴳ
const { Client, GatewayIntentBits, Events, EmbedBuilder } = require('discord.js');

// Create a new client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences, // Required to track status updates
],
});

// Configuration
const trackedRoleId = '1282481871900508262'; // Replace with the actual role ID
const triggerPhrase = '.gg/sabrina';
const notificationChannelId = '1281981654310719610'; // Replace with the ID of the notification channel

async function setupVanity(client) {
client.on(Events.ClientReady, () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on(Events.PresenceUpdate, async (oldPresence, newPresence) => {
try {
if (!newPresence.member) return;

// Log the user's tag
console.log(`Tracking status for user: ${newPresence.user.tag}`);

// Extract the old and new statuses
const oldStatus = oldPresence?.activities?.find((act) => act.state)?.state || '';
const newStatus = newPresence?.activities?.find((act) => act.state)?.state || '';

// Log extracted statuses
console.log('Old Status:', oldStatus);
console.log('New Status:', newStatus);

// If the status hasn't changed, exit
if (oldStatus === newStatus) {
console.log('Status has not changed, ignoring update.');
return;
}

// Check if the new status contains the exact trigger phrase
const isValidStatus = newStatus
.split(' ')
.some((word) => word === triggerPhrase);

console.log(`IsValidStatus: ${isValidStatus}`);

const member = newPresence.member;
const role = newPresence.guild.roles.cache.get(trackedRoleId);
const notificationChannel = newPresence.guild.channels.cache.get(notificationChannelId);
const { Client, GatewayIntentBits, Events, EmbedBuilder } = require('discord.js');

// Create a new client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences, // Required to track status updates
],
});

// Configuration
const trackedRoleId = '1282481871900508262'; // Replace with the actual role ID
const triggerPhrase = '.gg/sabrina';
const notificationChannelId = '1281981654310719610'; // Replace with the ID of the notification channel

async function setupVanity(client) {
client.on(Events.ClientReady, () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on(Events.PresenceUpdate, async (oldPresence, newPresence) => {
try {
if (!newPresence.member) return;

// Log the user's tag
console.log(`Tracking status for user: ${newPresence.user.tag}`);

// Extract the old and new statuses
const oldStatus = oldPresence?.activities?.find((act) => act.state)?.state || '';
const newStatus = newPresence?.activities?.find((act) => act.state)?.state || '';

// Log extracted statuses
console.log('Old Status:', oldStatus);
console.log('New Status:', newStatus);

// If the status hasn't changed, exit
if (oldStatus === newStatus) {
console.log('Status has not changed, ignoring update.');
return;
}

// Check if the new status contains the exact trigger phrase
const isValidStatus = newStatus
.split(' ')
.some((word) => word === triggerPhrase);

console.log(`IsValidStatus: ${isValidStatus}`);

const member = newPresence.member;
const role = newPresence.guild.roles.cache.get(trackedRoleId);
const notificationChannel = newPresence.guild.channels.cache.get(notificationChannelId);
if (isValidStatus) {
// Check if the user already has the role
if (!member.roles.cache.has(trackedRoleId)) {
console.log(`Assigning role ${trackedRoleId} to user ${member.user.tag}`);
// Assign the role to the user
await member.roles.add(trackedRoleId);

// Send a congratulatory message in the notification channel
if (notificationChannel) {
const embed = new EmbedBuilder()
.setTitle(`${member.user.username} Vanity`)
.setDescription(
`:AH_AestheticArrow: Thank you <@${member.id}> for putting \`.gg/sabrina\` in your status!
:AH_AestheticArrow: You have *received* our <@&${trackedRoleId}>!`
)
.setTimestamp()
.setColor(0xe553a6);
await notificationChannel.send({ embeds: [embed] });
}
} else {
console.log(`User ${member.user.tag} already has the role.`);
}
} else if (member.roles.cache.has(trackedRoleId)) {
// If the status no longer contains the phrase, remove the role
console.log(`Removing role ${trackedRoleId} from user ${member.user.tag}`);
await member.roles.remove(trackedRoleId);

if (notificationChannel) {
await notificationChannel.send(
`⚠️ <@${member.id}>, "${triggerPhrase}" was removed from your status, so the <@&${trackedRoleId}> role has been removed.`
);
}
} else {
console.log(`No action needed for user ${member.user.tag}`);
}
} catch (error) {
console.error('Error processing vanity status update:', error);
}
});
}

// Call the function to initialize
module.exports = { setupVanity };
if (isValidStatus) {
// Check if the user already has the role
if (!member.roles.cache.has(trackedRoleId)) {
console.log(`Assigning role ${trackedRoleId} to user ${member.user.tag}`);
// Assign the role to the user
await member.roles.add(trackedRoleId);

// Send a congratulatory message in the notification channel
if (notificationChannel) {
const embed = new EmbedBuilder()
.setTitle(`${member.user.username} Vanity`)
.setDescription(
`:AH_AestheticArrow: Thank you <@${member.id}> for putting \`.gg/sabrina\` in your status!
:AH_AestheticArrow: You have *received* our <@&${trackedRoleId}>!`
)
.setTimestamp()
.setColor(0xe553a6);
await notificationChannel.send({ embeds: [embed] });
}
} else {
console.log(`User ${member.user.tag} already has the role.`);
}
} else if (member.roles.cache.has(trackedRoleId)) {
// If the status no longer contains the phrase, remove the role
console.log(`Removing role ${trackedRoleId} from user ${member.user.tag}`);
await member.roles.remove(trackedRoleId);

if (notificationChannel) {
await notificationChannel.send(
`⚠️ <@${member.id}>, "${triggerPhrase}" was removed from your status, so the <@&${trackedRoleId}> role has been removed.`
);
}
} else {
console.log(`No action needed for user ${member.user.tag}`);
}
} catch (error) {
console.error('Error processing vanity status update:', error);
}
});
}

// Call the function to initialize
module.exports = { setupVanity };
Split in two because it was to long
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Sincerely, alxx ᴬᴳ
Look at my message at the top?
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/luna3mo ago
'joined members' isnt really clear all members in a guild have joined the guild
Sincerely, alxx ᴬᴳ
Like when a new member joins, it checks theirs tatus. Instead of checking everyone in the guild all the time.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/luna3mo ago
this code just checks whenever someone updates their presence idk if that emits upon join but you are never doing any checks across the guild
Sincerely, alxx ᴬᴳ
Well all the logging shows it only checks when a new member joins Oh?
treble/luna
treble/luna3mo ago
well yeah, your code only checks the member that the presence update event emitted for i dont see why you would think it would check the entire guild without any code to do so the comments also make it look like ai code
Sincerely, alxx ᴬᴳ
Well I'll fix that thank you I had a friend help me
treble/luna
treble/luna3mo ago
then ask your friend for support or send them here
Sincerely, alxx ᴬᴳ
Well they sent me here because they were to busy I don't really know how to fix that part
treble/luna
treble/luna3mo ago
then ask your friend, we arent 3rd party support, if you dont know how the code even works its impossible to explain
Sincerely, alxx ᴬᴳ
Thanks for the help :|

Did you find this page helpful?