Commands: !calc .
Feel free to comment.
$comma() Alias made by: Fibre0Ptics
alias -l comma {
var %a, %b = $regsub($ticks,$1,/\G([+-]?\d+?)(?=(?:\d{3})++(?=\.\d++$|$))/g,\1 $+ $chr(44),%a)
return %a
}
on $*:text:/^!calc\s(.*)/iS:#:{
if (!$2-) msg # I can't calculate nothing $nick $+ ...
elseif ($chr(37) isin $1-) msg # You cannot use percent in this calculator, sorry.
elseif ($remove($2-,k,m,b,$chr(44),$chr(32),.,+,-,/,*,^) !isnum) msg # Sorry $nick $+ , I can't calculate words.
else {
var %calc $calc($replace($remove($2-,$chr(32),$chr(36),$chr(44)),k,*1000,m,*1000000,b,*1000000000)))
msg # $+([,!calc,]) Calculating: $2-
msg # $+([,!calc,]) The Result: $comma($calc(%calc))
}
}
It's best you use $wildsite and local variables like so:
on $*:text:/^(!|@)Calc( |$)/Si:#:{
var %n = notice $nick, %m = $iif($regml(1) == !,%n,msg #), %2 = $strip($replace($2-,x,*))
if (!$ignore($wildsite)) {
if (!$strip($2)) {
%n please use !calc <calculation>
}
else {
%m Calculating: %2
%m Result is: $calc(%2)
.ignore -u4 $wildsite
}
}
}
Edit - I see what you're getting at. You only want the trigger to ignore for the else condition. Then it's best you notice the $nick to check for $2, so that it doesn't appear flood-y if messaged in the channel when someone messes with you.
I've taken the liberty of making a couple of adjustments.
The whole purpose of the original snippet is for simple calculations. I doubt the author has thought up a prudent calculation snippet as "fancy" as what you have demonstrated here. Let's not get into the matter of tampering with its fallacy by "digging more holes" :p Nothing is 100% perfect.; perfection is far too subjective.
Bielie, you can use if ($1 isnum 1000000-999999999), also your script will fail if the calculation includes spaces. Remove chr(32). And you are only calling %output once,
$iif($left($1,1) == @,/msg #,/notice $nick) $num(%calc) $iif($short(%scalc),$v1,$chr(32)) would work. You can even leave the $chr(32), just $iif($short(%scalc),$v1)
#calc on
on $*:TEXT:/^[!.@]calc(ulate)?\w/Si:#: {
if (!$2) { notice $nick Please use the correct syntax: !calc <calculation> | halt }
if ($regex($1-,/[^1-9][mbkt]/i)) { notice $nick Invalid expression | halt }
if ($regex($remove($2-,m,b,k,t),/\D/)) { notice $nick Invalid expression | halt }
var %calc $calc($replace($remove($2-,$chr(32)), :, /, x, *,t,*1000b,b,*1000m,m,*1000m,k,*1000))
$iif($left($1,1) == @,/msg #,/notice $nick) 4=2 $bytes(%calc,bd) $iif($short(%scalc),$chr(40) $+ $v1),)
.ignore -u3 $nick
}
alias short {
if ($1 < 0) { scon 0 return - $!+ $!short( $abs($1) ) }
if ($1 isnum 1000-999999) { return 3( $+ $round($calc($1- / 1000),2) $+ k $+ 3) }
if ($1 isnum 1000000-999999999) { return 3( $+ $round($calc($1- / 1000000),2) $+ m $+ 3) }
if ($1 isnum 1000000000-999999999999) { return 3( $+ $round($calc($1- / 1000000000),2) $+ b $+ 3) }
if ($1 >= 1000000000000) { return 3( $+ $round($calc($1- / 1000000000000),2) $+ t $+ 3) }
}
#calc end
Not waterproof but works in most cases
You should use something that works with more characters: This is mine ;)
#calc on
on $*:TEXT:/^[!.@]calc|calculate/Si:#: {
if (!$2) { notice $nick Please use the correct syntax: !calc <calculation> | halt }
var %output $iif($left($1,1) == @,/msg #,/notice $nick)
var %scalc $calc($2-)
var %calc $calc($replace($2-, :, /, x, *))
%output $2- 4=2 $num(%calc) $iif($short(%scalc),$v1,$chr(32))
.ignore -u3 $nick
}
alias num { return $bytes($1-,bd) }
alias short {
if ($1 < 0) { scon 0 return - $!+ $!short( $abs($1) ) }
if ($1 >= 1000) && ($1 <= 999999) { return 3( $+ $round($calc($1- / 1000),2) $+ k $+ 3) }
if ($1 >= 1000000) && ($1 < 999999999) { return 3( $+ $round($calc($1- / 1000000),2) $+ m $+ 3) }
if ($1 >= 1000000000) && ($1 < 999999999999) { return 3( $+ $round($calc($1- / 1000000000),2) $+ b $+ 3) }
if ($1 >= 1000000000000) && ($1 < 999999999999999) { return 3( $+ $round($calc($1- / 1000000000000),2) $+ t $+ 3) }
}
#calc end
No, it ain't the same. There's no such word as "rusult." It's a misspelling. That said, you should add a trigger protection to your code to safeguard yourself from being flooded out if someone's gonna screw you. You'll be screwed without it. Last but not least, leave a space between > !calc *so the code doesn't get triggered for an empty value if the $2- aren't filled.