Remove debuff through scripting

Hello, again. I'm trying to make an offensive skill that transfers any user's debuffs onto the target. The problem is I don't know how to remove the user's debuffs. So far, I have written this:
def witch_hunt(a, b) z = [0, 1, 2, 3, 4, 5, 6, 7] for i in z if a.debuff?(i) # put here the method which removes debuff(i) b.add_debuff(i, 2) end end end

I know there is a method called "item_effect_remove_debuff(user, item, effect)". I don't if this can be my solution or how to handle it tho.
Perhaps this scriptlet can help you with that.

EDIT: I had to modify some lines of code in the second method to make it as selective as it was expected.

Ruby:
# * Hand Over Buffs or Debuffs ACE * #
#   Scripter : Kyonides Arkanthes
#   2022-12-15

# 2 Formula Calls #
# - Target b gets Spellcaster a's buffs and debuffs - #
#   a.hand_over_buffs(b)
# - Target b gets Spellcaster a's debuffs only - #
#   a.hand_over_debuffs(b)
 
class Game_Battler
  attr_writer :buffs, :buff_turns
  def hand_over_buffs(target)
    target.buffs = @buffs.dup
    target.buff_turns = @buff_turns.dup
    @buffs.size.time do |n|
      @result.removed_buffs.push(n).uniq!
      target.result.added_debuffs.push(n).uniq!
    end
    clear_buffs
    target.refresh
    refresh
  end

  def hand_over_debuffs(target)
    @buffs.size.times do |n|
      next if @buffs[n] >= 0
      target.buffs[n] = @buffs[n]
      target.buff_turns[n] = @buff_turns[n]
      @buffs[n] = 0
      @buff_turns[n] = 0
      @result.removed_buffs.push(n).uniq!
      target.result.added_debuffs.push(n).uniq!
    end
    target.refresh
    refresh
  end
end
That's very kind of you to come with this. Thank you. However, I get this error:

1671134786103.png


My damage formula is this:

when /WITCH_HUNT/i a.hand_over_debuffs(b); value += a.atk * 4;

(I use Yanfly's Lunatic Damage Script)
Using just the default battle system like I did on my test project, I never got any error messages.
Sadly, I did notice that it simply ignored the contents of my methods for unknown reasons.
In order to make it run as intended, I had to come up with this new code:

Ruby:
# * Hand Over Buffs or Debuffs ACE * #
#   Scripter : Kyonides Arkanthes
#   2022-12-16

# * Instructions:
# Leave a skill note like the following:
# <handover buffs> or <handover debuffs> or <handover all buffs>
# You can only leave 1 of those 3 notes at any given time!

class Game_Battler
  REGEX_BUFFS = /<handover buffs>/i
  REGEX_DEBUFFS = /<handover debuffs>/i
  REGEX_DE_BUFFS = /<handover all buffs/i
  attr_reader :buffs, :buff_turns
  alias :kyon_de_buffs_gm_btlr_iue :item_user_effect
  def item_user_effect(user, item)
    kyon_de_buffs_gm_btlr_iue(user, item)
    return if
    if item.note[REGEX_BUFFS]
      hand_over_buffs(user)
    elsif item.note[REGEX_DEBUFFS]
      hand_over_debuffs(user)
    elsif item.note[REGEX_DE_BUFFS]
      hand_over_all_buffs(user)
    end
  end

  def clear_buff(pos)
    @buffs[pos] = 0
    @buff_turns.delete(pos)
  end

  def hand_over_buffs(user)
    user.buff_turns.keys.sort.each do |n|
      next if user.buffs[n] < 0
      @buffs = user.buffs[n]
      @buff_turns = user.buff_turns[n]
      user.clear_buff(n)
      @result.added_buffs.push(n).uniq!
    end
    user.refresh
    refresh
    @result.success = true
  end

  def hand_over_debuffs(user)
    user.buff_turns.keys.sort.each do |pos|
      next if user.buffs[pos] >= 0
      @buffs[pos] = user.buffs[pos]
      @buff_turns[pos] = user.buff_turns[pos]
      user.clear_buff(pos)
      @result.added_debuffs.push(pos).uniq!
    end
    user.refresh
    refresh
    @result.success = true
  end

  def hand_over_all_buffs(user)
    @buffs = user.buffs.dup
    @buff_turns = user.buff_turns.dup
    @buffs.size.times do |pos|
      if user.buffs[pos] > 0
        @result.added_buffs.push(n).uniq!
      else
        @result.added_debuffs.push(pos).uniq!
      end
    end
    user.clear_buffs
    user.refresh
    refresh
    @result.success = true
  end
end

It certainly defeats the purpose of using a damage formula as the means to apply the desired effect, yet, this new code definitely works in the default battle system. You can clearly see the effects on screen in no time.

What makes this new code different is that it uses Skill Notes instead of formulas. XD
Thank you for your effort. Really. The problem persists, so I created a new project and testes your script again. It works well until I add Yanfly's buff manager. It seems these two scripts are incompatible

Original thread: https://forums.rpgmakerweb.com/threads/remove-debuff-through-scripting.153328/