So what you are saying is when it comes time to point to a channel, because you know its not a query, $chan is $null?

Have you echo'd the event to find out where the channel is stored? Maybe try $rawmsg,