The problem is, that you can not change the key if you don't remove it first. And to remove it, you must know the current key, because "mode #chan -k" dont work. Is it possible, that the script check which key is actually set an remove it first with mode "#chan -k key"?