-
Notifications
You must be signed in to change notification settings - Fork 91
/
chat_action_manager.go
62 lines (55 loc) · 1.35 KB
/
chat_action_manager.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
log "github.com/sirupsen/logrus"
"sync"
"time"
)
type ChatAction struct {
tgbotapi.ChatActionConfig
Count int
}
type ChatActionManager struct {
ChatActionList []ChatAction
Lock sync.Mutex
}
func (cal *ChatActionManager) ActionLoop() {
for {
cal.Lock.Lock()
var err error
for i := range cal.ChatActionList {
_, err = Bot.Send(cal.ChatActionList[i])
if err != nil {
log.Errorf("Failed update chat action for %d; %s", cal.ChatActionList[i], err)
}
}
cal.Lock.Unlock()
time.Sleep(time.Second*2)
}
}
func (cal *ChatActionManager) AddAction(ca tgbotapi.ChatActionConfig) {
cal.Lock.Lock()
defer cal.Lock.Unlock()
for i := range cal.ChatActionList {
if cal.ChatActionList[i].ChatID == ca.ChatID {
cal.ChatActionList[i].Count++
return
}
}
cal.ChatActionList = append(cal.ChatActionList, ChatAction{ca, 1})
}
func (cal *ChatActionManager) DelAction(chatID int64) {
cal.Lock.Lock()
defer cal.Lock.Unlock()
for i := range cal.ChatActionList {
if cal.ChatActionList[i].ChatID == chatID {
if cal.ChatActionList[i].Count <= 1 {
cal.ChatActionList[i] = cal.ChatActionList[len(cal.ChatActionList)-1]
cal.ChatActionList = cal.ChatActionList[:len(cal.ChatActionList)-1]
} else {
cal.ChatActionList[i].Count--
}
return
}
}
}