Loop for bot role has

const rol = "1048495552322609232";
const guild = message.guild;
const mem = [];
const role = guild.roles.cache.get(rol);

if (!role) {
message.channel.send('Cannot find the role!');
} else {
guild.members.fetch()
.then((members) => {
console.log("hu");
members.forEach((member) => {
if (member.bot) {
console.log(`Bot: ${member.user.tag}`);
if (member.roles.cache.has(role.id)) {
mem.push(`<@${member.id}>`);
console.log(`Added: ${member.user.tag}`);
} else {
console.log(`Bot ${member.user.tag} doesn't have the role.`);
}
}
});
message.channel.send('hi');
console.log(mem);
})
.catch((e) => {
console.error(e);
});
}
const rol = "1048495552322609232";
const guild = message.guild;
const mem = [];
const role = guild.roles.cache.get(rol);

if (!role) {
message.channel.send('Cannot find the role!');
} else {
guild.members.fetch()
.then((members) => {
console.log("hu");
members.forEach((member) => {
if (member.bot) {
console.log(`Bot: ${member.user.tag}`);
if (member.roles.cache.has(role.id)) {
mem.push(`<@${member.id}>`);
console.log(`Added: ${member.user.tag}`);
} else {
console.log(`Bot ${member.user.tag} doesn't have the role.`);
}
}
});
message.channel.send('hi');
console.log(mem);
})
.catch((e) => {
console.error(e);
});
}
The problem is that even when bots are in the server having the role, it completely skips the condition and array will be empty
2 Replies
d.js toolkit
d.js toolkit8mo 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 OP
NanotechPikachu
NanotechPikachu8mo ago
const rol = "1048495552322609232";
const guild = message.guild;
const mem = [];
const role = guild.roles.cache.get(rol);

if (!role) {
message.channel.send('Cannot find the role!');
} else {
await guild.members.fetch()
.then((members) => {
console.log("hu");
members.forEach((member) => {
if (member.bot) {
console.log(`Bot: ${member.user.tag}`);
if (member.roles.cache.has(role.id)) {
mem.push(`<@${member.id}>`);
console.log(`Added: ${member.user.tag}`);
} else {
console.log(`Bot ${member.user.tag} doesn't have the role.`);
}
}
});
message.channel.send('hi');
console.log(mem);
})
.catch((e) => {
console.error(e);
});
}
const rol = "1048495552322609232";
const guild = message.guild;
const mem = [];
const role = guild.roles.cache.get(rol);

if (!role) {
message.channel.send('Cannot find the role!');
} else {
await guild.members.fetch()
.then((members) => {
console.log("hu");
members.forEach((member) => {
if (member.bot) {
console.log(`Bot: ${member.user.tag}`);
if (member.roles.cache.has(role.id)) {
mem.push(`<@${member.id}>`);
console.log(`Added: ${member.user.tag}`);
} else {
console.log(`Bot ${member.user.tag} doesn't have the role.`);
}
}
});
message.channel.send('hi');
console.log(mem);
})
.catch((e) => {
console.error(e);
});
}
Tried this too but, doesn't work Hmm when did that happen? Something like this loop worked around 1 month ago. Lemme try what you said now. Well, it works. Thanks