Is there a way to make a buff-stealing skill?

I want to know if there is a plugin or if there is a combination of plugins to use in order to make this kind of skill.
It's where a certain skill is used to take the buffs an enemy already has.

Apologies if there's an obvious answer to this question.
It can be done with Yanfly's Buffs & States Core (a free plugin), I guess, but I can't figure out how.
Here's what I tried to use:
Code:
<After Eval>
// Loop through all parameters
for (var i = 0; i < 8; i++) {
  if (target._buffs[i] > 0) {
  // Copy the target's buffs, if they exist
    user._buffs[i] = target._buffs[i];
  }
  // Remove the target's buffs
  target._buffs[i] = 0;
}
</After Eval>
The problem I have is while I can copy buffs, I can't copy their duration, meaning they'll expire the next turn, and I have no idea how to copy a buff's duration.

EDIT: Nevermind, there's actually a property called _buffTurns that can be used to return the number of remaining turns of a buff. Hooray!
Code:
<After Eval>
// Loop through all parameters
for (var i = 0; i < 8; i++) {
  if (target._buffs[i] > 0) {
  // Copy the target's buffs, if they exist
    user._buffs[i] = target._buffs[i];
  // Copy the duration of the target's buffs
    user._buffTurns[i] = target._buffTurns[i];
  }
  // Remove the target's buffs
  target._buffs[i] = 0;
}
</After Eval>

SECOND EDIT: Turns out my previous code overwrites the user's current buffs. I'll change it a bit again:

Code:
<After Eval>
// Loop through all parameters
for (var i = 0; i < 8; i++) {
  if (target._buffs[i] > 0) {
  // Copy the target's buffs, if they exist
    user._buffs[i] += target._buffs[i];
  // Copy the duration of the target's buffs
    user._buffTurns[i] = target._buffTurns[i];
  }
  // Remove the target's buffs
  target._buffs[i] = 0;
}
</After Eval>

If you also want to add the duration of the target's buffs:
Code:
<After Eval>
// Loop through all parameters
for (var i = 0; i < 8; i++) {
  if (target._buffs[i] > 0) {
  // Copy the target's buffs, if they exist
    user._buffs[i] += target._buffs[i];
  // Copy the duration of the target's buffs
    user._buffTurns[i] += target._buffTurns[i];
  }
  // Remove the target's buffs
  target._buffs[i] = 0;
}
</After Eval>
Thanks! Very much appreciated.
Hi Guys, I've tweaked the code above from JojoLaVache (lol, ah la vache!) to do the exact opposite: my skill will take all ailments and debuffs from target(s) and transfer them to the user. This spell is for a cleric who will absorb ailments from the group and take it on.

This will require Yanfly's


Code:
<After Eval>

// There will be some healing at the end of all this
var healing = 0;

// TRANSFER AILMENTS

// Get the states the target is currently affected by.
var states = target.states();

// This is the category we want to transfer.
var category = 'Ailment';

// Create a loop that goes through each state.
while (states.length > 0) {

  // Get the currently looped state.
  var state = states.shift();

  // Check if the state's categories has the one we want.
  if (state.category.contains(category.toUpperCase())) {

    // Get the number of turns left on the target for the state.
    var turns = target.stateTurns(state.id);

    // Remove the state from the target.
    target.removeState(state.id);

    // Check if target was already affected by this state
    if (user.isStateAffected(state.id)) {

        // Add existing turns to pool
        turns += user.stateTurns(state.id);

    } else {

        // Add the state to the user.
        user.addState(state.id);

    }

    // Set the number of turns for that state for the user.
    user.setStateTurns(state.id, turns);

    // Add healing
    healing += 1;

  }
}

// TRANSFER DEBUFFS

// Loop through all parameters
for (var i = 0; i < 8; i++) {

    // If debuff
    if (target._buffs[i] < 0) {
       
        // Copy the target's debuffs, if they exist
        user._buffs[i] += target._buffs[i];
     
        // Copy the duration of the target's debuffs
        user._buffTurns[i] += target._buffTurns[i];

        // Remove the target's debuffs
        target._buffs[i] = 0;

        // Heal
        healing += 1;

    }

}

// HEAL

// Check if the healing amount isn't 0...
if (healing !== 0) {

    // Gain HP
    user.gainHp(healing);

}

</After Eval>

Original thread: https://forums.rpgmakerweb.com/threads/is-there-a-way-to-make-a-buff-stealing-skill.120214/