diff --git a/main.go b/main.go index 08ffe76..ce690eb 100644 --- a/main.go +++ b/main.go @@ -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) { diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..c767e16 --- /dev/null +++ b/main_test.go @@ -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) + } + }) + } +}