/
timers.go
237 lines (204 loc) · 4.82 KB
/
timers.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package fifth
import (
"encoding/json"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/eddbc/fifth-bot/mux"
"github.com/eddbc/fifth-bot/storage"
"go.etcd.io/bbolt"
"log"
"sort"
"strconv"
"time"
)
type timer struct {
ID int `json:"id"`
Time time.Time `json:"time"`
Description string `json:"description"`
Pinged bool `json:"pinged"`
}
func (t *timer) toStr() string {
loc, _ := time.LoadLocation("Atlantic/Reykjavik")
_, _, d, h, m, _ := diff(time.Now().In(loc), t.Time)
return fmt.Sprintf("%v - %v (%vd %vh %vm) [%v]", t.Description, t.Time.Format("Jan 2, 15:04"), d, h, m, t.ID)
}
//AddTimer Bot command to add a timer to the timer board
func (f *Fifth) AddTimer(ds *discordgo.Session, dm *discordgo.Message, ctx *mux.Context) {
desc := ""
d := 0
h := 0
m := 0
var err error
for k, v := range ctx.Fields {
if k == 1 {
d, err = removeTrailingChar(v)
}
if k == 2 {
h, err = removeTrailingChar(v)
}
if k == 3 {
m, err = removeTrailingChar(v)
}
if k > 3 {
desc += v
if k+1 < len(ctx.Fields) {
desc += " "
}
}
if err != nil {
log.Println("Invalid Timer")
SendMsgToChan(dm.ChannelID, "Invalid Time Given")
return
}
}
if d > 16 || h > 23 || m > 59 {
log.Println("Invalid Timer")
SendMsgToChan(dm.ChannelID, "Invalid Time Given")
return
}
if desc == "" {
log.Println("Invalid Timer")
SendMsgToChan(dm.ChannelID, "Invalid Description Given")
return
}
loc, _ := time.LoadLocation("Atlantic/Reykjavik")
then := time.Now().In(loc).Add(
time.Hour*time.Duration(24*d) +
time.Hour*time.Duration(h) +
time.Minute*time.Duration(m))
s := fmt.Sprintf("Set timer for %v with description \"%s\"", then.Format("Jan 2, 15:04"), desc)
log.Println(s)
SendMsgToChan(dm.ChannelID, s)
saveTimer(timer{Time: then, Description: desc, Pinged: false})
}
//ListTimers bot command to return a list of current timers
func (f *Fifth) ListTimers(ds *discordgo.Session, dm *discordgo.Message, ctx *mux.Context) {
var timers []timer
storage.DB.View(func(tx *bbolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte(storage.TimersKey))
b.ForEach(func(k, v []byte) error {
r := timer{}
json.Unmarshal(v, &r)
timers = append(timers, r)
return nil
})
return nil
})
sort.Slice(timers, func(i, j int) bool {
return timers[i].Time.Before(timers[j].Time)
})
resp := "```\n"
if len(timers) == 0 {
resp += "No Timers"
}
for _, timer := range timers {
resp += timer.toStr() + "\n\n"
}
resp += "```"
log.Println(resp)
SendMsgToChan(dm.ChannelID, resp)
//SendMsgToChan(dm.ChannelID, resp)
}
// RemoveTimer bot command to remove a timer from the timer board
func (f *Fifth) RemoveTimer(ds *discordgo.Session, dm *discordgo.Message, ctx *mux.Context) {
i, err := strconv.Atoi(ctx.Fields[1])
SendMsgToChan(dm.ChannelID, fmt.Sprintf("Deleting timer [%v]...", i))
if err != nil {
return
}
storage.DB.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(storage.TimersKey))
b.Delete(storage.Itob(i))
return nil
})
}
func timerCron() {
storage.DB.Update(func(tx *bbolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte(storage.TimersKey))
b.ForEach(func(k, v []byte) error {
timer := timer{}
json.Unmarshal(v, &timer)
if !timer.Pinged {
then := time.Now().Add(30 * time.Minute)
if timer.Time.Before(then) {
SendMsgToChan(timerChannel, "@here Timer Warning : "+timer.toStr())
timer.Pinged = true
bytes, err := json.Marshal(timer)
if err == nil {
b.Put(k, bytes)
}
}
}
if timer.Time.Before(time.Now()) {
log.Printf("Timer has expired, deleting : < %v >", timer.toStr())
b.Delete(k)
}
return nil
})
return nil
})
}
func saveTimer(t timer) {
storage.DB.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(storage.TimersKey))
if b == nil {
return nil
}
id, _ := b.NextSequence()
t.ID = int(id)
buf, _ := json.Marshal(t)
b.Put(storage.Itob(t.ID), buf)
return nil
})
}
func removeTrailingChar(s string) (int, error) {
if last := len(s) - 1; last >= 0 {
s = s[:last]
}
i, err := strconv.Atoi(s)
return i, err
}
func diff(a, b time.Time) (year, month, day, hour, min, sec int) {
if a.Location() != b.Location() {
b = b.In(a.Location())
}
if a.After(b) {
a, b = b, a
}
y1, M1, d1 := a.Date()
y2, M2, d2 := b.Date()
h1, m1, s1 := a.Clock()
h2, m2, s2 := b.Clock()
year = int(y2 - y1)
month = int(M2 - M1)
day = int(d2 - d1)
hour = int(h2 - h1)
min = int(m2 - m1)
sec = int(s2 - s1)
// Normalize negative values
if sec < 0 {
sec += 60
min--
}
if min < 0 {
min += 60
hour--
}
if hour < 0 {
hour += 24
day--
}
if day < 0 {
// days in month:
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
day += 32 - t.Day()
month--
}
if month < 0 {
month += 12
year--
}
return
}