Reputation: 13
So I want it where, if a user gets banned from this specified guild, it will ban them in every other guild that the bot is in. Do I use fetch bans to do this?
Upvotes: 0
Views: 169
Reputation: 6645
Before we continue you need to make sure that you are requesting the following intents when creating your bot, as they are necessary to achieve your goal: GUILDS
, GUILD_BANS
.
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_BANS]
});
You should also set up a constant called MAIN_GUILD_ID
or something along those lines:
const MAIN_GUILD_ID = 'ID';
Now you need to listen to the guildBanAdd
event and check if the GuildBan.guild#id
equals the main Guild#id
(making sure the user was banned in the main Guild
).
client.on('guildBanAdd', async ban => {
if (ban.guild.id !== MAIN_GUILD_ID) return;
});
Then you can loop through all of the Guilds
your bot is in and ban the user there.
client.on('guildBanAdd', async ban => {
if (ban.guild.id !== MAIN_GUILD_ID) return;
// Getting the guilds from the cache and filtering the main guild out.
const guilds = await client.guilds.cache.filter(guild => guild.id !== MAIN_GUILD_ID);
guilds.forEach(guild => {
guild.members.ban(ban.user, {
reason: `${ban.user.tag} was banned from ${guild.name}.`
}).then(() => {
console.log(`Banned ${ban.user.tag} from ${guild.name}.`);
}).catch(err => {
console.error(`Failed to ban ${ban.user.tag} from ${guild.name}.`, err);
});
})
});
I'm assuming this is for a private bot that is in a few guilds, otherwise, if the bot is in a few hundred or more guilds it will get your application rate-limited pretty fast.
Upvotes: 1