Remove Timestamp type, use time.Time
This commit is contained in:
parent
092735083d
commit
fccf6db03e
5 changed files with 14 additions and 47 deletions
|
@ -14,6 +14,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MessageType is the type of Message
|
// MessageType is the type of Message
|
||||||
|
@ -60,11 +61,11 @@ type Message struct {
|
||||||
// CAUTION: this field may be removed in a
|
// CAUTION: this field may be removed in a
|
||||||
// future API version; it is safer to calculate
|
// future API version; it is safer to calculate
|
||||||
// the creation time via the ID.
|
// the creation time via the ID.
|
||||||
Timestamp Timestamp `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
|
||||||
// The time at which the last edit of the message
|
// The time at which the last edit of the message
|
||||||
// occurred, if it has been edited.
|
// occurred, if it has been edited.
|
||||||
EditedTimestamp Timestamp `json:"edited_timestamp"`
|
EditedTimestamp *time.Time `json:"edited_timestamp"`
|
||||||
|
|
||||||
// The roles mentioned in the message.
|
// The roles mentioned in the message.
|
||||||
MentionRoles []string `json:"mention_roles"`
|
MentionRoles []string `json:"mention_roles"`
|
||||||
|
|
6
state.go
6
state.go
|
@ -306,7 +306,7 @@ func (s *State) MemberAdd(member *Member) error {
|
||||||
} else {
|
} else {
|
||||||
// We are about to replace `m` in the state with `member`, but first we need to
|
// We are about to replace `m` in the state with `member`, but first we need to
|
||||||
// make sure we preserve any fields that the `member` doesn't contain from `m`.
|
// make sure we preserve any fields that the `member` doesn't contain from `m`.
|
||||||
if member.JoinedAt == "" {
|
if member.JoinedAt.IsZero() {
|
||||||
member.JoinedAt = m.JoinedAt
|
member.JoinedAt = m.JoinedAt
|
||||||
}
|
}
|
||||||
*m = *member
|
*m = *member
|
||||||
|
@ -637,7 +637,7 @@ func (s *State) MessageAdd(message *Message) error {
|
||||||
if message.Content != "" {
|
if message.Content != "" {
|
||||||
m.Content = message.Content
|
m.Content = message.Content
|
||||||
}
|
}
|
||||||
if message.EditedTimestamp != "" {
|
if message.EditedTimestamp != nil {
|
||||||
m.EditedTimestamp = message.EditedTimestamp
|
m.EditedTimestamp = message.EditedTimestamp
|
||||||
}
|
}
|
||||||
if message.Mentions != nil {
|
if message.Mentions != nil {
|
||||||
|
@ -649,7 +649,7 @@ func (s *State) MessageAdd(message *Message) error {
|
||||||
if message.Attachments != nil {
|
if message.Attachments != nil {
|
||||||
m.Attachments = message.Attachments
|
m.Attachments = message.Attachments
|
||||||
}
|
}
|
||||||
if message.Timestamp != "" {
|
if !message.Timestamp.IsZero() {
|
||||||
m.Timestamp = message.Timestamp
|
m.Timestamp = message.Timestamp
|
||||||
}
|
}
|
||||||
if message.Author != nil {
|
if message.Author != nil {
|
||||||
|
|
16
structs.go
16
structs.go
|
@ -150,7 +150,7 @@ type Integration struct {
|
||||||
ExpireGracePeriod int `json:"expire_grace_period"`
|
ExpireGracePeriod int `json:"expire_grace_period"`
|
||||||
User *User `json:"user"`
|
User *User `json:"user"`
|
||||||
Account IntegrationAccount `json:"account"`
|
Account IntegrationAccount `json:"account"`
|
||||||
SyncedAt Timestamp `json:"synced_at"`
|
SyncedAt time.Time `json:"synced_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExpireBehavior of Integration
|
// ExpireBehavior of Integration
|
||||||
|
@ -197,7 +197,7 @@ type Invite struct {
|
||||||
Channel *Channel `json:"channel"`
|
Channel *Channel `json:"channel"`
|
||||||
Inviter *User `json:"inviter"`
|
Inviter *User `json:"inviter"`
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
CreatedAt Timestamp `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
MaxAge int `json:"max_age"`
|
MaxAge int `json:"max_age"`
|
||||||
Uses int `json:"uses"`
|
Uses int `json:"uses"`
|
||||||
MaxUses int `json:"max_uses"`
|
MaxUses int `json:"max_uses"`
|
||||||
|
@ -258,8 +258,8 @@ type Channel struct {
|
||||||
LastMessageID string `json:"last_message_id"`
|
LastMessageID string `json:"last_message_id"`
|
||||||
|
|
||||||
// The timestamp of the last pinned message in the channel.
|
// The timestamp of the last pinned message in the channel.
|
||||||
// Empty if the channel has no pinned messages.
|
// nil if the channel has no pinned messages.
|
||||||
LastPinTimestamp Timestamp `json:"last_pin_timestamp"`
|
LastPinTimestamp *time.Time `json:"last_pin_timestamp"`
|
||||||
|
|
||||||
// Whether the channel is marked as NSFW.
|
// Whether the channel is marked as NSFW.
|
||||||
NSFW bool `json:"nsfw"`
|
NSFW bool `json:"nsfw"`
|
||||||
|
@ -453,7 +453,7 @@ type Guild struct {
|
||||||
// The time at which the current user joined the guild.
|
// The time at which the current user joined the guild.
|
||||||
// This field is only present in GUILD_CREATE events and websocket
|
// This field is only present in GUILD_CREATE events and websocket
|
||||||
// update events, and thus is only present in state-cached guilds.
|
// update events, and thus is only present in state-cached guilds.
|
||||||
JoinedAt Timestamp `json:"joined_at"`
|
JoinedAt time.Time `json:"joined_at"`
|
||||||
|
|
||||||
// The hash of the guild's discovery splash.
|
// The hash of the guild's discovery splash.
|
||||||
DiscoverySplash string `json:"discovery_splash"`
|
DiscoverySplash string `json:"discovery_splash"`
|
||||||
|
@ -782,8 +782,8 @@ type Member struct {
|
||||||
// The guild ID on which the member exists.
|
// The guild ID on which the member exists.
|
||||||
GuildID string `json:"guild_id"`
|
GuildID string `json:"guild_id"`
|
||||||
|
|
||||||
// The time at which the member joined the guild, in ISO8601.
|
// The time at which the member joined the guild.
|
||||||
JoinedAt Timestamp `json:"joined_at"`
|
JoinedAt time.Time `json:"joined_at"`
|
||||||
|
|
||||||
// The nickname of the member, if they have one.
|
// The nickname of the member, if they have one.
|
||||||
Nick string `json:"nick"`
|
Nick string `json:"nick"`
|
||||||
|
@ -801,7 +801,7 @@ type Member struct {
|
||||||
Roles []string `json:"roles"`
|
Roles []string `json:"roles"`
|
||||||
|
|
||||||
// When the user used their Nitro boost on the server
|
// When the user used their Nitro boost on the server
|
||||||
PremiumSince Timestamp `json:"premium_since"`
|
PremiumSince *time.Time `json:"premium_since"`
|
||||||
|
|
||||||
// Is true while the member hasn't accepted the membership screen.
|
// Is true while the member hasn't accepted the membership screen.
|
||||||
Pending bool `json:"pending"`
|
Pending bool `json:"pending"`
|
||||||
|
|
10
types.go
10
types.go
|
@ -12,18 +12,8 @@ package discordgo
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"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.
|
// 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
|
// Message is not always present, there are cases where api calls can fail
|
||||||
// without returning a json message.
|
// without returning a json message.
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
package discordgo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTimestampParse(t *testing.T) {
|
|
||||||
ts, err := Timestamp("2016-03-24T23:15:59.605000+00:00").Parse()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if ts.Year() != 2016 || ts.Month() != time.March || ts.Day() != 24 {
|
|
||||||
t.Error("Incorrect date")
|
|
||||||
}
|
|
||||||
if ts.Hour() != 23 || ts.Minute() != 15 || ts.Second() != 59 {
|
|
||||||
t.Error("Incorrect time")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, offset := ts.Zone()
|
|
||||||
if offset != 0 {
|
|
||||||
t.Error("Incorrect timezone")
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue