Skip to content

Commit

Permalink
Merge pull request #27 from wneessen/fix-compare
Browse files Browse the repository at this point in the history
v0.2.3: Fix `/compare` command
  • Loading branch information
wneessen authored Nov 19, 2022
2 parents b50b847 + 9ed0c6a commit 196d0a8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
11 changes: 5 additions & 6 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,20 @@ func (b *Bot) StartTimeUnix() int64 {

// NewRequester returns a Requester based on if it's a channel interaction or DM
func (b *Bot) NewRequester(i *discordgo.Interaction) (*Requester, error) {
r := &Requester{nil, b.Model.User, nil}
if i.User != nil {
u, err := b.Model.User.GetByUserID(i.User.ID)
if err != nil {
switch {
case errors.Is(err, model.ErrUserNotExistent):
return r, fmt.Errorf("user is not registered")
return nil, fmt.Errorf("user is not registered")
default:
return r, err
return nil, err
}
}
r.User = u
return NewRequesterFromUser(u, b.Model.User)
}
if i.Member != nil {
r.Member = i.Member
return NewRequesterFromMember(i.Member, b.Model.User)
}
return r, nil
return nil, fmt.Errorf("neither interaction user nor member found")
}
31 changes: 26 additions & 5 deletions bot/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,33 @@ var (
"Please store your cookie with the **/setrat** command first")
ErrRATCookieExpired = errors.New("your Sea of Thieves authentication token is expired. " +
"Please use the **/setrat** command to update your token")
ErrMemberNil = errors.New("provided Member pointer must not be nil")
ErrUserNil = errors.New("provided User pointer must not be nil")
)

// NewRequesterFromMember returns a new *Requester pointer from a given *discordgo.Member
func NewRequesterFromMember(m *discordgo.Member, um *model.UserModel) (*Requester, error) {
r := &Requester{UserModel: um}
if m == nil {
return r, ErrMemberNil
}
u, err := r.UserModel.GetByUserID(m.User.ID)
if err != nil {
return r, ErrUserNotRegistered
}
r.User = u
return r, nil
}

// NewRequesterFromUser returns a new *Requester pointer from a given *model.User
func NewRequesterFromUser(u *model.User, um *model.UserModel) (*Requester, error) {
r := &Requester{UserModel: um, User: u}
if u == nil {
return r, ErrUserNil
}
return r, nil
}

// IsAdmin returns true if the Requester has administrative permissions on the guild
func (r *Requester) IsAdmin() bool {
return r.Member.Permissions&discordgo.PermissionAdministrator != 0
Expand All @@ -38,11 +63,7 @@ func (r *Requester) CanModerateMembers() bool {
// GetSoTRATCookie checks if the Requester has a SoT RAT cookie and reads it from the DB
func (r *Requester) GetSoTRATCookie() (string, error) {
if r.User == nil {
u, err := r.UserModel.GetByUserID(r.Member.User.ID)
if err != nil {
return "", ErrUserNotRegistered
}
r.User = u
return "", ErrUserNil
}
c, err := r.UserModel.GetPrefStringEnc(r.User, model.UserPrefSoTAuthToken)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion bot/sc_handler_sot_userstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ func (b *Bot) ScheduledEventUpdateUserStats() error {

// StoreSoTUserStats will retrieve the latest user stats from the API and store them in the DB
func (b *Bot) StoreSoTUserStats(u *model.User) error {
r := &Requester{nil, b.Model.User, u}
r, err := NewRequesterFromUser(u, b.Model.User)
if err != nil {
b.Log.Warn().Msgf("failed to create new requester: %s", err)
return err
}
ub, err := b.SoTGetUserBalance(r)
if err != nil {
switch {
Expand Down
3 changes: 3 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ var (

// ErrDeedDuplicate should be used in case a deed to be inserted into the database already exists
ErrDeedDuplicate = errors.New("deed already existent in database")

// ErrUserNil should be returned if the check for the *User returns nil
ErrUserNil = errors.New("user pointer must not be nil:w")
)

// Model is a collection of all available models
Expand Down
4 changes: 4 additions & 0 deletions model/user_pref.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ func getUserPrefEnc[V string | bool | int | int64](m UserModel, u *User, k UserP
var bv []byte
var ob bytes.Buffer

if u == nil {
return v, ErrUserNil
}

q := `SELECT pref_val
FROM user_prefs u
WHERE u.user_id = $1 AND u.pref_key = $2 AND u.is_enc = true`
Expand Down

0 comments on commit 196d0a8

Please sign in to comment.