$chan has no value in the On Quit event. You either specify the channel or use $comchan($me,N) or $chan(N) <- N being your Nth channel.