'Can you mass kick people from a server using discord.js

So I want to have a moderation bot that kicks all members with a certain role e.g. "unverified" from the server. 1, is this possible? 2, is this allowed, or would it possibly be a Discord API Breach?

I have a normal kick/ ban command and despite searching the web for ages I can't find any answers. Any help would be hugely appreciated. Thanks in advance.



Solution 1:[1]

Yes it is possible, you would just have to be able to group them somehow, (say the role you mentioned for instance) and then run a forEach() on it and kick.

Is it allowed, yes.

Is it a breach: unclear depends on how many you are kicking but these commands will only kick in small groups (5 per 5 seconds).

Example below:

regular command:

(prefix)purge RoleName Reason

const { Discord, Permissions } = require('discord.js')

module.exports.run = async (client, message, args) => {
    if (!message.member.Permissions.has("ADMINISTRATOR")) {
//handle however if they are not admin
    } else {
        let kicked = []
        const role = message.guild.roles.cache.find(r => r.name === args[0])
        args.shift()
        const kickReason = args.join(' ') || 'No Reason'
        message.guild.members.forEach(member => {
            if (member.roles.has(role)) {
                kicked.push(member)
                member.kick({
                    reason: kickReason
                })
            }
        })
        const completeEmbed = new Discord.MessageEmbed()
            .setTitle('Purge')
            .setDescription(`Members with the following role have been purged "${role.name}".`)
            .addField({
                name: `The members kicked were:`,
                value: `${kicked.join('\n')}`
            })

        message.channel.send({
            embeds: [completeEmbed],
            ephemeral: true
        })
    }
}
module.exports.help = {
    name: "purge",
    description: "Kick members with a certain role",
    usage: "(prefix)purge [RoleName] [Reason]"
}

And as a slash command (interaction): and can be built differently if needed.

/purge role: Rolename reason: reason

const { Discord, Permissions, SlashCommandBuilder } = require('discord.js')

module.exports = {
    data: new SlashCommandBuilder()
        .setName('purge')
        .setDescription('Purge server of role')
        .addRoleOption(option =>
            option
            .setName('role')
            .setDescription('Which role to purge')
            .setRequired(true))
        .addStringOption(option =>
            option
            .setName('reason')
            .setDescription('Reason for purge.')
            .setRequired(true)),
    async execute(client, interaction) {
    const reason4Kick = interaction.options.getString('reason')
    const role2Kick = interaction.options.getRole('role')

    if (!interaction.member.Permission.has('ADMINISTRATOR')) {
// handle if user doesn't have permission to run
    return
    } else {
        let kicked = []
        interaction.guild.members.forEach(member => {
            if (member.roles.has(role2Kick)) {
                kicked.push(member)
                member.kick({
                    reason: reason4Kick
                })
            }
        })
        const completeEmbed = new Discord.MessageEmbed()
            .setTitle('Purge')
            .setDescription(`Members with the following role have been purged "${role.name}".`)
            .addField({
                name: `The members kicked were:`,
                value: `${kicked.join('\n')}`
            })

        interaction.channel.reply({
            embeds: [completeEmbed],
            ephemeral: true
        })
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1