Also, you made a huge mistake - while setting colors in the script, you neglected to use \"0\" before all numbers below ten, ie, \"01, 02, 03\" etc. Not doing this will result in skewed information because the bot cannot return numbers correctly.
Another good fix for messages sent to query:
close -m $nick
And for the \"on :text::*:{\" events:
if (!$chan) close -m $nick