Reputation: 17
I am programming in discord.js and I am making a command which would kick everybody from a server. The thing is, I wanted to kick everyone except me, and I don't know how to do that, nor did I find any tutorial on it. Here is my code:
bot.on("message", async message => {
if(message.content.startsWith(`${prefix}gone`)) {
message.guild.members.cache.forEach(member => member.ban())
}
}
)
Upvotes: 0
Views: 818
Reputation: 5174
In your .forEach()
loop you need to add a check to see if the member
has the same ID as you so you could do this two ways:
Either by making the bot ignore the user who typed in the command:
message.guild.members.cache.forEach(member => {
if (member.id !== message.author.id) member.ban()
})
Or if you want to just make it not ban you specifically then:
message.guild.members.cache.forEach(member => {
if (member.id !== 'USER_ID') member.ban()
})
You can also write those checks inside of a .filter()
like so:
message.guild.members.cache.filter(member => member.id !== 'USER_ID').forEach(member => member.ban())
Upvotes: 1
Reputation: 1434
Try something like this:
const MY_USER_ID = 'your user id'
bot.on("message", async message => {
if(message.content.startsWith(`${prefix}gone`)) {
message.guild.members.cache.forEach(member => {
if (member.id !== MY_USER_ID) {
member.ban()
}
})
}
})
Upvotes: 1