mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: May 2008
Posts: 329
A
AWEstun Offline OP
Fjord artisan
OP Offline
Fjord artisan
A
Joined: May 2008
Posts: 329
How do I code a random saying, but not more than X times in Y hours?


I registered; you should too.
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Here's my suggestion. This will read a random line every 5 minutes, and as long as that line hasn't been said more than 10 times in 2 hours, it will be sent to the channel.
This also presumes that the random sayings are in a text file called rand_saying.txt
Code:
on me:*:join:#:{
  .timer 0 300 rand_saying $chan
}
alias -l rand_saying {
  :rand_saying
  var %rand_saying = $r(1,$lines(rand_saying.txt))
  inc -u7200 $+(%,rand_saying,%rand_saying)
  if $($+(%,rand_saying,%rand_saying),2) > 10 {
    goto rand_saying
  }
  else {
    .msg $1 $read(rand_saying.txt,%rand_saying)
  }
}

Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
As has been told multiple times, you don't need to pass $chan to an alias from that event. And a while is a little better than using IF + GOTO.

To the OP...

Code:
on me:*:join:#: {
  .timerRandSaying 0 7200 msg $chan $!read(random.txt)
}


That will send a random line of text from the text file once every 12 minutes (720 seconds). That would be 10 times in 2 hours. If you want it done more often, you can make it more complicated by tracking every line, which is what RussellB was showing...

Code:
on me:*:join:#: {
  .timer 0 300 RandomText
}

alias RandomText {
  var %line = $rand(1,$lines(Random.txt))
  while ($($+(%,Random.,%line),2) >= 10) {
    var %line = $rand(1,$lines(Random.txt))
  }
  inc -u7200 %Random. $+ %line
  msg $chan $read(Random.txt,%line)
}


Note that the on JOIN is optional. You can just use the alias whenever you want with /RandomMessage . If you want to adjust the timer in the second one, change the 300 to whatever you want. 300 = every 5 minutes.

NOTE: It is very important when using either RussellB's code or my second code that your text file containing the random lines has a decent number of lines of text in it. If you only have one or two lines, it's possible to loop over and over because you're reading the same line multiple times. This really won't be much of an issue if you use the timer set to 5 minutes if you have at least 2 lines because no line will be used more than 10 times except unless 10 out of 12 times one of the two lines are used, which will be rare. If you only have 1 line, it will be an endless loop after the 10th display. It will really become an issue if you just use the alias or lower the timer delay because you can display messages a lot of times within 2 hours and if you use up your lines, it will be an endless loop. If you have at least 10 lines, you shouldn't have to worry unless you really get carried away using the alias. smile


Invision Support
#Invision on irc.irchighway.net
Joined: Dec 2002
Posts: 2,031
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Dec 2002
Posts: 2,031

Originally Posted By: Riamus2

As has been told multiple times, you don't need to pass $chan to an alias from that event.


Actually, with the timer you would have to pass it because the script will be done processing and the $chan $nick etc. info will no longer be available when the timer executes and the alias is called.

Code:

on *:TEXT:*:#: {
  if (!$timer(parmtest)) timerparmtest 10 1 parmtest
}
alias parmtest echo -a $nick $chan $1- <-- No info



Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
As RoCk said, $chan will be $null when the timer fires, so you'd have to pass it in the alias, except not in the way you and RussellB showed. Once more (this has been pointed out to both of you more times than it should), you should not pass unknown content to /timer, including $chan. Channel names can have special characters like % and $, and a string of the form #$something evaluates to $something, if the latter is a built-in identifier. If $something is a custom identifier, it won't be called by #$something (possibly a (weak) security precaution), but it will be called by #$($something) (still a valid channel name). Now imagine the situation where $something is a custom identifier that does something potentially dangerous (eg delete files)...

You should both really burn this issue into your minds; posting insecure code over and over has to stop. The fact that #$something works like that has been mentioned a few times in these forums. You know by now what to do to write safe code (eg use the $safe alias I posted the last time this issue came up).

There are also a couple of less serious issues.

First, $lines(file.txt) should be stored in a variable, then that variable should be used inside the loop. This is important in this case, because you don't know for sure how many times it will loop, so having mirc calculate the number of lines (a slow operation on moderately large files) each time will certainly cause problems.

Another problem is the /inc -u method, which can be horribly inaccurate. Each time /inc -u is called, it restarts the 2-hour timer. Consider this example: line 4 happens to be picked once every, say, 13 times (starting from the first call). This means that every 13*300 (the latter being the /timer interval), ie 6900 seconds, the -u timer for %Random.4 would be reset just before it expired (more precisely, 5 minutes before). This way %Random.4 would reach the value 10 in 6900*10 secs, ie in about 19 hours, and the script would skip it (although it obviously shouldn't). This would exacerbate the not-enough-lines problem you described, by unnecessarily skipping line 4. A more accurate way would be to /set -u7200 %Random.<line> 1 if the variable doesn't exist, otherwise /inc it (without -u).


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
I never said or implied that the code I posted was perfect or without possible flaws. If you people are going to always get on my back for trying to help, then maybe I should just stop trying.

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
He's not "getting on your back", it's just that a small mistake that makes /timer, /scid or /scon code exploitable can open up an entire backdoor into your PC so it's a serious issue that we try to raise awareness of. Unfortunately that means that helpers have to deal with being told about it from time to time.


Link Copied to Clipboard