mIRC Homepage
Posted By: killagod47 Way to only read whole numbers? - 28/07/15 03:42 PM
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
}
Posted By: Wims Re: Way to only read whole numbers? - 28/07/15 03:53 PM
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)) {
Posted By: killagod47 Re: Way to only read whole numbers? - 28/07/15 03:57 PM
Thanks man, this works perfectly!
Posted By: Plornt Re: Way to only read whole numbers? - 28/07/15 08:42 PM
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.
Posted By: Wims Re: Way to only read whole numbers? - 28/07/15 08:59 PM
Indeed, in this case $int should be used, solving the decimal issue
Posted By: Plornt Re: Way to only read whole numbers? - 28/07/15 09:12 PM
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.
© mIRC Discussion Forums