Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CreatedAt() to most entities and improve type consistency of stored timestaps #185

Merged
merged 9 commits into from Aug 16, 2022
61 changes: 57 additions & 4 deletions discord/activity.go
@@ -1,6 +1,11 @@
package discord

import "github.com/disgoorg/snowflake/v2"
import (
"time"

"github.com/disgoorg/disgo/json"
"github.com/disgoorg/snowflake/v2"
)

// ActivityType represents the status of a user, one of Game, Streaming, Listening, Watching, Custom or Competing
type ActivityType int
Expand All @@ -21,7 +26,7 @@ type Activity struct {
Name string `json:"name"`
Type ActivityType `json:"type"`
URL *string `json:"url"`
CreatedAt int64 `json:"created_at"`
CreatedAt time.Time `json:"created_at"`
Timestamps *ActivityTimestamps `json:"timestamps,omitempty"`
ApplicationID snowflake.ID `json:"application_id,omitempty"`
Details *string `json:"details,omitempty"`
Expand All @@ -35,6 +40,31 @@ type Activity struct {
Buttons []string `json:"buttons"`
}

func (a *Activity) UnmarshalJSON(data []byte) error {
type activity Activity
var v struct {
CreatedAt int64 `json:"created_at"`
activity
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*a = Activity(v.activity)
a.CreatedAt = time.UnixMilli(v.CreatedAt)
return nil
}

func (a Activity) MarshalJSON() ([]byte, error) {
type activity Activity
return json.Marshal(struct {
CreatedAt int64 `json:"created_at"`
activity
}{
CreatedAt: a.CreatedAt.UnixMilli(),
activity: (activity)(a),
})
}

// ActivityFlags add additional information to an activity
type ActivityFlags int

Expand All @@ -59,8 +89,31 @@ type ActivityButton struct {

// ActivityTimestamps represents when a user started and ended their activity
type ActivityTimestamps struct {
Start int64 `json:"start,omitempty"`
End int64 `json:"end,omitempty"`
Start time.Time
End time.Time
}

func (a *ActivityTimestamps) UnmarshalJSON(data []byte) error {
var v struct {
Start int64 `json:"start"`
End int64 `json:"end"`
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
a.Start = time.UnixMilli(v.Start)
a.End = time.UnixMilli(v.End)
return nil
}

func (a ActivityTimestamps) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Start int64 `json:"start,omitempty"`
End int64 `json:"end,omitempty"`
}{
Start: a.Start.UnixMilli(),
End: a.End.UnixMilli(),
})
}

// ActivityEmoji is an Emoji object for an Activity
Expand Down
8 changes: 8 additions & 0 deletions discord/application.go
Expand Up @@ -48,6 +48,10 @@ func (a Application) CoverURL(opts ...CDNOpt) *string {
return &url
}

func (a Application) CreatedAt() time.Time {
return a.ID.Time()
}

type PartialApplication struct {
ID snowflake.ID `json:"id"`
Flags ApplicationFlags `json:"flags"`
Expand Down Expand Up @@ -221,6 +225,10 @@ func (t Team) IconURL(opts ...CDNOpt) *string {
return &url
}

func (t Team) CreatedAt() time.Time {
return t.ID.Time()
}

type TeamMember struct {
MembershipState MembershipState `json:"membership_state"`
Permissions []TeamPermissions `json:"permissions"`
Expand Down
35 changes: 35 additions & 0 deletions discord/channel.go
Expand Up @@ -87,6 +87,9 @@ type Channel interface {
// Name returns the name of the Channel.
Name() string

// CreatedAt returns the creation time of the Channel.
CreatedAt() time.Time

channel()
}

Expand Down Expand Up @@ -349,6 +352,10 @@ func (c GuildTextChannel) DefaultAutoArchiveDuration() AutoArchiveDuration {
return c.defaultAutoArchiveDuration
}

func (c GuildTextChannel) CreatedAt() time.Time {
return c.id.Time()
}

func (GuildTextChannel) channel() {}
func (GuildTextChannel) guildChannel() {}
func (GuildTextChannel) messageChannel() {}
Expand Down Expand Up @@ -413,6 +420,10 @@ func (c DMChannel) LastPinTimestamp() *time.Time {
return c.lastPinTimestamp
}

func (c DMChannel) CreatedAt() time.Time {
return c.id.Time()
}

func (DMChannel) channel() {}
func (DMChannel) messageChannel() {}

Expand Down Expand Up @@ -557,6 +568,10 @@ func (c GuildVoiceChannel) RateLimitPerUser() int {
return c.rateLimitPerUser
}

func (c GuildVoiceChannel) CreatedAt() time.Time {
return c.id.Time()
}

func (GuildVoiceChannel) channel() {}
func (GuildVoiceChannel) messageChannel() {}
func (GuildVoiceChannel) guildChannel() {}
Expand Down Expand Up @@ -638,6 +653,10 @@ func (c GuildCategoryChannel) ParentID() *snowflake.ID {
return nil
}

func (c GuildCategoryChannel) CreatedAt() time.Time {
return c.id.Time()
}

func (GuildCategoryChannel) channel() {}
func (GuildCategoryChannel) guildChannel() {}

Expand Down Expand Up @@ -762,6 +781,10 @@ func (c GuildNewsChannel) ParentID() *snowflake.ID {
return c.parentID
}

func (c GuildNewsChannel) CreatedAt() time.Time {
return c.id.Time()
}

func (GuildNewsChannel) channel() {}
func (GuildNewsChannel) guildChannel() {}
func (GuildNewsChannel) messageChannel() {}
Expand Down Expand Up @@ -898,6 +921,10 @@ func (c GuildThread) DefaultAutoArchiveDuration() AutoArchiveDuration {
return 0
}

func (c GuildThread) CreatedAt() time.Time {
return c.id.Time()
}

func (GuildThread) channel() {}
func (GuildThread) guildChannel() {}
func (GuildThread) messageChannel() {}
Expand Down Expand Up @@ -995,6 +1022,10 @@ func (c GuildStageVoiceChannel) ParentID() *snowflake.ID {
return c.parentID
}

func (c GuildStageVoiceChannel) CreatedAt() time.Time {
return c.id.Time()
}

func (GuildStageVoiceChannel) channel() {}
func (GuildStageVoiceChannel) guildChannel() {}
func (GuildStageVoiceChannel) guildAudioChannel() {}
Expand Down Expand Up @@ -1085,6 +1116,10 @@ func (c GuildForumChannel) ParentID() *snowflake.ID {
return c.parentID
}

func (c GuildForumChannel) CreatedAt() time.Time {
return c.id.Time()
}

func (GuildForumChannel) channel() {}
func (GuildForumChannel) guildChannel() {}

Expand Down
9 changes: 9 additions & 0 deletions discord/emoji.go
@@ -1,6 +1,8 @@
package discord

import (
"time"

"github.com/disgoorg/snowflake/v2"
)

Expand Down Expand Up @@ -35,6 +37,13 @@ func (e Emoji) URL(opts ...CDNOpt) string {
return formatAssetURL(CustomEmoji, opts, e.ID)
}

func (e Emoji) CreatedAt() time.Time {
if e.ID == 0 {
return time.Time{}
}
return e.ID.Time()
}

type EmojiCreate struct {
Name string `json:"name"`
Image Icon `json:"image"`
Expand Down
4 changes: 4 additions & 0 deletions discord/guild.go
Expand Up @@ -171,6 +171,10 @@ func (g Guild) BannerURL(opts ...CDNOpt) *string {
return &url
}

func (g Guild) CreatedAt() time.Time {
return g.ID.Time()
}

type RestGuild struct {
Guild
Stickers []Sticker `json:"stickers"`
Expand Down
4 changes: 4 additions & 0 deletions discord/member.go
Expand Up @@ -60,6 +60,10 @@ func (m Member) AvatarURL(opts ...CDNOpt) *string {
return &url
}

func (m Member) CreatedAt() time.Time {
return m.User.CreatedAt()
}

// MemberAdd is used to add a member via the oauth2 access token to a guild
type MemberAdd struct {
AccessToken string `json:"access_token"`
Expand Down
6 changes: 6 additions & 0 deletions discord/role.go
@@ -1,6 +1,8 @@
package discord

import (
"time"

"github.com/disgoorg/disgo/json"
"github.com/disgoorg/snowflake/v2"
)
Expand Down Expand Up @@ -38,6 +40,10 @@ func (r Role) IconURL(opts ...CDNOpt) *string {
return &url
}

func (r Role) CreatedAt() time.Time {
return r.ID.Time()
}

// RoleTag are tags a Role has
type RoleTag struct {
BotID *snowflake.ID `json:"bot_id,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions discord/sticker.go
@@ -1,6 +1,8 @@
package discord

import (
"time"

"github.com/disgoorg/snowflake/v2"
)

Expand All @@ -27,6 +29,10 @@ func (s Sticker) URL(opts ...CDNOpt) string {
return formatAssetURL(CustomSticker, append(opts, WithFormat(format)), s.ID)
}

func (s Sticker) CreatedAt() time.Time {
return s.ID.Time()
}

type StickerType int

const (
Expand Down
5 changes: 5 additions & 0 deletions discord/user.go
Expand Up @@ -2,6 +2,7 @@ package discord

import (
"strconv"
"time"

"github.com/disgoorg/disgo/json"
"github.com/disgoorg/snowflake/v2"
Expand Down Expand Up @@ -96,6 +97,10 @@ func (u User) BannerURL(opts ...CDNOpt) *string {
return &url
}

func (u User) CreatedAt() time.Time {
return u.ID.Time()
}

// OAuth2User represents a full User returned by the oauth2 endpoints
type OAuth2User struct {
User
Expand Down
14 changes: 14 additions & 0 deletions discord/webhook.go
Expand Up @@ -2,6 +2,7 @@ package discord

import (
"fmt"
"time"

"github.com/disgoorg/disgo/json"
"github.com/disgoorg/snowflake/v2"
Expand All @@ -25,6 +26,7 @@ type Webhook interface {
Name() string
Avatar() *string
AvatarURL(opts ...CDNOpt) *string
CreatedAt() time.Time
webhook()
}

Expand Down Expand Up @@ -155,6 +157,10 @@ func (w IncomingWebhook) URL() string {
return WebhookURL(w.ID(), w.Token)
}

func (w IncomingWebhook) CreatedAt() time.Time {
return w.id.Time()
}

func (IncomingWebhook) webhook() {}

var _ Webhook = (*ChannelFollowerWebhook)(nil)
Expand Down Expand Up @@ -237,6 +243,10 @@ func (w ChannelFollowerWebhook) DefaultAvatarURL(opts ...CDNOpt) string {
return formatAssetURL(DefaultUserAvatar, opts, 0)
}

func (w ChannelFollowerWebhook) CreatedAt() time.Time {
return w.id.Time()
}

func (ChannelFollowerWebhook) webhook() {}

var _ Webhook = (*ApplicationWebhook)(nil)
Expand Down Expand Up @@ -314,6 +324,10 @@ func (w ApplicationWebhook) DefaultAvatarURL(opts ...CDNOpt) string {
return formatAssetURL(DefaultUserAvatar, opts, 0)
}

func (w ApplicationWebhook) CreatedAt() time.Time {
return w.id.Time()
}

func (ApplicationWebhook) webhook() {}

type WebhookSourceGuild struct {
Expand Down