forked from rumblefrog/discordgo
/
types.go
177 lines (145 loc) · 4.14 KB
/
types.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//go:generate generateEmojiCodeMap -pkg discordgo
// Discordgo - Discord bindings for Go
// Available at https://github.com/bwmarrin/discordgo
// Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file contains custom types, currently only a timestamp wrapper.
package discordgo
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
)
// Timestamp stores a timestamp, as sent by the Discord API.
type Timestamp string
// Parse parses a timestamp string into a time.Time object.
// The only time this can fail is if Discord changes their timestamp format.
func (t Timestamp) Parse() (time.Time, error) {
return time.Parse(time.RFC3339, string(t))
}
// RESTError stores error information about a request with a bad response code.
// Message is not always present, there are cases where api calls can fail
// without returning a json message.
type RESTError struct {
Request *http.Request
Response *http.Response
ResponseBody []byte
Message *APIErrorMessage // Message may be nil.
}
func newRestError(req *http.Request, resp *http.Response, body []byte) *RESTError {
restErr := &RESTError{
Request: req,
Response: resp,
ResponseBody: body,
}
// Attempt to decode the error and assume no message was provided if it fails
var msg *APIErrorMessage
err := json.Unmarshal(body, &msg)
if err == nil {
restErr.Message = msg
}
return restErr
}
func (r RESTError) Error() string {
return fmt.Sprintf("HTTP %s, %s", r.Response.Status, r.ResponseBody)
}
// IDSlice Is a slice of snowflake id's that properly marshals and unmarshals the way discord expects them to
// They unmarshal from string arrays and marshals back to string arrays
type IDSlice []int64
func (ids *IDSlice) UnmarshalJSON(data []byte) error {
if len(data) < 3 {
return nil
}
// Split and strip away "[" "]"
split := strings.Split(string(data[1:len(data)-1]), ",")
*ids = make([]int64, 0, len(split))
for _, s := range split {
s = strings.TrimSpace(s)
if len(s) < 3 {
// Empty or invalid
continue
}
// Strip away quotes and parse
parsed, err := strconv.ParseInt(s[1:len(s)-1], 10, 64)
if err != nil {
return err
}
*ids = append(*ids, parsed)
}
return nil
}
func (ids IDSlice) MarshalJSON() ([]byte, error) {
// Capacity:
// 2 brackets
// each id is:
// 18 characters currently, but 1 extra added for the future,
// 1 comma
// 2 quotes
if len(ids) < 1 {
return []byte("[]"), nil
}
outPut := make([]byte, 1, 2+(len(ids)*22))
outPut[0] = '['
for i, id := range ids {
if i != 0 {
outPut = append(outPut, '"', ',', '"')
} else {
outPut = append(outPut, '"')
}
outPut = append(outPut, []byte(strconv.FormatInt(id, 10))...)
}
outPut = append(outPut, '"', ']')
return outPut, nil
}
type EmojiName struct {
string
}
func (emoji EmojiName) String() string {
if codepoint, ok := emojiCodeMap[emoji.string]; ok {
emoji.string = codepoint
}
// Discord does not accept the emoji qualifier character.
// return strings.Replace(emoji.string, "\uFE0F", "", 1)
// this no longer the case? in fact its required?
return emoji.string
}
// Discord is super inconsistent with with types in some places (especially presence updates,
// might aswell change them to map[string]interface{} soon because there is 0 validation)
type DiscordFloat float64
func (df *DiscordFloat) UnmarshalJSON(data []byte) error {
var dst json.Number
err := json.Unmarshal(data, &dst)
if err != nil {
return err
}
parsed, err := dst.Float64()
if err != nil {
return err
}
*df = DiscordFloat(parsed)
return nil
}
type DiscordInt64 int64
func (di *DiscordInt64) UnmarshalJSON(data []byte) error {
var dst json.Number
err := json.Unmarshal(data, &dst)
if err != nil {
return err
}
parsed, err := dst.Int64()
if err != nil {
// Attempt to fallback to float, we lost some precision but eh, what can you do when discord is so freaking inconsistent
f, err := dst.Float64()
if err != nil {
return err
}
*di = DiscordInt64(int64(f))
return nil
}
*di = DiscordInt64(parsed)
return nil
}