/
cmd_profile.go
147 lines (120 loc) · 3.97 KB
/
cmd_profile.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
package bot
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/api/cmdroute"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/utils/json/option"
"github.com/holedaemon/bot2/internal/db/models"
"github.com/holedaemon/bot2/internal/db/modelsx"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
"github.com/zikaeroh/ctxlog"
"go.uber.org/zap"
)
var (
noProfileError = &api.InteractionResponseData{
Content: option.NewNullableString("You need to initialize a profile with `/profile init` before running this command"),
Flags: discord.EphemeralMessage,
}
)
func (b *Bot) cmdProfileInit(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
id := data.Event.SenderID()
if id == 0 {
ctxlog.Error(ctx, "sender id is 0")
return respondError("An unexpected error has occurred, oops!")
}
exists, err := models.UserProfiles(qm.Where("user_id = ?", id.String())).Exists(ctx, b.db)
if err != nil {
ctxlog.Error(ctx, "error fetching user profile", zap.Error(err))
return dbError
}
if exists {
return respond("You have already created a profile")
}
p := &models.UserProfile{
UserID: id.String(),
}
if err := modelsx.UpsertUserProfile(ctx, b.db, p); err != nil {
ctxlog.Error(ctx, "error upserting user profile", zap.Error(err))
return dbError
}
return respondSilent("Your profile has been created")
}
func (b *Bot) cmdProfileDelete(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
confirmation, err := data.Options.Find("are-you-sure").BoolValue()
if err != nil {
ctxlog.Error(ctx, "error parsing value as boolean", zap.Error(err))
return respondError("Error parsing confirmation as a boolean.. hehe")
}
if !confirmation {
return respondSilent("Stop wasting my time...")
}
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 respondSilent("Can't delete a profile if you don't have one...")
}
ctxlog.Error(ctx, "error fetching profile", zap.Error(err))
return dbError
}
if err := p.Delete(ctx, b.db); err != nil {
ctxlog.Error(ctx, "error deleting profile", zap.Error(err))
return dbError
}
return respondSilent("Your profile has been deleted")
}
func (b *Bot) cmdProfileGet(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
id := data.Event.SenderID()
if id == 0 {
ctxlog.Error(ctx, "sender id is 0")
return respondError("Unexpected error has occurred, oops!")
}
exists, err := modelsx.UserProfileExists(ctx, b.db, id.String())
if err != nil {
ctxlog.Error(ctx, "error checking if profile exists", zap.Error(err))
return dbError
}
if !exists {
return noProfileError
}
addr := fmt.Sprintf("%s/profile", b.siteAddress)
return respondSilentf("You can find your profile at <%s>", addr)
}
func (b *Bot) cmdProfileSetTimezone(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
tz := data.Options.Find("timezone").String()
if tz == "" {
return respondError("Timezone cannot be blank!!")
}
_, err := time.LoadLocation(tz)
if err != nil {
return respondError("Unable to parse given timezone. Are you sure it's real?")
}
id := data.Event.SenderID()
if id == 0 {
ctxlog.Error(ctx, "sender id is 0")
return respondError("Unexpected error has occurred, oops!")
}
p, err := modelsx.FetchUserProfile(ctx, b.db, id.String())
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return noProfileError
}
ctxlog.Error(ctx, "error fetching profile", zap.Error(err))
return dbError
}
p.Timezone = null.StringFrom(tz)
if err := modelsx.UpsertUserProfile(ctx, b.db, p); err != nil {
return dbError
}
return respondSilentf("Your timezone has been set to `%s`", tz)
}