mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jul 2015
Posts: 4
K
Self-satisified door
OP Offline
Self-satisified door
K
Joined: Jul 2015
Posts: 4
I have a code for my twitch bot that gives the top 1-10 users with the most points in chat. As always people try to break the bot and have started doing commands like !top 4.5345454 The code will still show the top 4 but it will repeat "the top 4.5345454 users are..."
Is there a way to only allow it to read whole numbers because when people make very long numbers after the decimal it makes the chat quite spammy.


The code is
Code:
on *:text:!top*:#: {
  if ($2 < 1 || $0 < 2) {
    msg $chan /me $nick to use the !top command you must specify a positive number.
  }
  elseif ($2 > 10) {
    msg $chan /me $nick you cannot specify a number greater than 10.
  }
  else {
    var %ID = $ini(%rosterFile, 0)
    if ($2 > %ID) {
      msg $chan /me $nick there are only %ID people in the level system.
    }
    else {
      write -c %topFile

      var %topList = The $2 highest level users:
      var %i = 1
      while (%i <= $2) {
        $getTopID()
        var %name = $readini(%topFile, %i, name)
        var %points = $readini(%topFile, %i, levels)
        if (%i != $2) {
          %topList = $+(%topList, $chr(32), %name, $chr(32), %points, $chr(44))
        }
        else {
          %topList = $+(%topList, $chr(32), %name, $chr(32), %points)
        }
        inc %i
      }
      msg $chan /me %topList
    }
  }
}

// ------------------------------------------------------------------------------------------------------------------------

// Format: $getTopID()
// Gets the ID with the highest number of points that is not already in the temp file
alias -l getTopID {
  var %rosterID = $ini(%rosterFile, 0)
  var %topID = $ini(%topFile, 0)
  var %highestPoints = 0
  var %highestID = -1

  var %i = 1
  while (%i <= %rosterID) {
    var %points = $readini(%rosterFile, %i, levels)
    if (%points >= %highestPoints) {
      var %topNameFound = $false
      var %j = 1
      while (%j <= %topID) {
        var %name = $readini(%rosterFile, %i, name)
        var %topName = $readini(%topFile, %j, name)
        if (%name == %topName) {
          %topNameFound = $true
          break
        }
        inc %j
      }
      if (%topNameFound == $false) {
        %highestPoints = %points
        %highestID = %i
      }
    }
    inc %i
  }

  var %highestName = $readini(%rosterFile, %highestID, name)
  writeini -n %topFile $calc(%topID + 1) name %highestName
  writeini -n %topFile $calc(%topID + 1) levels %highestPoints
}

Joined: Jul 2006
Posts: 3,945
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,945
Check that the parameter is a whole number, meaning there is only digit in it, regex can quickly do that, you basically only want to accept digit from 1 to 9, included:
Code:
if (!$regex($2,^[1-9]$)) {
    msg $chan /me $nick to use the !top command you must specify a positive integer.
  }
Without regex, you could check that with $istok:
Code:
if (!$istok(1 2 3 4 5 6 7 8 9,$2,32)) {


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jul 2015
Posts: 4
K
Self-satisified door
OP Offline
Self-satisified door
K
Joined: Jul 2015
Posts: 4
Thanks man, this works perfectly!

Joined: May 2010
Posts: 29
P
Ameglian cow
Offline
Ameglian cow
P
Joined: May 2010
Posts: 29
Originally Posted By: Wims
Check that the parameter is a whole number, meaning there is only digit in it, regex can quickly do that, you basically only want to accept digit from 1 to 9, included:
Code:
if (!$regex($2,^[1-9]$)) {
    msg $chan /me $nick to use the !top command you must specify a positive integer.
  }
Without regex, you could check that with $istok:
Code:
if (!$istok(1 2 3 4 5 6 7 8 9,$2,32)) {


To add to this for future viewers if you wish to allow numbers greater than 1-9 without hardcoding a ton of number tokens (without using regex) you can use:

Code:
if ($round($2,0) == $2) { }


Which will ensure that your number is a whole number. That being said it will still allow you to do things like: "1.00000000" at which point you can just used the rounded output instead of using $2.

Joined: Jul 2006
Posts: 3,945
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,945
Indeed, in this case $int should be used, solving the decimal issue


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: May 2010
Posts: 29
P
Ameglian cow
Offline
Ameglian cow
P
Joined: May 2010
Posts: 29
Originally Posted By: Wims
Indeed, in this case $int should be used, solving the decimal issue


Oops yep thats a better method. I should probably read through the identifier list at some point.


Link Copied to Clipboard