You don't need to check if $nick is already an @op of $chan during ON JOIN, because that never happens.
But you do need to check if yourself has privileges to op them. If you want to trigger only if you are @op in the channel, you can replace *:JOIN: with @*:JOIN: or if you have & or ~ status without also having @ status, you can instead do:
if (!$nick($chan,$me,a,rvh)) return
For $nick == $me, you can just RETURN since a HALT might interfere with other scripts which intercept the JOIN event. However, in this case you can skip checking if the $nick == $me because the @*:JOIN: prevents this from triggering when you join, because you can't be already-op when you join, the same way they can't be already-op when they join.
It's best practice to be in the habit of using the 'nt' switches with $read except when you specifically want to evaluate %word and $word found in the match text, or when you specifically want to treat a numeric line#1 as if it's the number of lines in the text file.
Using $read with the 'w' switch returns the matchtext, which the if() statement puts into $v1, so you can save doing a 2nd disk read by instead creating %str like: var %str $v1
When you are reading the file, it returns the first match, so this always returns the #test1 line for the 1st host, and the #test3 line for the 2nd host. You can change the script to keep reading multiple times to find more than 1 line having their address, by starting the search at different lines in the file, but it would be simpler for you to have each address appear 1 time like hostname.irccloud;#test3 #test2
This code is triggered with someone joining all channels, but then it checks if it's in a list of channels once it's triggered. It's faster to instead limit the triggering to just the list of channels, by changing :#: to :#test1,#test2,#test3: without any spaces.
Your code makes it possible for false matches. If I own website irccloud.co then I can create address id-586239.lymington.irccloud.co and this will match $read(%file,w,$+(*,%host,*)) and $+(*,%host,*) will be a wildcard match for $gettok(%str,1,59). If the purpose of your wildcard is to match the line where the host is immediately followed by the semicolon then $read(%file,ntw,$+(%host,;?*)) Notice how this does not match a line with nothing after the semi-colon
It's wasted effor to test for whether $+(*,%host,*) matches the string which matched the same wildcard scan of the disk file, because this would always match except if %host contains the illegal semicolon
Your code also blindly attempts to +o someone in multiple channels without checking to see if they're actually in all of them, which is guaranteed to send an erroneous +o command to the first of the channels they join. One you modify the disk file to have all the channels on the same line, you would then parse it to op them just in the channel that $nick is currently joining. The line beginning with 'mode' would instead be replaced by:
if ($istok(%chanop,$chan,32)) mode $chan +o $nick | .notice @ $+ $chan $chan - $me ( $+ $me $+ ) opped: $nick
If you want to have it attempt to op them in all listed channels to cover the situation where they joined #test3 before you joined it, but are joining #test2 after you joined the channel, you can have it loop through the channel list just in case:
var %i 1 | while ($gettok(%chanop,%i,32)) {
var %chan $v1
if ( ($nick(%chan,$nick,r) && ($me isop %chan)) {
mode %chan +o $nick | .notice @ $+ %chan %chan - $me ( $+ $me $+ ) opped: $nick
}
inc %i
}
If you wanted the channels to be listed on multiple rows so that sometimes people could be voiced instead of op'ed, you can have the lines be hostname;#test1 o #test2 v
and then the code will need to change to handle the different modes, which I won't take the trouble to change unless that's what you're needing, since it would be simpler to have a different %file for each purpose.
Below is sanitized code. I could have made it be shorter since %file and %host and %str are used 1 time, but I left them that way to be more readible
on @*:JOIN:#test1,#test2,#test3:{
var %host = $ial($nick).host , %file = autoopip.txt
if ($read(%file,ntw,$+(%host,;?*))) {
var %str $v1 , %chanop $gettok(%str,2,59)
if ($istok(%chanop,$chan,32)) mode $chan +o $nick | .notice @ $+ $chan $chan - $me ( $+ $me $+ ) opped: $nick
}
}