Skip to content

Commit

Permalink
Add convertIDtoCreationTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jyap808 committed Jul 7, 2023
1 parent ae05996 commit 68e74ad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,19 @@ func terminateMember(s *discordgo.Session, guildID string, userID string, reason
}
}

// Ported from https://github.com/hugonun/discordid2date/blob/master/main.js#L5
func convertIDtoCreationTime(id string) time.Time {
idInt, _ := strconv.ParseInt(id, 10, 64)
bin := strconv.FormatInt(idInt, 2)
unixbin := ""
m := 64 - len(bin)
unixbin = bin[0 : 42-m]
unixInt, _ := strconv.ParseInt(unixbin, 2, 64)
unixCreationTime := unixInt + 1420070400000
creationTime := time.Unix(0, unixCreationTime*int64(time.Millisecond))
return creationTime
}

// This function is called on GuildMemberAdd event
// Currently just performs Flood handling
func guildMemberAdd(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
Expand Down
35 changes: 35 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"reflect"
"testing"
"time"
)

func Test_convertIDtoCreationTime(t *testing.T) {
timestamp := "2017-01-28 06:02:39.924 +0000 UTC"
timeObj, _ := time.Parse("2006-01-02 15:04:05.999 -0700 MST", timestamp)

type args struct {
id string
}
tests := []struct {
name string
args args
want time.Time
}{
{
name: "274781269861072896",
args: args{"274781269861072896"},
want: timeObj,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := convertIDtoCreationTime(tt.args.id).UTC() // Convert to UTC
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("convertIDtoCreationTime() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 68e74ad

Please sign in to comment.