mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-03 13:34:27 +00:00
83 lines
1.4 KiB
Go
83 lines
1.4 KiB
Go
package discord
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const DiscordEpoch = 1420070400000 * int64(time.Millisecond)
|
|
|
|
type Snowflake int64
|
|
|
|
func NewSnowflake(t time.Time) Snowflake {
|
|
return Snowflake(TimeToDiscordEpoch(t) << 22)
|
|
}
|
|
|
|
func ParseSnowflake(sf string) (Snowflake, error) {
|
|
i, err := strconv.ParseInt(sf, 10, 64)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return Snowflake(i), nil
|
|
}
|
|
|
|
func (s *Snowflake) UnmarshalJSON(v []byte) error {
|
|
id := strings.Trim(string(v), `"`)
|
|
if id == "null" {
|
|
return nil
|
|
}
|
|
|
|
i, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*s = Snowflake(i)
|
|
return nil
|
|
}
|
|
|
|
func (s *Snowflake) MarshalJSON() ([]byte, error) {
|
|
var id string
|
|
|
|
switch i := int64(*s); i {
|
|
case -1: // @me
|
|
id = "@me"
|
|
case 0:
|
|
return []byte("null"), nil
|
|
default:
|
|
id = strconv.FormatInt(i, 10)
|
|
}
|
|
|
|
return []byte(`"` + id + `"`), nil
|
|
}
|
|
|
|
func (s Snowflake) String() string {
|
|
return strconv.FormatUint(uint64(s), 10)
|
|
}
|
|
|
|
func (s Snowflake) Valid() bool {
|
|
return uint64(s) > 0
|
|
}
|
|
|
|
func (s Snowflake) Time() time.Time {
|
|
return time.Unix(0, int64(s)>>22*1000000+DiscordEpoch)
|
|
}
|
|
|
|
func (s Snowflake) Worker() uint8 {
|
|
return uint8(s & 0x3E0000)
|
|
}
|
|
|
|
func (s Snowflake) PID() uint8 {
|
|
return uint8(s & 0x1F000 >> 12)
|
|
}
|
|
|
|
func (s Snowflake) Increment() uint16 {
|
|
return uint16(s & 0xFFF)
|
|
}
|
|
|
|
func TimeToDiscordEpoch(t time.Time) int64 {
|
|
return t.UnixNano()/int64(time.Millisecond) - DiscordEpoch
|
|
}
|