refactor: Move base64 generation to util

master
Luther Wen Xu 2020-06-07 19:52:29 +07:00
parent 75b91331a7
commit 1605f43d9f
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
2 changed files with 12 additions and 16 deletions

@ -2,8 +2,6 @@ package commands
import ( import (
"fmt" "fmt"
"math/rand"
"time"
"github.com/bwmarrin/discordgo" "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.") util.SendSuccessEmbed(dg, m.ChannelID, "Level is being recalculated.")
rand.Seed(time.Now().UnixNano()) calculationCode = util.RandomBase64(20)
calculationCode = randomBase64(20)
level.RecalculateEverything(dg, m.GuildID) level.RecalculateEverything(dg, m.GuildID)
util.SendSuccessEmbed(dg, m.ChannelID, "Level recalculated successfully.") util.SendSuccessEmbed(dg, m.ChannelID, "Level recalculated successfully.")
} }
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+="
var calculationCode string var calculationCode string
func init() { func init() {
rand.Seed(time.Now().UnixNano()) calculationCode = util.RandomBase64(20)
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)
} }

@ -28,3 +28,13 @@ func (s src) Seed(seed int64) {
} }
var Rand = mrand.New(src{}) 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)
}