const Command = require('../Command')
const DiscordServer = require('../../DiscordServer')
const { Role } = require('discord.js')

async function recursiveUpdate (memberArray, server, msg) {
  let nextMember = memberArray.pop()
  if (!nextMember) {
    return msg.reply(`:white_check_mark: Finished bulk update! ${server.bulkUpdateCount} members affected.`).then(() => {
      server.bulkUpdateCount = 0
      server.ongoingBulkUpdate = false
    })
  }

  if (!nextMember.user.bot) {
    let member = await server.getMember(nextMember.id)
    if (member) {
      await member.verify({ skipWelcomeMessage: true })
      server.bulkUpdateCount++
    }
  }
  return recursiveUpdate(memberArray, server, msg)
}

module.exports =
class UpdateCommand extends Command {
  constructor (client) {
    super(client, {
      name: 'update',
      properName: 'Update',
      aliases: ['roverupdate'],
      description: '`<Discord User>` Forcibly update verification status of a user, same as them running !verify. Make sure you @mention the user.',

      args: [
        {
          key: 'user',
          prompt: 'User to update',
          type: 'user|role'
        }
      ]
    })
  }

  hasPermission (msg) {
    return this.client.isOwner(msg.author) || msg.member.hasPermission(this.userPermissions) || msg.member.roles.find(role => role.name === 'RoVer Admin') || msg.member.roles.find(role => role.name === 'RoVer Updater')
  }

  async fn (msg, args) {
    let target = args.user
    DiscordServer.clearMemberCache(target.id)

    let server = await this.discordBot.getServer(msg.guild.id)
    if (!(target instanceof Role)) { // They want to update a specific user (roles have .hoist, users do not)
      let member = await server.getMember(target.id)
      if (!member) {
        return msg.reply('User not in guild.')
      }

      member.verify({ message: msg, skipWelcomeMessage: true })
    } else if (!this.discordBot.isPremium()) {
      return msg.reply('Sorry, updating more than one user is only available with RoVer Plus: <https://www.patreon.com/erynlynn>.')
    } else { // They want to update a whole role (premium feature)
      let roleMembers = target.members.array()
      let affectedCount = roleMembers.length // # of affected users
      let server = await this.discordBot.getServer(msg.guild.id)

      if (server.ongoingBulkUpdate) {
        return msg.reply('There is already an ongoing bulk update in this server.')
      }

      if (affectedCount > 250) {
        return msg.reply(`Sorry, but RoVer only supports updating up to 250 members at once. Updating this role would affect ${affectedCount} members.`)
      }

      server.ongoingBulkUpdate = true
      msg.reply(`:hourglass_flowing_sand: Updating ${affectedCount} members. We'll let you know when we're done.`)

      recursiveUpdate(roleMembers, server, msg)
    }
  }
}

@@ -122,16 +122,65 @@
 ord.js')
+%0Aconst config = require('../../data/client.json')
 %0A%0Aasync 
@@ -2492,24 +2492,72 @@
 ')%0A      %7D%0A%0A
+      const limit = config.massUpdateLimit %7C%7C 0%0A
       if (af
@@ -2570,19 +2570,21 @@
 Count %3E 
-250
+limit
 ) %7B%0A    
@@ -2655,11 +2655,16 @@
  to 
-250
+$%7Blimit%7D
  mem

Make update limit configurable