forked from johansundell/cocbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfun.go
More file actions
50 lines (47 loc) · 1023 Bytes
/
fun.go
File metadata and controls
50 lines (47 loc) · 1023 Bytes
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
package main
import (
"context"
"fmt"
"time"
)
func init() {
key := commandFunc{"!game", "Lets play a game", "", categoryFun}
lockMap.Lock()
defer lockMap.Unlock()
botFuncs[key] = func(ctx context.Context, command string) (string, error) {
if command == key.command {
s, m, err := getSessionsAndMessageFromContext(ctx)
if err != nil {
return "", err
}
n := 100
str := "Formatting main hard drive %d%% left"
msg, err := s.ChannelMessageSend(m.ChannelID, fmt.Sprintf(str, n))
if err != nil {
return "", err
}
ticker := time.NewTicker(1 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
if n == 0 {
ticker.Stop()
s.ChannelMessageDelete(m.ChannelID, msg.ID)
}
n = n - 5
msg, err = s.ChannelMessageEdit(m.ChannelID, msg.ID, fmt.Sprintf(str, n))
if err != nil {
return
}
case <-quit:
ticker.Stop()
return
}
}
}()
}
return "", nil
}
}