Electric Eel nerve cells
Most nervous cells in the animal kingdom discharge once a while regardless of what's happening. An Electric Eel is different. They have some cell structures that can hold a charge for longer. I can’t remember if these were specialised cells or actual nerve cells, but regardless they can hold thich charge. Thanks to many of these cells connected in series they can give big shocks.
In the plant kingdom we see plenty of plants that use mechanisms for rapid movement. It can be used to catch animals, fling seeds or fire tiny needles. These are often one shots or require some time to recharge, but serve their purpose.
A lightning tree can use the same idea. Adding some of these cells throughout, though these are much slower than in the animal kingdom. Electricity is a very energy wasting method more used by creatures using a ton of energy, so trees and plants would use a for their metabolism stable method.
The electricity can be grounded to itself. Some cells on the bark will be primed to fire if something touches it, while close-by cells will ground the electricity. Something landing would likely touch both and get a nasty big shock as it closes the circuit.
If you truly want a tesla coil/van der Graaf generator it will be much more tricky. The bark must be resistant to electricity, like plastic protecting a wire. Otherwise the tree would just ground to itself. Next it needs to up the voltage to insane amounts. It isn't impossible, as you can get table top versions and create sparks yourself. It'll have only a few exposed parts for the electricity to flow at strategic locations. Then it needs something close-by to close the circuit to the ground. Flying creatures would still have a potential to be safe. You could have it discharge not based on physics of high energy potentials, but 'manually'. This way a bird or animal triggers something in the tree that will let a discharge phase begin. It'll ground via the animal in either itself with strategically placed grounds or into the ground itself. It can potentially change the potential depending on the perceived size of the animal, not using too much overkill on insects.
The tree would have multiple stacks of these electrical cells to have several charges ready, making it more difficult to deplete the tree at one go. It can also have both systems at once, allowing it to fend off both small and big creatures at will. After It'll happily recharge by standing in the sun.
A way to up the metabolism is hidden in DWKraus his answer. Feeding on the insects and other animals can provide a boost to extracting energy. It can have digestion pools or assisting life that will make it easy for the tree to absorb these nutrients, allowing for quicker metabolism, quicker recharge and extra nutrients overall to survive.