Milen/persistent/save.go

44 lines
729 B
Go

package persistent
import (
"io/ioutil"
"os"
"time"
"github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo"
"gitea.teamortix.com/chanbakjsd/Milen/util"
)
func init() {
data, err := ioutil.ReadFile("persistent.toml")
if err != nil {
return
}
var loaded persistent
toml.Unmarshal(data, &loaded)
if loaded.IgnoredChannels != nil {
IgnoredChannels = loaded.IgnoredChannels
}
}
func SaveLoop(dg *discordgo.Session) {
for {
time.Sleep(time.Minute)
Save(dg)
}
}
func Save(dg *discordgo.Session) {
file, err := os.Create("persistent.toml")
if err != nil {
util.ReportError(dg, err)
return
}
defer file.Close()
toml.NewEncoder(file).Encode(&persistent{
IgnoredChannels: IgnoredChannels,
})
}