I think the opening would be better as:
on @*:deop:#:
I think the line:
if ($opnick == $me) && ($nick != $me) && (X isin #) { .msg x op # }
is meant to be:
if($deopnick == $me) && ($nick != $me)...
On message, X might also like to know who to op.