-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
66 lines (55 loc) · 1.38 KB
/
user.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
package tetrio
import (
"context"
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"github.com/gkaply532/tetrio/v2/types"
)
type NoUserError struct {
UID string
wrapped error
}
func (e NoUserError) Error() string {
return fmt.Sprintf("tetrio: no such user: %q", e.UID)
}
func (e NoUserError) Unwrap() error {
return e.wrapped
}
type SearchError uint64
func (e SearchError) Error() string {
return fmt.Sprintf("tetrio: no linked user found for discord snowflake: %d", uint64(e))
}
func (s Session) GetUser(ctx context.Context, name string) (*types.FullUser, error) {
if len(name) > 60 {
return nil, errors.New("tetrio: username too long")
}
endpoint := "/users/" + url.PathEscape(strings.ToLower(name))
user, err := do[*types.FullUser](ctx, s, endpoint)
if err != nil {
var tetrioErr Error
if errors.As(err, &tetrioErr) && strings.Contains(
strings.ToLower(string(tetrioErr)),
"no such user",
) {
return nil, NoUserError{
UID: name,
wrapped: err,
}
}
return nil, err
}
return user, nil
}
func (s Session) SearchUser(ctx context.Context, discordID uint64) (types.PartialUser, error) {
user, err := do[types.PartialUser](ctx, s, "/users/search/"+url.PathEscape(strconv.FormatUint(discordID, 10)))
if err != nil {
return types.PartialUser{}, err
}
if user.ID == "" {
return types.PartialUser{}, SearchError(discordID)
}
return user, nil
}