Iwo
Iwo

Reputation: 3

Is it possible to ban users that aren't in the discord server?

I recently started on making my discord bot in javascript. I made my own bot have the power of banning people and it successfully bans people that are IN the server, but I wanted to know if there was a way I can ban people that left the server or even never joined the server.

const bUser = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));

if(!bUser) return message.channel.send("Couldn't find user.");

message.guild.member(bUser).ban(banreason);

return message.channel.send("User " + bUser + " was banned");

When I ban someone not in the server it says: "Couldn't find user."

Upvotes: 0

Views: 5293

Answers (1)

slothiful
slothiful

Reputation: 5623

You can use the Guild.ban() method which uses a User instead of a GuildMember.

Example:

// async context

try {
  const user = message.mentions.users.first() || await client.fetchUser(args[0]);
  if (!user) return await message.channel.send('Unable to find user.');

  const reason = args.slice(1).join(' ') || 'No reason provided';

  await message.guild.ban(user, { reason: reason });
} catch(err) {
  console.error(err);
}

Upvotes: 1

Related Questions