privated some variables, minor clean up

This commit is contained in:
vel 2021-08-21 19:16:39 -07:00
parent e84930df6d
commit b4515ad957
Signed by: velvox
GPG Key ID: 1C8200C1D689CEF5
3 changed files with 16 additions and 14 deletions

View File

@ -117,8 +117,8 @@ func Start() {
if initProvider == nil { if initProvider == nil {
log.Fatalf("You have not chosen a database provider. Please refer to the docs") log.Fatalf("You have not chosen a database provider. Please refer to the docs")
} }
CurrentProvider = initProvider() currentProvider = initProvider()
loadGuilds() Guilds = loadGuilds()
// We need a token // We need a token
if botToken == "" { if botToken == "" {

View File

@ -32,7 +32,7 @@ var saveLock = make(map[string]*sync.Mutex)
// loadGuilds // loadGuilds
// Load all known guilds from the filesystem, from inside GuildsDir // Load all known guilds from the filesystem, from inside GuildsDir
func loadGuilds() { func loadGuilds() (guilds map[string]*framework.Guild) {
// Check if the configured guild directory exists, and create it if otherwise // Check if the configured guild directory exists, and create it if otherwise
if _, existErr := os.Stat(GuildsDir); os.IsNotExist(existErr) { if _, existErr := os.Stat(GuildsDir); os.IsNotExist(existErr) {
mkErr := os.MkdirAll(GuildsDir, 0755) mkErr := os.MkdirAll(GuildsDir, 0755)
@ -42,10 +42,11 @@ func loadGuilds() {
log.Warningf("There are no Guilds to load; data for new Guilds will be saved to: %s", GuildsDir) log.Warningf("There are no Guilds to load; data for new Guilds will be saved to: %s", GuildsDir)
// There are no guilds to load, so we can return early // There are no guilds to load, so we can return early
return return guilds
} }
// Get a list of files in the directory // Get a list of files in the directory
guilds = make(map[string]*framework.Guild)
files, rdErr := ioutil.ReadDir(GuildsDir) files, rdErr := ioutil.ReadDir(GuildsDir)
if rdErr != nil { if rdErr != nil {
log.Fatalf("Failed to read guild directory: %s", rdErr) log.Fatalf("Failed to read guild directory: %s", rdErr)
@ -98,15 +99,15 @@ func loadGuilds() {
} }
// Add the loaded guild to the map // Add the loaded guild to the map
framework.Guilds[guildId] = &framework.Guild{ guilds[guildId] = &framework.Guild{
ID: guildId, ID: guildId,
Info: gInfo, Info: gInfo,
} }
} }
if len(framework.Guilds) == 0 { if len(guilds) == 0 {
log.Warningf("There are no guilds to load; data for new guilds will be saved to \"%s\"", GuildsDir) log.Warningf("There are no guilds to load; data for new guilds will be saved to \"%s\"", GuildsDir)
return return guilds
} }
// :) // :)
@ -115,7 +116,8 @@ func loadGuilds() {
plural = "s" plural = "s"
} }
log.Infof("Loaded %d guild%s", len(framework.Guilds), plural) log.Infof("Loaded %d guild%s", len(guilds), plural)
return guilds
} }
// save // save

View File

@ -33,7 +33,7 @@ type GuildInfo struct {
// of storage types // of storage types
type GuildProvider struct { type GuildProvider struct {
Save func(guild *Guild) Save func(guild *Guild)
Load func() Load func() map[string]*Guild
} }
// Guild // Guild
@ -49,10 +49,10 @@ type Guild struct {
// Otherwise, there will be information desync // Otherwise, there will be information desync
var Guilds = make(map[string]*Guild) var Guilds = make(map[string]*Guild)
// CurrentProvider // currentProvider
// A reference to a function that provides the guild info system with a database // A reference to a function that provides the guild info system with a database
// Or similar system to save guild data. // Or similar system to save guild data.
var CurrentProvider GuildProvider var currentProvider GuildProvider
// getGuild // getGuild
// Return a Guild object corresponding to the given guildId // Return a Guild object corresponding to the given guildId
@ -116,14 +116,14 @@ func getGuild(guildId string) *Guild {
// loadGuilds // loadGuilds
// Load all known guilds from the database // Load all known guilds from the database
func loadGuilds() { func loadGuilds() map[string]*Guild {
CurrentProvider.Load() return currentProvider.Load()
} }
// save // save
// saves guild data to the database // saves guild data to the database
func (g *Guild) save() { func (g *Guild) save() {
CurrentProvider.Save(g) currentProvider.Save(g)
} }
// GetMember // GetMember