Search code examples
scriptingmirc

mIRC/mSL words from readini, can they be split?


Brand new to mSL and just playing around with trying to make a bot. So I am trying to make something that when a user says a certain word, they get +1 to a count against their name. However they can then not say the word again to further increase their count unlimited times, they must find a new word.

To ensure the words cannot be used multiple times I am writing the words to a file, I then need to load these words and check if its been said already or not against what the user has just said, and act appropriately

on *:TEXT:&:#:{
var %msg

if ($1 == text1) { %msg = msg1 }
elseif ($1 == text2) { %msg = msg2 }
elseif ($1 == text3) { %msg = msg3 }
else { return }
msg # %msg 

var %keyword = $readini(keyword.ini,#,$nick)
if (%keyword == $1) {
 msg # you already have this keyword! :(
}
else {
  var %count = $readini(cookies.ini,#,$nick)
  inc %count
  writeini cookies.ini # $nick %count
  writeini keyword.ini # $nick %keyword $+ , $+ $1
 }
}

keyword.ini file looks like:

nickname=text1,text2

is there anyway in mSL that I can grab the list (already done in code above) and then use something similar to .split(,) to divide up the words to run through a for/next?

Thanks in advance

EDIT: I tried out the below and although it did work! I then deleted the file to test it out, and it never remade the file despite the writeini. I even added a writeini keyword.ini at the start of the script to make sure the file is present before any text is written, still didn't make it.

on *:TEXT:&:#:{
  var %msg

if ($1 == text1) { %msg = msg1 }
elseif ($1 == text2) { %msg = msg2 }
elseif ($1 == text3) { %msg = msg3 }
else { return }
msg # %msg 

  var %i = 1, %keyword = $readini(keyword.ini,n,$chan,$nick), %cookie = $readini(cookies.ini,n,#,$nick)
  while (%i <= $numtok(%keyword, 44)) {
    if ($istok(%keyword, $1, 44)) {
      msg # you already have this keyword! :(
    }
    else {
      inc %cookie
      writeini cookies.ini $chan $nick %cookie
      msg # congrats! you found a keywords
      writeini keyword.ini $chan $nick $addtok(%keyword, $1, 44)
    }
    inc %i
  }

Solution

  • You're looking for mIRC's token identifiers. I would suggest reading the help files (/help token identifiers) to read more about them.

    Use $istok() to check whether the line contains that keyword already:

    if ($istok(%keyword, $1, 44)) // Keyword exists
    

    Use $addtok() to add a new keyword to the line, and then write it to the file:

    writeini keyword.ini # $nick $addtok(%keyword, $1, 44)
    

    Use $numtok() and $gettok() to create a loop to read all the values:

    var %i = 1, %keywords = $readini(cookies.ini, n, channel, nick)
    while (%i <= $numtok(%keywords, 44)) {
      echo -a Keyword %i $+ : $gettok(%keywords, %i, 44)
      inc %i
    }
    

    Important note: always use the n switch with $readini() (like I did above) when reading data, especially when it's data that users can enter. Without it, $readini() will evaluate the contents (e.g., $me will be evaluated to your current nickname). Users can inject malicious code this way.


    Edit for the inserted question: You are using a while loop to check whether they possess the cookie - it will loop once for every cookie they have (0 loops for no cookies). You don't need this while loop at all, since $istok(%keywords $1, 44) will take all the keywords and return $true if $1 is in that list of tokens.

    Just the following will suffice:

    var %keywords = $readini(keyword.ini,n,$chan,$nick), %cookie = $readini(cookies.ini,n,#,$nick)
    if ($istok(%keywords, $1, 44)) {
      ; the token $1 is in the list of tokens %keywords
      msg # you already have this cookie! :(
    }
    else {
      ; the token $1 did not appear in the list of tokens %keywords
      inc %cookie
      writeini cookies.ini $chan $nick %cookie
      writeini keyword.ini $chan $nick $addtok(%keywords, $1, 44)
    }