-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
141 lines (116 loc) · 3.07 KB
/
commands.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
package main
import (
"errors"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"
"github.com/araddon/dateparse"
"github.com/bwmarrin/discordgo"
"github.com/huijaaja42/reminder-bot/model"
)
func addCommand(optionMap map[string]*discordgo.ApplicationCommandInteractionDataOption, user string, channel string) (string, error) {
var text string
var timeString string
if opt, ok := optionMap["text"]; ok {
text = opt.StringValue()
}
if opt, ok := optionMap["time"]; ok {
timeString = opt.StringValue()
}
if text == "" || timeString == "" {
return "", errors.New("error: invalid input")
}
t := time.Now()
if strings.HasPrefix(timeString, "in") {
r := regexp.MustCompile(`in\s*(?P<value>\d+)\s*(?P<unit>s|min|h|d|w|mon|y)`)
match := r.FindStringSubmatch(timeString)
if match == nil {
return "", errors.New("error: invalid time format")
}
var unit string
var valueString string
for i, v := range r.SubexpNames() {
if v == "unit" {
unit = match[i]
}
if v == "value" {
valueString = match[i]
}
}
value, err := strconv.Atoi(valueString)
if err != nil {
return "", errors.New("error: invalid time format")
}
switch unit {
case "s":
t = t.Add(time.Second * time.Duration(value))
case "min":
t = t.Add(time.Minute * time.Duration(value))
case "h":
t = t.Add(time.Hour * time.Duration(value))
case "d":
t = t.AddDate(0, 0, value)
case "w":
t = t.AddDate(0, 0, value*7)
case "mon":
t = t.AddDate(0, 1, value)
case "y":
t = t.AddDate(1, 0, value)
default:
return "", errors.New("error: invalid time format")
}
} else {
var err error
t, err = dateparse.ParseStrict(timeString)
if err != nil {
return "", errors.New("error: invalid time format")
}
}
id, err := box.Put(&model.Reminder{
User: user,
Channel: channel,
Time: t.Unix(),
Text: text,
})
if err != nil {
log.Printf("Database error: %v", err)
return "", errors.New("error: database error")
}
return fmt.Sprintf("Will remind you at <t:%v:f> id: `%v`", t.Unix(), id), nil
}
func removeCommand(id uint64, user string) error {
r, err := box.Get(id)
if err != nil || r == nil || r.User != user {
return errors.New("error: invalid id")
}
err = box.RemoveId(r.Id)
if err != nil {
return errors.New("error: invalid id")
}
return nil
}
func listCommand(user string, channel string) (string, error) {
query := box.Query(model.Reminder_.User.Equals("", false))
query.SetStringParams(model.Reminder_.User, user)
reminders, err := query.Find()
if err != nil {
return "", errors.New("error: database error")
}
list := "**Your scheduled reminders:**\n"
for _, r := range reminders {
if r.Channel == channel {
list += fmt.Sprintf("id: `%v` at <t:%v:f> in this channel:\n> %s\n", r.Id, r.Time, r.Text)
continue
}
st, _ := s.Channel(r.Channel)
if st.Type == discordgo.ChannelTypeDM {
list += fmt.Sprintf("id: `%v` at <t:%v:f> in DM:\n> %s\n", r.Id, r.Time, r.Text)
continue
}
list += fmt.Sprintf("id: `%v` at <t:%v:f> in <#%v>:\n> %s\n", r.Id, r.Time, r.Channel, r.Text)
}
return list, nil
}