forked from johansundell/cocbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlastrecive.go
More file actions
62 lines (56 loc) · 1.27 KB
/
lastrecive.go
File metadata and controls
62 lines (56 loc) · 1.27 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"context"
"fmt"
"time"
)
type receive struct {
donations
}
func (v receive) String() string {
return fmt.Sprintf("%s got %d troops %s ago\n", v.name, v.amount, (time.Duration(v.min) * time.Second).String())
}
func init() {
key := commandFunc{"!what to name 2", "To list the last 10 to receive troops", "", categoryStats}
lockMap.Lock()
defer lockMap.Unlock()
botFuncs[key] = func(ctx context.Context, command string) (string, error) {
if key.command == command {
don, err := getReceive(10)
if err != nil {
return "", err
}
msg := ""
for _, v := range don {
msg += fmt.Sprintf("%s got %d troops %s ago\n", v.name, v.amount, (time.Duration(v.min) * time.Second).String())
}
return msg, nil
}
return "", nil
}
}
var queryRecive = `
SELECT
(r.current - r.prev) AS diff,
m.name,
TIME_TO_SEC(TIMEDIFF(NOW(), r.ts)) AS since
FROM
receive r
JOIN
members m ON m.member_id = r.member_id
ORDER BY r.receive_id DESC
LIMIT 0 , ?
`
func getReceive(numToFetch int) ([]receive, error) {
rows, err := db.Query(queryRecive, numToFetch)
if err != nil {
return nil, err
}
don := []receive{}
for rows.Next() {
d := receive{}
rows.Scan(&d.amount, &d.name, &d.min)
don = append(don, d)
}
return don, nil
}