Just a note - instead of $chan(#CHAN).mode you should use
$gettok($chan(#chan).mode,1,32)
Otherwise it will trigger if the chanel happens to keyed, and the key has an i in it.
It's not very likely in this case i know - but its something you should keep in mind