From 1605f43d9f73405760b847f2e8164d202c36c445 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Sun, 7 Jun 2020 19:52:29 +0800 Subject: [PATCH] refactor: Move base64 generation to util --- commands/migrate.go | 18 ++---------------- util/rand.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/commands/migrate.go b/commands/migrate.go index ee0ee5a..904da1a 100644 --- a/commands/migrate.go +++ b/commands/migrate.go @@ -2,8 +2,6 @@ package commands import ( "fmt" - "math/rand" - "time" "github.com/bwmarrin/discordgo" @@ -29,25 +27,13 @@ func handleRecalculateLevel(dg *discordgo.Session, m *discordgo.MessageCreate, a ) } util.SendSuccessEmbed(dg, m.ChannelID, "Level is being recalculated.") - rand.Seed(time.Now().UnixNano()) - calculationCode = randomBase64(20) + calculationCode = util.RandomBase64(20) level.RecalculateEverything(dg, m.GuildID) util.SendSuccessEmbed(dg, m.ChannelID, "Level recalculated successfully.") } -const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+=" - var calculationCode string func init() { - rand.Seed(time.Now().UnixNano()) - calculationCode = randomBase64(20) -} - -func randomBase64(n int) string { - b := make([]byte, n) - for i := range b { - b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))] - } - return string(b) + calculationCode = util.RandomBase64(20) } diff --git a/util/rand.go b/util/rand.go index 4df0b59..cb12d05 100644 --- a/util/rand.go +++ b/util/rand.go @@ -28,3 +28,13 @@ func (s src) Seed(seed int64) { } var Rand = mrand.New(src{}) + +const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+=" + +func RandomBase64(n int) string { + b := make([]byte, n) + for i := range b { + b[i] = letterBytes[Rand.Intn(64)] + } + return string(b) +}