-
Notifications
You must be signed in to change notification settings - Fork 927
/
calc.go
43 lines (36 loc) · 922 Bytes
/
calc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package calc
import (
"fmt"
"strings"
"sync"
"github.com/alfredxing/calc/compute"
"github.com/jonas747/dcmd"
"github.com/jonas747/yagpdb/commands"
)
var (
// calc/compute isnt threadsafe :'(
computeLock sync.Mutex
)
var replacer = strings.NewReplacer("x", "*", "×", "*", "÷", "/")
var Command = &commands.YAGCommand{
CmdCategory: commands.CategoryTool,
Name: "Calc",
Aliases: []string{"c", "calculate"},
Description: "Calculator 2+2=5",
RunInDM: true,
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "Expression", Type: dcmd.String},
},
RunFunc: func(data *dcmd.Data) (interface{}, error) {
computeLock.Lock()
defer computeLock.Unlock()
toCompute := data.Args[0].Str()
toCompute = replacer.Replace(toCompute)
result, err := compute.Evaluate(toCompute)
if err != nil {
return err, err
}
return fmt.Sprintf("Result: `%f`", result), nil
},
}