You could simply use something like:
on *:TEXT:!voice*:#:{
if ($2) && ($2 ison $chan) { mode $chan +v $2 }
else { mode $chan +v $nick }
}
Though there are several (shorter) ways to achieve the same.
Note that ANYONE can use this trigger to voice themselves or others.