Search code examples
loopswhile-loopircmirc

IRC Script: Why isn't this code working? ON JOIN While loop keeps freezing


This piece of code is meant to create a variable when a person joins a channel. Then it is supposed to increase that variable every second that a person remains in the channel. Then it should check to see if that variable reaches 15, which would mean the person has remained in the channel for 15 seconds. If true, it should do a conglomerate of different actions. But that's where the code stops working. The 15-second code is never triggered and I don't know why.

on *:JOIN:*: {
        inc -c %timeinchan. $+ $nick
        if (%timeinchan. $+ $nick == 15) {
          set %tempstats. $+ $nick $read(scores.fil, nr, $lower($nick))
          set %temppoints. $+ $nick $gettok(%tempstats. $+ $nick,2,59)
          set %newpoints. $+ $nick $calc(%temppoints. $+ $nick + 5)
          set %newstats $replace(%tempstats. $+ $nick, %temppoints $+ $nick, %newpoints. $+ $nick)
          write -s $+ $nick scorestest.fil %newstats
          msg $chan $Nick has been awarded 5 points for staying in the channel for 15 seconds. }
      }

Today, I thought of using a while loop as a possible solution. Something like this:

on *:JOIN:*: {
  while ($nick ison $chan) && ($nick != $me) { 
    inc -c %timeinchan. $+ $nick
    if (%timeinchan. $+ $nick == 15) {
      set %tempstats. $+ $nick $read(scores.fil, nr, $lower($nick))
      set %temppoints. $+ $nick $gettok(%tempstats. $+ $nick,2,59)
      set %newpoints. $+ $nick $calc(%temppoints. $+ $nick + 5)
      set %newstats $replace(%tempstats. $+ $nick, %temppoints $+ $nick, %newpoints. $+ $nick)
      write -s $+ $nick scorestest.fil %newstats
      msg $chan $Nick has been awarded 5 points for staying in the channel for 15 seconds. }
  }

But this doesn't work either. In fact, once someone joins a channel, the script makes mIRC stall, freeze and then crash.

So...

Any suggestions?


Solution

  • first, you dont need a "*" on on join event, since on join events will only happens in a channel

    by the way, events wont happen on on join event, if no one joins, thats why it is not working
    why dont you use timers?

    replace the version you have with this one

    on *:JOIN:#: {
    .timer $+ $nick 0 15 checknick $nick $chan
    }
    
    alias checknick {
    if ($1 ison $2) {
    set %tempstats. $+ $1 $read(scores.fil, nr, $lower($1))
    set %temppoints. $+ $1 $gettok(%tempstats. $+ $1,2,59)
    set %newpoints. $+ $1 $calc(%temppoints. $+ $1 + 5)
    set %newstats $replace(%tempstats. $+ $1, %temppoints $+ $1, %newpoints. $+ $1)
    write $+(-,w,*,$1,*) scorestest.fil %newstats
    msg $2 $1 has been awarded 5 points for staying in the channel for 15 seconds. }
    }
    

    so, everytime someone joins in a channel, it will activate a timer with their name after 15 seconds, it will check if nick is on channel, and then will apply the points on their nick

    while conditions needs variables to work, so

    while ($nick ison $chan) && ($nick != $me) { 
    

    since $nick will always be on channel on on join event, it will always be true

    tell me if its that what you want