-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
133 lines (102 loc) · 2.88 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
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
package revolt
import (
"encoding/json"
"time"
"github.com/oklog/ulid/v2"
)
// User struct.
type User struct {
Client *Client
CreatedAt time.Time
Id string `json:"_id"`
Username string `json:"username"`
Avatar *Attachment `json:"avatar"`
Relations []*UserRelations `json:"relations"`
Badges int `json:"badges"`
Status *UserStatus `json:"status"`
Relationship string `json:"relationship"`
IsOnline bool `json:"online"`
Flags int `json:"flags"`
BotInformation *BotInformation `json:"bot"`
}
// User relations struct.
type UserRelations struct {
Id string `json:"_id"`
Status string `json:"status"`
}
// User status struct.
type UserStatus struct {
Text string `json:"text"`
Presence string `json:"presence"`
}
// Bot information struct.
type BotInformation struct {
Owner string `json:"owner"`
}
// Calculate creation date and edit the struct.
func (u *User) CalculateCreationDate() error {
ulid, err := ulid.Parse(u.Id)
if err != nil {
return err
}
u.CreatedAt = time.UnixMilli(int64(ulid.Time()))
return nil
}
// Create a mention format.
func (u User) FormatMention() string {
return "<@" + u.Id + ">"
}
// Open a DM with the user.
func (u User) OpenDirectMessage() (*Channel, error) {
dmChannel := &Channel{}
dmChannel.Client = u.Client
resp, err := u.Client.Request("GET", "/users/"+u.Id+"/dm", []byte{})
if err != nil {
return dmChannel, err
}
err = json.Unmarshal(resp, dmChannel)
return dmChannel, err
}
// Fetch default user avatar.
func (u User) FetchDefaultAvatar() (*Binary, error) {
avatarData := &Binary{}
resp, err := u.Client.Request("GET", "/users/"+u.Id+"/default_avatar", []byte{})
if err != nil {
return avatarData, err
}
avatarData.Data = resp
return avatarData, nil
}
// Fetch user relationship.
func (u User) FetchRelationship() (*UserRelations, error) {
relationshipData := &UserRelations{}
relationshipData.Id = u.Id
resp, err := u.Client.Request("GET", "/users/"+u.Id+"/relationship", []byte{})
if err != nil {
return relationshipData, err
}
err = json.Unmarshal(resp, relationshipData)
return relationshipData, err
}
// Block user.
func (u User) Block() (*UserRelations, error) {
relationshipData := &UserRelations{}
relationshipData.Id = u.Id
resp, err := u.Client.Request("PUT", "/users/"+u.Id+"/block", []byte{})
if err != nil {
return relationshipData, err
}
err = json.Unmarshal(resp, relationshipData)
return relationshipData, err
}
// Un-block user.
func (u User) Unblock() (*UserRelations, error) {
relationshipData := &UserRelations{}
relationshipData.Id = u.Id
resp, err := u.Client.Request("DELETE", "/users/"+u.Id+"/block", []byte{})
if err != nil {
return relationshipData, err
}
err = json.Unmarshal(resp, relationshipData)
return relationshipData, err
}