Try using $chat(%nick,1).wid instead of $chat($chat(0)).wid
$chat(0) returns the number of open chat windows.
$chat(%nick,1) isnt guaranteed to refer to the chat session assosciated with the open event
here's a trick i saw qwerty using a while ago, $wid doesnt return the correct value in the event, but you can catch its value with the following:
alias -l getwid %wid = $wid | return =
on ^*:open:$($getwid):{ }
then %wid returns the correct window ID inside the event