This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reminder.go
71 lines (62 loc) · 1.54 KB
/
reminder.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
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"strings"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/whyrusleeping/hellabot"
"github.com/softwareniagara/irc_bot/store"
)
func ReminderNotify(bot *hbot.Bot, s *store.Store, channel string) error {
r, err := s.NextReminder(time.Now())
if err != nil {
if err == sql.ErrNoRows {
return nil
}
return err
}
msg := fmt.Sprintf("%s: reminder: %s", r.Nick, r.Msg)
bot.Notice(channel, msg)
return s.RemoveReminder(r.RowID)
}
func ReminderNotifyLoop(bot *hbot.Bot, s *store.Store, channel string) {
for {
if err := ReminderNotify(bot, s, channel); err != nil {
log.Println(err)
}
time.Sleep(10 * time.Second)
}
}
func ReminderTrigger(s *store.Store) hbot.Trigger {
return hbot.Trigger{
Condition: HasCommand("!remindme"),
Action: func(bot *hbot.Bot, msg *hbot.Message) bool {
if err := s.Authorized(msg.From, store.RoleAdmin, store.RoleRegular); err != nil {
ErrorReply(bot, msg, err)
return true
}
var wait time.Duration
fset := flag.NewFlagSet("remindme", flag.ContinueOnError)
fset.DurationVar(&wait, "wait", 0, "how long to wait before reminding")
if err := ParseFlags(msg, fset); err != nil {
ErrorReply(bot, msg, err)
return true
}
err := s.InsertReminder(&store.Reminder{
Time: time.Now().Add(wait),
Created: time.Now(),
Nick: msg.From,
Msg: strings.Join(fset.Args(), " "),
})
if err != nil {
ErrorReply(bot, msg, err)
return true
}
ReplyTo(bot, msg, "ok")
return true
},
}
}