-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_time.go
123 lines (101 loc) · 2.96 KB
/
cmd_time.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
package bot
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/api/cmdroute"
"github.com/holedaemon/bot2/internal/db/modelsx"
"github.com/zikaeroh/ctxlog"
"go.uber.org/zap"
)
func (b *Bot) cmdTimeIn(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
tz := data.Options.Find("timezone").String()
if tz == "" {
id := data.Event.SenderID()
if id == 0 {
ctxlog.Error(ctx, "sender id is 0")
return respondError("An unexpected error has occurred, oops!")
}
p, err := modelsx.FetchUserProfile(ctx, b.db, id.String())
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return respondError("You gotta give me a timezone to work with")
}
ctxlog.Error(ctx, "error fetching user profile")
return dbError
}
if !p.Timezone.Valid {
return respondError("You gotta give me a timezone to work with")
}
tz = p.Timezone.String
}
loc, err := time.LoadLocation(tz)
if err != nil {
return respondError("Unable to parse given timezone. Are you sure it's real?")
}
now := time.Now().In(loc)
return respondf(
"It is %s in `%s`",
now.Format("January 2, 2006 03:04 PM"),
tz,
)
}
var formatMap = map[string]string{
"t": "<t:%d:t>",
"T": "<t:%d:T>",
"d": "<t:%d:d>",
"D": "<t:%d:D>",
"f": "<t:%d:f>",
"F": "<t:%d:F>",
"R": "<t:%d:R>",
}
func (b *Bot) cmdTimeStamp(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
t := data.Options.Find("time").String()
tz := data.Options.Find("timezone").String()
format := data.Options.Find("format").String()
switch format {
case "t", "T", "d", "D", "f", "F", "R":
case "":
format = "t"
default:
return respondError("Invalid format, valid options are: t, T, d, D, f, F, R. See <https://discord.com/developers/docs/reference#message-formatting-timestamp-styles> for more info")
}
if tz == "" {
id := data.Event.SenderID()
if id == 0 {
ctxlog.Error(ctx, "sender id is 0")
return respondError("An unexpected error has occurred, oops!")
}
p, err := modelsx.FetchUserProfile(ctx, b.db, id.String())
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return respondError("You gotta give me a timezone to work with")
}
ctxlog.Error(ctx, "error fetching user profile")
return dbError
}
if !p.Timezone.Valid {
return respondError("You gotta give me a timezone to work with")
}
tz = p.Timezone.String
}
loc, err := time.LoadLocation(tz)
if err != nil {
return respondError("Unable to parse given timezone. Are you sure it's real?")
}
now := time.Now().In(loc)
res, err := b.when.Parse(t, now)
if err != nil {
ctxlog.Error(ctx, "error parsing time", zap.Error(err))
return respondError("Unable to parse given time...")
}
if res == nil {
return respondError("Unable to match given time...")
}
formatStr := formatMap[format]
formatStr = fmt.Sprintf(formatStr, res.Time.Unix())
return respondSilentf("`%s`", formatStr)
}