forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
158 lines (124 loc) · 3.85 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package resource
import (
"encoding/json"
"errors"
"sync"
"github.com/andersfylling/disgord/rest/httd"
. "github.com/andersfylling/snowflake"
)
const (
// StatusIdle presence status for idle
StatusIdle = "idle"
// StatusDnd presence status for dnd
StatusDnd = "dnd"
// StatusOnline presence status for online
StatusOnline = "online"
// StatusOffline presence status for offline
StatusOffline = "offline"
)
type UserInterface interface {
Mention() string
MentionNickname() string
String() string
}
// TODO: https://discordapp.com/developers/docs/topics/gateway#activity-object-activity-structure
type UserActivity struct{}
// ---------
// TODO: should a user object always have a Snowflake?
func NewUser() *User {
return &User{}
}
type User struct {
ID Snowflake `json:"id,omitempty"`
Username string `json:"username,omitempty"`
Discriminator string `json:"discriminator,omitempty"`
Email string `json:"email,omitempty"`
Avatar *string `json:"avatar"` // data:image/jpeg;base64,BASE64_ENCODED_JPEG_IMAGE_DATA //TODO: pointer?
Token string `json:"token,omitempty"`
Verified bool `json:"verified,omitempty"`
MFAEnabled bool `json:"mfa_enabled,omitempty"`
Bot bool `json:"bot,omitempty"`
sync.RWMutex `json:"-"`
}
func (u *User) Mention() string {
return "<@" + u.ID.String() + ">"
}
func (u *User) MentionNickname() string {
return "<@!" + u.ID.String() + ">"
}
func (u *User) String() string {
return u.Username + "#" + u.Discriminator + "{" + u.ID.String() + "}"
}
// Partial check if this is not a complete user object
// Assumption: has a snowflake.
func (u *User) Partial() bool {
return (u.Username + u.Discriminator) == ""
}
func (u *User) MarshalJSON() ([]byte, error) {
if u.ID.Empty() {
return []byte("{}"), nil
}
return json.Marshal(User(*u))
}
// func (u *User) UnmarshalJSON(data []byte) error {
// return json.Unmarshal(data, &u.userJSON)
// }
func (u *User) Clear() {
//u.d.Avatar = nil
}
func (u *User) SendMsg(requester httd.Requester, msg *Message) (channelID Snowflake, messageID Snowflake, err error) {
return NewSnowflake(0), NewSnowflake(0), errors.New("not implemented")
}
func (u *User) SendMsgString(requester httd.Requester, msg string) (channelID Snowflake, messageID Snowflake, err error) {
return NewSnowflake(0), NewSnowflake(0), errors.New("not implemented")
}
func (u *User) DeepCopy() *User {
user := NewUser()
u.RLock()
user.ID = u.ID
user.Username = u.Username
user.Discriminator = u.Discriminator
user.Email = u.Email
user.Token = u.Token
user.Verified = u.Verified
user.MFAEnabled = u.MFAEnabled
user.Bot = u.Bot
if u.Avatar != nil {
avatar := *u.Avatar
user.Avatar = &avatar
}
u.RUnlock()
return user
}
func (u *User) Valid() bool {
return u.ID > 0
}
// -------
func NewUserPresence() *UserPresence {
return &UserPresence{}
}
type UserPresence struct {
User *User `json:"user"`
Roles []Snowflake `json:"roles"`
Game *UserActivity `json:"activity"`
GuildID Snowflake `json:"guild_id"`
Nick string `json:"nick"`
Status string `json:"status"`
}
func (p *UserPresence) Update(status string) {
// Update the presence.
// talk to the discord api
}
func (p *UserPresence) String() string {
return p.Status
}
func (p *UserPresence) Clear() {
p.Game = nil
}
type UserConnection struct {
ID string `json:"id"` // id of the connection account
Name string `json:"name"` // the username of the connection account
Type string `json:"type"` // the service of the connection (twitch, youtube)
Revoked bool `json:"revoked"` // whether the connection is revoked
Integrations []*IntegrationAccount `json:"integrations"` // an array of partial server integrations
}