Kuezy
Kuezy

Reputation: 101

DiscordJS global kick

I have one question. How do I kick a user in every server that the bot and the user is in? I already tried it by myself, but it doesn't work.

bot.guilds.cache.map(SusUser.kick())

Upvotes: 0

Views: 108

Answers (1)

Samathingamajig
Samathingamajig

Reputation: 13245

This should work:

// https://stackoverflow.com/a/39914235/12101554
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// end copy

for (const guild of bot.guilds.cache) {
  const susGuildUser = await guild.members.fetch(SusUser);
  if (susGuildUser) {
    susGuildUser.kick();
    await sleep(200); // To prevent rate limiting, sleep 200ms between each kick (don't wait if user not in guild)
  }
}

This loops through all the guilds in the cache, looks for SusUser (i'm assuming this is a User object, which would automatically cast to UserResolvable), and if it exists (if they're in that server), kick them.

Upvotes: 2

Related Questions