I was wondering if it's possible to ban someone via User ID. Normally, to ban someone, you'd use a GuildMember and the .ban() method:
GuildMember.ban()
However, if a user is not in the guild you want to ban them from, it's impossible to get their GuildMember object. Is there an alternate method?
Using the actual Discord program, it is possible to ban someone before they join by mentioning them with their User ID (for example: <@123456789012345678>). Then, you can right-click the mention, and then ban them:

Is there a way to replicate this with discord.js?
During my research on the problem, I stumbled upon someone asking the same question as me. The accepted answer was to use the Guild.ban function.
However, since I couldn't find the method in the Guild class docs, and the same answer used fetchUser() (a deprecated function), I came to the conclusion that it would only work in discord.js v11.
Since it was previously a function in an earlier version, I'm sure there's probably still a way to do it now that I haven't found.
Lastly, I know that I could just add the User ID to an array, and whenever the guildMemberAdd event is emitted I could check their User ID against that array, but I'd still like to know if banning a member by User ID is possible. Thanks in advance!
Edit: Docs for the v11 Guild.ban() function
In v12 discord.js moved such methods which interact with guild members all to the GuildMemberManager class, accessible via guild.members.
The method you're looking for is GuildMemberManager.ban(id). There's an example on the docs:
guild.members.ban('84484653687267328')
.then(user => console.log(`Banned ${user.username || user.id || user} from ${guild.name}`))
.catch(console.error);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With