canopeas/config/config.go

124 lines
3.0 KiB
Go

package config
import (
_ "embed"
"io/ioutil"
"os"
"github.com/pelletier/go-toml"
)
//go:embed canopeas.example.toml
var defaultConfig []byte
type Config struct {
Token string `toml:"token"`
Prefix string `toml:"prefix"`
MCM struct {
Token string `toml:"token"`
URL string `toml:"url"`
} `toml:"mcm"`
Server struct {
Address string `toml:"address"`
Port int `toml:"port"`
} `toml:"server"`
DBPath string `toml:"db_path"`
MCPath string `toml:"mc_path"`
ImgurClientID string `toml:"imgur_client_id"`
ServerAPI struct {
Endpoint string `toml:"endpoint"`
Token string `toml:"token"`
} `toml:"serverapi"`
Twitter struct {
ConsumerKey string `toml:"consumer_key"`
ConsumerSecret string `toml:"consumer_secret"`
AccessToken string `toml:"access_token"`
AccessSecret string `toml:"access_secret"`
} `toml:"twitter"`
StaffRoles []string `toml:"staff_roles"`
Echoes []Echo `toml:"echoes"`
Albums []Album `toml:"albums"`
MessageRoles []MessageRole `toml:"message_roles"`
Register struct {
URL string `toml:"url"`
Role string `toml:"role"`
WelcomeChannel string `toml:"welcome_channel"`
} `toml:"register"`
LeaveChannel string `toml:"leave_channel"`
FiredRole string `toml:"fired_role"`
MemeRate string `toml:"meme_rate"`
Insult struct {
Targets []string `toml:"targets"`
Comparisons []string `toml:"comparisons"`
Adjectives []string `toml:"adjectives"`
Nouns []string `toml:"nouns"`
} `toml:"insult"`
Compliment struct {
Verbs []string `toml:"verbs"`
Nouns []string `toml:"nouns"`
MinorThings []string `toml:"minor_things"`
} `toml:"compliment"`
Welcome struct {
Channel string `toml:"channel"`
Message string `toml:"message"`
DM string `toml:"dm"`
} `toml:"welcome"`
}
type MessageRole struct {
ChannelID string `toml:"channel_id"`
MessageID string `toml:"message_id"`
Reactions []MessageReaction `toml:"reactions"`
}
type MessageReaction struct {
Emoji string `toml:"emoji"`
RoleID string `toml:"role_id"`
}
type Echo struct {
Name string `toml:"name"`
Aliases []string `toml:"aliases"`
Message string `toml:"message"`
Help string `toml:"help"`
}
type Album struct {
Name string `toml:"name"`
Aliases []string `toml:"aliases"`
AlbumID string `toml:"album_id"`
Help string `toml:"help"`
}
func Load(configPath string) (*Config, error) {
var err error
var configContent []byte
if len(configPath) == 0 {
configPath = "canopeas.toml"
}
configContent, err = ioutil.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {
if err = ioutil.WriteFile(configPath, defaultConfig, os.ModePerm); err != nil {
return nil, err
}
configContent = defaultConfig
} else {
return nil, err
}
}
var cfg Config
tree, err := toml.LoadBytes(configContent)
if err != nil {
return nil, err
}
if err := tree.Unmarshal(&cfg); err != nil {
return nil, err
}
return &cfg, nil
}