need help with this code

Hey, I’m tryna make a bot that checks for a specific keyword within someone’s custom status and if they have the keyword, it gives them a role, and if they don’t, it removes the role. The code is semi working, it’s logging in the console every time someone changes there status but isn’t logging when it is the correct status. and removes the role even if it’s the wrong or right status.
19 Replies
d.js toolkit
d.js toolkit2mo ago
- What are your intents? GuildVoiceStates is required to receive voice data! - Show what dependencies you are using -- generateDependencyReport() is exported from @discordjs/voice. - Try looking at common examples: https://github.com/discordjs/voice-examples. - 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!
gblazzy
gblazzyOP2mo ago
const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences, // Needed to track user presence
],
});

// Define the target custom status and role ID
const TARGET_CUSTOM_STATUS = '.gg/dallaspd'; // The custom status you want to check for
const ROLE_ID = '1287515637714911322'; // The ID of the role you want to assign

client.on('ready', () => {
console.log(`${client.user.username} is online!`);
});

client.on('presenceUpdate', async (oldPresence, newPresence) => {
const member = newPresence.member;

if (!member) return; // Ensure this is a guild member

// Log user's status change
console.log(`${member.user.tag} changed status:`);

// Check activities for the target custom status
const activities = newPresence.activities;
const hasTargetStatus = activities.some(activity =>
activity.type === 'CUSTOM' && activity.name === TARGET_CUSTOM_STATUS
);

// Log current activities
activities.forEach(activity => {
console.log(`- ${activity.type}: ${activity.name}`);
});

// Find the role by ID
const role = member.guild.roles.cache.get(ROLE_ID);

// If the user has the target custom status
if (hasTargetStatus) {
console.log(`${member.user.tag} has the target custom status: "${TARGET_CUSTOM_STATUS}".`);

// Check if the user already has the role
if (role) {
if (!member.roles.cache.has(role.id)) {
try {
await member.roles.add(role);
console.log(`Assigned role "${role.name}" to ${member.user.tag}.`);
} catch (error) {
console.error(`Failed to add role: ${error}`);
}
} else {
console.log(`${member.user.tag} already has the role "${role.name}".`);
}
} else {
console.error(`Role with ID ${ROLE_ID} not found.`);
}
} else {
// If the user doesn't have the target status, check if they have the role and remove it
if (role && member.roles.cache.has(role.id)) {
try {
await member.roles.remove(role);
console.log(`Removed role "${role.name}" from ${member.user.tag}.`);
} catch (error) {
console.error(`Failed to remove role: ${error}`);
}
} else {
console.log(`${member.user.tag} does not have the role "${role ? role.name : 'unknown'}".`);
}
}
});

client.login('TOCKEN');
const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences, // Needed to track user presence
],
});

// Define the target custom status and role ID
const TARGET_CUSTOM_STATUS = '.gg/dallaspd'; // The custom status you want to check for
const ROLE_ID = '1287515637714911322'; // The ID of the role you want to assign

client.on('ready', () => {
console.log(`${client.user.username} is online!`);
});

client.on('presenceUpdate', async (oldPresence, newPresence) => {
const member = newPresence.member;

if (!member) return; // Ensure this is a guild member

// Log user's status change
console.log(`${member.user.tag} changed status:`);

// Check activities for the target custom status
const activities = newPresence.activities;
const hasTargetStatus = activities.some(activity =>
activity.type === 'CUSTOM' && activity.name === TARGET_CUSTOM_STATUS
);

// Log current activities
activities.forEach(activity => {
console.log(`- ${activity.type}: ${activity.name}`);
});

// Find the role by ID
const role = member.guild.roles.cache.get(ROLE_ID);

// If the user has the target custom status
if (hasTargetStatus) {
console.log(`${member.user.tag} has the target custom status: "${TARGET_CUSTOM_STATUS}".`);

// Check if the user already has the role
if (role) {
if (!member.roles.cache.has(role.id)) {
try {
await member.roles.add(role);
console.log(`Assigned role "${role.name}" to ${member.user.tag}.`);
} catch (error) {
console.error(`Failed to add role: ${error}`);
}
} else {
console.log(`${member.user.tag} already has the role "${role.name}".`);
}
} else {
console.error(`Role with ID ${ROLE_ID} not found.`);
}
} else {
// If the user doesn't have the target status, check if they have the role and remove it
if (role && member.roles.cache.has(role.id)) {
try {
await member.roles.remove(role);
console.log(`Removed role "${role.name}" from ${member.user.tag}.`);
} catch (error) {
console.error(`Failed to remove role: ${error}`);
}
} else {
console.log(`${member.user.tag} does not have the role "${role ? role.name : 'unknown'}".`);
}
}
});

client.login('TOCKEN');
That is my code Also I just noticed I posted this in the wrong area 😭 The newest version, just installed it again today. And all the logs is when someone changes there status, and when it removes the role
gblazzy
gblazzyOP2mo ago
No description
gblazzy
gblazzyOP2mo ago
wdym im still new to this stuff 😭
d.js docs
d.js docs2mo ago
:dtypes: v10: ActivityType - Custom read more
gblazzy
gblazzyOP2mo ago
so how do i fix it alr Still getting the same issues, nothing really changes, still also showing “4”
gblazzy
gblazzyOP2mo ago
No description
gblazzy
gblazzyOP2mo ago
I have the right status set so in theory it should give me the role and log it not say ion got the role Still no change. 😭 Sorry to bother you
d.js docs
d.js docs2mo ago
If you aren't getting any errors, try to place console.log checkpoints throughout your code to find out where execution stops. - Once you do, log relevant values and if-conditions - More sophisticated debugging methods are breakpoints and runtime inspections: learn more
NyR
NyR2mo ago
Qjuh told you that activity.type is a number but you are still comparing it to a string, you stringified the enum
gblazzy
gblazzyOP2mo ago
Yeah but how to fix?
NyR
NyR2mo ago
Do not put the enum in strings, use it as is, import the ActivityType enum from d.js, you should know by now how to use enums If not, then #rules 3
mo
mo2mo ago
so a vanity bot checker
gblazzy
gblazzyOP2mo ago
Yh
mo
mo2mo ago
bio or statues
gblazzy
gblazzyOP2mo ago
Status
mo
mo2mo ago
this is legit chat gpt code lol let me fix it rq 1 sec @gblazzy do u even know how to code or
gblazzy
gblazzyOP2mo ago
Yh I ran it through GPT to try and find the mistake
mo
mo2mo ago
ohh alr i remade it works nowo anyways gl cya
Want results from more Discord servers?
Add your server