Reputation: 11
How can you check roles from a certain server/guild?
Scenario: I have two discord servers, one old with 3k+ people, the other new: only selected people can join, my bot is on both. I have been handing out invites through PM, but I want to make the invite link public But only certain people can join new server, only the people that have "OG" role, if they don't have that role in server 1, they will get kicked/banned from server two whenever they try to join
so far I have this:
bot.on("guildMemberAdd", member => {
let guild = member.guild;
let user = member.user;
// log [join] from each server
console.log(user.username + " (" + user.id + ") joined " + guild.name + " (" + guild.id + ")");
});
Upvotes: 1
Views: 5287
Reputation: 2821
As long as you have the Guild
object of the server you want to check the role of in your client's list of Guilds, this isn't too difficult to do. All you need to do is figure out the id string of the Guild you want to check permissions in. Then, you need to get the user's GuildMember
from the other Guild and check its roles. If they aren't in the other Guild or they don't have the role, kick/ban them.
bot.on("guildMemberAdd", member => {
let guild = member.guild;
let user = member.user;
let oldGuild = bot.guilds.filter(x.id => x === /* put the id string here */);
let oldGuildMember = oldGuild.member(user);
if (oldGuildMember == undefined || !oldGuildMember.roles.has('OG')) {
member.kick().then((kicked) => {
if (kicked) {
console.log('Kicked ' + user.name);
} else {
console.log('Failed to kick user.');
}
});
}
});
Upvotes: 1