Skip to content

Commit

Permalink
add snowflake parse from int64
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jan 24, 2022
1 parent b8b0134 commit ad7f328
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ id.Int64()
// this can be used for various pagination requests to the discord api
id = NewSnowflake(time.Now())

// returns the int64 as a Snowflake
id = ParseSnowflake(123456789012345678)

// returns a snowflake from an environment variable
id = GetSnowflakeEnv("guild_id")
```
Expand Down
9 changes: 9 additions & 0 deletions snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ func NewSnowflake(timestamp time.Time) Snowflake {
return Snowflake(strconv.FormatInt(((timestamp.UnixNano()/1_000_000)-Epoch)<<22, 10))
}

// ParseSnowflake parses a Snowflake int64 into a Snowflake
//goland:noinspection GoUnusedExportedFunction
func ParseSnowflake(i int64) Snowflake {
if i == 0 {
return ""
}
return Snowflake(strconv.FormatInt(i, 10))
}

// GetSnowflakeEnv returns a new Snowflake from an environment variable
//goland:noinspection GoUnusedExportedFunction
func GetSnowflakeEnv(key string) Snowflake {
Expand Down

0 comments on commit ad7f328

Please sign in to comment.