Like napa stated, implementing some checks are recommended. The crucial one has to be
if ($2 ison $chan) {
because if someone mistypes or enters something else other than an actual nickname in the channel where you have the code run, you'll get an error. Last but not least, a trigger flood check is advised, just in case an op or halfop gets bananas for whatever reason to screw up the client that runs the code by way of flooding. Better safe than sorry!