2020-01-02 05:39:52 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2020-01-17 22:29:13 +00:00
|
|
|
"strings"
|
2020-01-02 05:39:52 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
// Epoch is the Discord epoch constant in time.Duration (nanoseconds)
|
2020-05-08 20:52:46 +00:00
|
|
|
// since Unix epoch.
|
2020-07-29 20:58:39 +00:00
|
|
|
const Epoch = 1420070400000 * time.Millisecond
|
2020-05-08 20:52:46 +00:00
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
// DurationSinceEpoch returns the duration from the Discord epoch to current.
|
|
|
|
func DurationSinceEpoch(t time.Time) time.Duration {
|
|
|
|
return time.Duration(t.UnixNano()) - Epoch
|
2020-05-08 20:52:46 +00:00
|
|
|
}
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
type Snowflake uint64
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-05-08 20:52:46 +00:00
|
|
|
// NullSnowflake gets encoded into a null. This is used for
|
|
|
|
// optional and nullable snowflake fields.
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullSnowflake = ^Snowflake(0)
|
2020-05-08 20:52:46 +00:00
|
|
|
|
2020-01-02 05:39:52 +00:00
|
|
|
func NewSnowflake(t time.Time) Snowflake {
|
2020-07-29 20:58:39 +00:00
|
|
|
return Snowflake((DurationSinceEpoch(t) / time.Millisecond) << 22)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 07:24:15 +00:00
|
|
|
func ParseSnowflake(sf string) (Snowflake, error) {
|
2020-05-14 08:03:48 +00:00
|
|
|
if sf == "null" {
|
|
|
|
return NullSnowflake, nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 04:53:22 +00:00
|
|
|
u, err := strconv.ParseUint(sf, 10, 64)
|
2020-01-22 07:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-08-20 04:53:22 +00:00
|
|
|
return Snowflake(u), nil
|
2020-01-22 07:24:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 04:19:24 +00:00
|
|
|
func (s *Snowflake) UnmarshalJSON(v []byte) error {
|
2020-05-14 08:03:48 +00:00
|
|
|
p, err := ParseSnowflake(strings.Trim(string(v), `"`))
|
2020-01-04 04:19:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-14 08:03:48 +00:00
|
|
|
*s = p
|
2020-01-04 04:19:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 20:52:46 +00:00
|
|
|
func (s Snowflake) MarshalJSON() ([]byte, error) {
|
2020-05-14 08:03:48 +00:00
|
|
|
// This includes 0 and null, because MarshalJSON does not dictate when a
|
|
|
|
// value gets omitted.
|
2020-07-29 20:10:17 +00:00
|
|
|
if !s.IsValid() {
|
2020-01-19 06:06:00 +00:00
|
|
|
return []byte("null"), nil
|
2020-05-08 20:52:46 +00:00
|
|
|
} else {
|
|
|
|
return []byte(`"` + strconv.FormatInt(int64(s), 10) + `"`), nil
|
2020-01-17 22:29:13 +00:00
|
|
|
}
|
2020-01-04 04:19:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 07:49:10 +00:00
|
|
|
// String returns the ID, or nothing if the snowflake isn't valid.
|
2020-01-02 05:39:52 +00:00
|
|
|
func (s Snowflake) String() string {
|
2020-05-14 08:03:48 +00:00
|
|
|
// Check if negative.
|
2020-07-29 20:10:17 +00:00
|
|
|
if !s.IsValid() {
|
2020-05-13 21:51:26 +00:00
|
|
|
return ""
|
|
|
|
}
|
2020-01-02 05:39:52 +00:00
|
|
|
return strconv.FormatUint(uint64(s), 10)
|
|
|
|
}
|
|
|
|
|
2020-07-29 20:10:17 +00:00
|
|
|
// IsValid returns whether or not the snowflake is valid.
|
|
|
|
func (s Snowflake) IsValid() bool {
|
2020-07-29 20:58:39 +00:00
|
|
|
return !(int64(s) == 0 || s == NullSnowflake)
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
// IsNull returns whether or not the snowflake is null.
|
|
|
|
func (s Snowflake) IsNull() bool {
|
|
|
|
return s == NullSnowflake
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:39:52 +00:00
|
|
|
func (s Snowflake) Time() time.Time {
|
2020-07-29 20:58:39 +00:00
|
|
|
unixnano := time.Duration(s>>22)*time.Millisecond + Epoch
|
2020-05-08 20:52:46 +00:00
|
|
|
return time.Unix(0, int64(unixnano))
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s Snowflake) Worker() uint8 {
|
2020-05-08 21:11:56 +00:00
|
|
|
return uint8(s & 0x3E0000 >> 17)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s Snowflake) PID() uint8 {
|
|
|
|
return uint8(s & 0x1F000 >> 12)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Snowflake) Increment() uint16 {
|
|
|
|
return uint16(s & 0xFFF)
|
|
|
|
}
|
2020-07-21 20:27:59 +00:00
|
|
|
|
|
|
|
type AppID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullAppID = AppID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s AppID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *AppID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s AppID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s AppID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s AppID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s AppID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s AppID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s AppID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s AppID) Increment() uint16 { return Snowflake(s).Increment() }
|
|
|
|
|
|
|
|
type AttachmentID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullAttachmentID = AttachmentID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s AttachmentID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *AttachmentID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s AttachmentID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s AttachmentID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s AttachmentID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s AttachmentID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s AttachmentID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s AttachmentID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s AttachmentID) Increment() uint16 { return Snowflake(s).Increment() }
|
|
|
|
|
|
|
|
type AuditLogEntryID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullAuditLogEntryID = AuditLogEntryID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s AuditLogEntryID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *AuditLogEntryID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s AuditLogEntryID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s AuditLogEntryID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s AuditLogEntryID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s AuditLogEntryID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s AuditLogEntryID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s AuditLogEntryID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s AuditLogEntryID) Increment() uint16 { return Snowflake(s).Increment() }
|
|
|
|
|
|
|
|
type ChannelID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullChannelID = ChannelID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s ChannelID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *ChannelID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s ChannelID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s ChannelID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s ChannelID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s ChannelID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s ChannelID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s ChannelID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s ChannelID) Increment() uint16 { return Snowflake(s).Increment() }
|
2020-09-24 18:43:20 +00:00
|
|
|
func (s ChannelID) Mention() string { return "<#" + s.String() + ">" }
|
2020-07-21 20:27:59 +00:00
|
|
|
|
|
|
|
type EmojiID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullEmojiID = EmojiID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s EmojiID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *EmojiID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s EmojiID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s EmojiID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s EmojiID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s EmojiID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s EmojiID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s EmojiID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s EmojiID) Increment() uint16 { return Snowflake(s).Increment() }
|
|
|
|
|
|
|
|
type IntegrationID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullIntegrationID = IntegrationID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s IntegrationID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *IntegrationID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s IntegrationID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s IntegrationID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s IntegrationID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s IntegrationID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s IntegrationID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s IntegrationID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s IntegrationID) Increment() uint16 { return Snowflake(s).Increment() }
|
|
|
|
|
|
|
|
type GuildID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullGuildID = GuildID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s GuildID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *GuildID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s GuildID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s GuildID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s GuildID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s GuildID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s GuildID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s GuildID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s GuildID) Increment() uint16 { return Snowflake(s).Increment() }
|
|
|
|
|
|
|
|
type MessageID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullMessageID = MessageID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s MessageID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *MessageID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s MessageID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s MessageID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s MessageID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s MessageID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s MessageID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s MessageID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s MessageID) Increment() uint16 { return Snowflake(s).Increment() }
|
|
|
|
|
|
|
|
type RoleID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullRoleID = RoleID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s RoleID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *RoleID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s RoleID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s RoleID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s RoleID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s RoleID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s RoleID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s RoleID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s RoleID) Increment() uint16 { return Snowflake(s).Increment() }
|
2020-09-24 18:43:20 +00:00
|
|
|
func (s RoleID) Mention() string { return "<@&" + s.String() + ">" }
|
2020-07-21 20:27:59 +00:00
|
|
|
|
|
|
|
type UserID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullUserID = UserID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s UserID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *UserID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s UserID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s UserID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s UserID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s UserID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s UserID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s UserID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s UserID) Increment() uint16 { return Snowflake(s).Increment() }
|
2020-09-24 18:43:20 +00:00
|
|
|
func (s UserID) Mention() string { return "<@" + s.String() + ">" }
|
2020-07-21 20:27:59 +00:00
|
|
|
|
|
|
|
type WebhookID Snowflake
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
const NullWebhookID = WebhookID(NullSnowflake)
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s WebhookID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
|
|
|
|
func (s *WebhookID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
|
|
|
|
func (s WebhookID) String() string { return Snowflake(s).String() }
|
2020-07-29 20:10:17 +00:00
|
|
|
func (s WebhookID) IsValid() bool { return Snowflake(s).IsValid() }
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s WebhookID) IsNull() bool { return Snowflake(s).IsNull() }
|
|
|
|
func (s WebhookID) Time() time.Time { return Snowflake(s).Time() }
|
|
|
|
func (s WebhookID) Worker() uint8 { return Snowflake(s).Worker() }
|
|
|
|
func (s WebhookID) PID() uint8 { return Snowflake(s).PID() }
|
|
|
|
func (s WebhookID) Increment() uint16 { return Snowflake(s).Increment() }
|