mIRC Homepage
Posted By: Malko Lottery problems - 05/02/15 08:32 AM
Hey guys i've been trying to learn programming lately and have set up a points system and some other smaller features for my twitch stream. I've been working on a random lottery feature so every few mins it selects a random viewer and says you have won the random lottery you win 50 points (random number out of selected few) and writes it to the points.ini file. this is what i got so far

Code:
on *:text:!startlottery:#:{
  $+(.timerlottery.,#,.,$nick) 0 60 add.pts $+(#,.,$nick)
  if ($nick isop #) { msg # [Lottery]: $nick(#,$rand(1,$nick(#,0))) You have just won the random lottery }
  else 
  on !*:part:#:$+(.timerlottery.,#,.,$nick) off
  alias -l add.pts {
    writeini -n Points.ini $1 Points $calc($readini(Points.ini,$1,Points /set %rand $rand(1,10) { if (%rand == 1) { /msg $chan You win 5 points } 
      elseif (%rand == 2) { /msg $chan You win 10 points } 
      elseif (%rand == 3) { /msg $chan You win 20 points } 
      elseif (%rand == 4) { /msg $chan You win 30 points } 
      elseif (%rand == 5) { /msg $chan You win 40 points } 
      elseif (%rand == 6) { /msg $chan You win 50 points } 
      elseif (%rand == 7) { /msg $chan You win 60 points } 
      elseif (%rand == 8) { /msg $chan You win 70 points } 
      elseif (%rand == 9) { /msg $chan You win 80 points } 
      elseif (%rand == 10) { /msg $chan You win 90 points } 
  else { /msg $chan You win 150 points } } } 


}


Would rather the lottery start when the bot starts but not sure how to do that.
Thanks malko
Posted By: Belhifet Re: Lottery problems - 05/02/15 10:51 AM
on me:*:JOIN:#: { do stuff }
Posted By: Raccoon Re: Lottery problems - 05/02/15 12:36 PM
This line cannot exist. You have a bunch of lines all cobbled into one in no way that makes sense.

writeini -n Points.ini $1 Points $calc($readini(Points.ini,$1,Points /set %rand $rand(1,10) { if (%rand == 1) { /msg $chan You win 5 points }

Overall your code has a dozen glaring syntactic errors that makes it look like you just copied chunks of code from other people's scripts and pasted them in randomly.

But if you'd like to learn some logic, check this out:

Originally Posted By: Malko
/set %rand $rand(1,10)
if (%rand == 1) { /msg $chan You win 5 points }
elseif (%rand == 2) { /msg $chan You win 10 points }
elseif (%rand == 3) { /msg $chan You win 20 points }
elseif (%rand == 4) { /msg $chan You win 30 points }
elseif (%rand == 5) { /msg $chan You win 40 points }
elseif (%rand == 6) { /msg $chan You win 50 points }
elseif (%rand == 7) { /msg $chan You win 60 points }
elseif (%rand == 8) { /msg $chan You win 70 points }
elseif (%rand == 9) { /msg $chan You win 80 points }
elseif (%rand == 10) { /msg $chan You win 90 points }
else { /msg $chan You win 150 points }


becomes

Code:
var %rand = $rand(1,10)
var %pointlist = 5,10,20,30,40,50,60,70,80,150
var %points = $gettok(%pointlist,%rand,44)
msg $chan $nick wins %points points!


You might notice in your code that if %rand can be a number 1 through 10, there's no possible way for "else" to arrive at 150 points... so I replaced 90 with 150.

Anyway, good luck.

I highly recommend writing your own code from scratch and avoid any copy/pasting from other people's scripts you don't fully understand letter for letter.
Posted By: Malko Re: Lottery problems - 05/02/15 10:20 PM
Thanks for the help! i did some coding a while ago late last year and then its been a while since that so I've been going over what i already done and trying to use the code i used for my points system in this. I'm just struggling to get my head around the part where it picks someone random and gives them a amount of points and tells them how many. I can't find many good places to learn how to program in this language.
Posted By: Malko Re: Lottery problems - 07/02/15 01:17 AM
So i have got it down to this, but its not excuting anything, i feel like i'm missing something important, can anyone see whats wrong?
Code:
on me:*:JOIN:#: {
  $+(.timerlottery.,#,.,$nick) 0 60 add.pts $+(#,.,$nick)
  if ($nick isop #) { msg # [Lottery]: $nick(#,$rand(1,$nick(#,0))) You have just won the random lottery }
  else 
  on !*:part:#:$+(.timerlottery.,#,.,$nick) off
  alias -l add.pts {
    writeini -n Points.ini $1 Points $calc($readini(Points.ini,$1,Points /set %rand $rand(1,10) { if (%rand == 1) { /msg $chan You win 5 points } 
      var %rand = $rand(1,10)
      var %pointlist = 5,10,20,30,40,50,60,70,80,150
      var %points = $gettok(%pointlist,%rand,44)
  msg $chan $nick wins %points points! } } 
Posted By: Wims Re: Lottery problems - 07/02/15 01:53 AM
Why do you have an on part event and the alias add.pts inside the on join event? That would be the important things you would be missing wink which was pointed out by Raccoon, basically, but not explicitely
Posted By: Malko Re: Lottery problems - 07/02/15 02:31 AM
Oh yeah i see that now! so should i have something there instead? because i've taken it out and nothing has changed, i feel like the timer isn't working properly
Posted By: Wims Re: Lottery problems - 07/02/15 02:39 AM
It's impossible to tell without seeing the code, and I couldn't really tell what you really wanted to do so I didn't assume anything.
Btw, /timer can be dangerous, as it is right now you can be exploited, read this page http://en.wikichip.org/wiki/mirc/msl_injection
Posted By: Malko Re: Lottery problems - 07/02/15 02:42 AM
Do you have a safer way of doing it?
Code:
on me:*:JOIN:#: {
  $+(.timerlottery.,#,.,$nick) 0 60 add.pts $+(#,.,$nick)
  if ($nick isop #) { msg # [Lottery]: $nick(#,$rand(1,$nick(#,0))) You have just won the random lottery }
  else 
  {
    writeini -n Points.ini $1 Points $calc($readini(Points.ini,$1,Points /set %rand $rand(1,10) { if (%rand == 1) { /msg $chan You win 5 points } 
      var %rand = $rand(1,10)
      var %pointlist = 5,10,20,30,40,50,60,70,80,150
      var %points = $gettok(%pointlist,%rand,44)
  msg $chan $nick wins %points points! } } 


}
Posted By: Wims Re: Lottery problems - 07/02/15 03:11 AM
Well this script is non sense, it's triggering only for when you (your bot) join the channel.
Can you explain clearly what you want the script to do?
Posted By: Malko Re: Lottery problems - 07/02/15 03:17 AM
Ok, So what i want it to do is when the bot joins the stream it starts a loop with a timer so like every 5 mins it will select a random viewer and send them a message "you won the lottery you get ** points" and give them a randomized amount of points. Does that make sense? thanks for helping
Posted By: Wims Re: Lottery problems - 07/02/15 03:40 AM
Yes, however, you cannot check if you are op in an on join event because you're never op when you join a channel, the technical correct way to do this is to set a variable to 1 when you join, and then use an on op event, checking if you are the one getting the op and then you check if the variable is set (unset that variable whenever you get opped anyway). From that point, start the timer:

$+(.timerlottery.,#) 0 300 lottery $safe(#)

Then, make the alias lottery:

Code:
alias lottery {
 var %winner $nick($1,$r(1,$nick($1,0)))
 var %point $gettok(5,10,20,30,40,50,60,70,80,150,$r(1,10),44)
 var %inisection $+($1,.,%winner)
 writeini -n Points.ini %inisection Points $calc($readini(Points.ini,%inisection,Points) + %point)
 msg $1 %winner wins %point points!
}
Posted By: Malko Re: Lottery problems - 07/02/15 04:25 AM
So when the bot joins this should start, and run the code where it picks a random person, but it doesn't do anything.
Code:
on me:*:JOIN:#: {
  $+(.timerlottery.,#) 0 60 lottery $safe(#)
  {
    alias lottery {
      var %winner $nick($1,$r(1,$nick($1,0)))
      var %point $gettok(5,10,20,30,40,50,60,70,80,150,$r(1,10),44)
      var %inisection $+($1,.,%winner)
      writeini -n Points.ini %inisection Points $calc($readini(Points.ini,%inisection,Points) + %point)
      msg $1 %winner wins %point points!
    }
  }
}
Posted By: Wims Re: Lottery problems - 07/02/15 04:36 AM
We just went over how you cannot have a an alias inside an event...
And I just went over how you should be setting a variable on join and waiting for the on op event to trigger etc.


Code:
on me:*:join:#:set %justjoined $addtok(%justjoined,$chan,32)
on me:*:part:#:set %justjoined $remtok(%justjoined,$chan,32) | .timerlottery. $+ $chan off
on *:disconnect unset %justjoined | .timerlottery.* off
on *:op:#:{
 if ($opnick == $me) {
   if ($istok(%justjoined,$chan,32)) {
     $+(.timerlottery.,$chan) 0 60 lottery $safe($chan)
   }
   set %justjoined $remtok(%justjoined,$chan,32)
 }
}
alias lottery {
 var %winner $nick($1,$r(1,$nick($1,0)))
 var %point $gettok(5 10 20 30 40 50 60 70 80 150,$r(1,10),32)
 var %inisection $+($1,.,%winner)
 writeini -n Points.ini %inisection Points $calc($readini(Points.ini,%inisection,Points) + %point)
 msg $1 %winner wins %point points!
}
alias safe return $!decode( $encode($1-, m) ,m)
The $safe alias is required to prevent the /timer command from double evaluating the $chan value, which could result in an exploit (the value of $chan is not known since it can be any channel), read more here http://en.wikichip.org/wiki/mirc/msl_injection

This is untested but should work
Posted By: Malko Re: Lottery problems - 07/02/15 05:12 AM
Sorry i'm having trouble learning this language, its hard. I see what you meant by setting another variable though.
I read about the $safe and that's interesting. But i tested it and still didn't work, not outputting anything, its got the %justjoined in the variables though.
Posted By: Wims Re: Lottery problems - 07/02/15 05:27 AM
I edited my previous post, it had an error with the list of different number of points (I blame Raccoon and his commas for this grin )
The line to update is the one with $gettok, change to:
Code:
 var %point $gettok(5 10 20 30 40 50 60 70 80 150,$r(1,10),32)
Posted By: Malko Re: Lottery problems - 07/02/15 05:39 AM
Whoops! ok changed that! but still not outputting anything, so it should pick a random user every 60 seconds right?
Posted By: raycomp Re: Lottery problems - 07/02/15 11:51 AM
maybe post your full script, updated with the suggestions already made to you. That way maybe you will get better advise on the whole script
Posted By: Wims Re: Lottery problems - 07/02/15 04:29 PM
I just tested the script, which works as expected, you may have conflicting scripts, I suggest putting this new one on a new remote file
Posted By: Malko Re: Lottery problems - 07/02/15 09:53 PM
Ok i went through and made some changes to the other script, here is the whole thing, lottery at the end. http://pastebin.com/KsHZmRfY
Thanks for helping
Posted By: Wims Re: Lottery problems - 08/02/15 02:02 AM
I don't see any conflict, it should work, do you get any error in the status window? Did you try to debug the code a bit?
Posted By: Majeye Re: Lottery problems - 11/02/15 06:22 PM
Originally Posted By: Malko
Thanks for the help! i did some coding a while ago late last year and then its been a while since that so I've been going over what i already done and trying to use the code i used for my points system in this. I'm just struggling to get my head around the part where it picks someone random and gives them a amount of points and tells them how many. I can't find many good places to learn how to program in this language.


mSL isn't a common language for coding Twitch bots (even though it is growing drastically for some odd reason). I'd recommend Python. (It's also an easier language to learn).

My small piece of advice is anytime you have a new script with an alias, put that entire script in a new remote file. Don't add it in with others, they will conflict at some point.

If you insist on using mSL, read the comments above, they are very helpful.
Posted By: westor Re: Lottery problems - 11/02/15 07:41 PM
Originally Posted By: Majeye
Originally Posted By: Malko
Thanks for the help! i did some coding a while ago late last year and then its been a while since that so I've been going over what i already done and trying to use the code i used for my points system in this. I'm just struggling to get my head around the part where it picks someone random and gives them a amount of points and tells them how many. I can't find many good places to learn how to program in this language.


mSL isn't a common language for coding Twitch bots (even though it is growing drastically for some odd reason). I'd recommend Python. (It's also an easier language to learn).

My small piece of advice is anytime you have a new script with an alias, put that entire script in a new remote file. Don't add it in with others, they will conflict at some point.

If you insist on using mSL, read the comments above, they are very helpful.


Who told you that mSL is not? with mIRC Scripting Lanugage you can create everything you want, just you need to learn about to how-to, what is that you cannot create with it?
Posted By: Malko Re: Lottery problems - 11/02/15 10:38 PM
So when putting them in different remote files do i just create each one in the folder, and does it matter what they are called and do they have to be linked somehow?
Because i still can't get the lottery script going?
© mIRC Discussion Forums