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
14 changes: 14 additions & 0 deletions discord/application_command.go
Expand Up @@ -2,6 +2,7 @@ package discord

import (
"fmt"
"time"

"github.com/disgoorg/disgo/json"
"github.com/disgoorg/snowflake/v2"
Expand All @@ -27,6 +28,7 @@ type ApplicationCommand interface {
DefaultMemberPermissions() Permissions
DMPermission() bool
Version() snowflake.ID
CreatedAt() time.Time
applicationCommand()
}

Expand Down Expand Up @@ -174,6 +176,10 @@ func (c SlashCommand) Version() snowflake.ID {
return c.version
}

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

func (SlashCommand) applicationCommand() {}

var _ ApplicationCommand = (*UserCommand)(nil)
Expand Down Expand Up @@ -262,6 +268,10 @@ func (c UserCommand) Version() snowflake.ID {
return c.version
}

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

func (UserCommand) applicationCommand() {}

var _ ApplicationCommand = (*MessageCommand)(nil)
Expand Down Expand Up @@ -350,4 +360,8 @@ func (c MessageCommand) Version() snowflake.ID {
return c.version
}

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

func (MessageCommand) applicationCommand() {}
10 changes: 9 additions & 1 deletion discord/attachment.go
@@ -1,6 +1,10 @@
package discord

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

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

//Attachment is used for files sent in a Message
type Attachment struct {
Expand All @@ -16,6 +20,10 @@ type Attachment struct {
Ephemeral bool `json:"ephemeral,omitempty"`
}

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

type AttachmentUpdate interface {
attachmentUpdate()
}
Expand Down
10 changes: 9 additions & 1 deletion discord/auto_moderation.go
@@ -1,6 +1,10 @@
package discord

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

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

type AutoModerationEventType int

Expand Down Expand Up @@ -62,6 +66,10 @@ type AutoModerationRule struct {
ExemptChannels []snowflake.ID `json:"exempt_channels"`
}

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

type AutoModerationRuleCreate struct {
Name string `json:"name"`
EventType AutoModerationEventType `json:"event_type"`
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/guild_scheduled_event.go
Expand Up @@ -25,6 +25,10 @@ type GuildScheduledEvent struct {
UserCount int `json:"user_count"`
}

func (e GuildScheduledEvent) CreatedAt() time.Time {
return e.ID.Time()
}

type GuildScheduledEventCreate struct {
ChannelID snowflake.ID `json:"channel_id,omitempty"`
EntityMetaData *EntityMetaData `json:"entity_metadata,omitempty"`
Expand Down
14 changes: 14 additions & 0 deletions discord/integration.go
Expand Up @@ -2,6 +2,7 @@ package discord

import (
"fmt"
"time"

"github.com/disgoorg/disgo/json"
"github.com/disgoorg/snowflake/v2"
Expand Down Expand Up @@ -38,6 +39,7 @@ type Integration interface {
json.Marshaler
Type() IntegrationType
ID() snowflake.ID
CreatedAt() time.Time
}

type UnmarshalIntegration struct {
Expand Down Expand Up @@ -121,6 +123,10 @@ func (i TwitchIntegration) ID() snowflake.ID {
return i.IntegrationID
}

func (i TwitchIntegration) CreatedAt() time.Time {
return i.IntegrationID.Time()
}

type YouTubeIntegration struct {
IntegrationID snowflake.ID `json:"id"`
Name string `json:"name"`
Expand Down Expand Up @@ -155,6 +161,10 @@ func (i YouTubeIntegration) ID() snowflake.ID {
return i.IntegrationID
}

func (i YouTubeIntegration) CreatedAt() time.Time {
return i.IntegrationID.Time()
}

type BotIntegration struct {
IntegrationID snowflake.ID `json:"id"`
Name string `json:"name"`
Expand All @@ -181,3 +191,7 @@ func (BotIntegration) Type() IntegrationType {
func (i BotIntegration) ID() snowflake.ID {
return i.IntegrationID
}

func (i BotIntegration) CreatedAt() time.Time {
return i.IntegrationID.Time()
}