Author Topic: Random script  (Read 2915 times)

Morio

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 256
Random script
« on: January 18, 2008, 08:49:11 AM »
This is a script that randomly choses one of a number of alternatives given to it. I saw a script like this on a channel and it seemed to be quite popular so I decided to try and make one myself too. I also did this to learn about arrays and what you can do with them.

I hope you find use for this script

Code: [Select]
Sub ONTEXT(Message,Channel,Nickname,Host,ServerNumber)

if len(message) > 8 then
  'check for the command
  if LCase(left(Message,7)) = "!decide" then

      'strip the command from the arguments
      Choices = Mid(Message,9)

      'separate the arguments and put them in an array
      a = Split(Choices,",")
      n = UBound(a)

      'check if you get more than one argument
      If n > 0 then
      Valid = True

        'check that none of the arguments are empty
        For Check = 0 to n
          If Len(a(n)) = 0 then Valid = False
        Next

        'randomly chose one of the arguments
        If Valid = True then
          Randomize
          'randomly chose a value between 0 and n+1 to give all arguments the same probability
          Number = rnd * (n+1)

          'in the extremely rare case that the rnd returns exatly n+1 set the number to n
          If Number = n+1 then Number = Number = n
            Number = Int(Number)
              SendCommand "/msg " & Channel & " " & Nickname & ": " & a(number),ServerNumber
        End If
       
        'if one of the arguments is empty tell the person that the argument isn't valid
        If Valid = False then SendCommand "/msg " & Channel & " " & Nickname & ": not a valid argument",ServerNumber

      End If

      'if only one argument is given this message gets posted
      If n = 0 then SendCommand "/msg " & channel & " " & Nickname & ": I'm sure you can decide that by yourself if you think really hard",Servernumber
 
  End If
End If
End Sub

The script gets triggered when someone starts a message with !decide, and the arguments are separated by commas. For example '!decide to be, not to be'

Note that this script doesn't trigger for yourself
« Last Edit: January 18, 2008, 08:51:05 AM by Morio »
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein

Omni_Bowser

  • Newbie
  • *
  • Posts: 13
Re: Random script
« Reply #1 on: February 17, 2008, 12:41:59 PM »
I love this script LoL... Is there a way by chance to make it so I can also activate the script?