2020-06-09 13:04:44 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-02-22 18:27:49 +00:00
|
|
|
_ "embed"
|
2020-06-09 13:04:44 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2020-10-25 21:17:40 +00:00
|
|
|
"github.com/pelletier/go-toml"
|
2020-06-09 13:04:44 +00:00
|
|
|
)
|
|
|
|
|
2021-05-04 16:18:15 +00:00
|
|
|
//go:embed canopeas.example.toml
|
2021-02-22 18:27:49 +00:00
|
|
|
var defaultConfig []byte
|
2020-06-09 13:04:44 +00:00
|
|
|
|
|
|
|
type Config struct {
|
2020-08-06 20:07:32 +00:00
|
|
|
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"`
|
2021-01-17 02:45:06 +00:00
|
|
|
DBPath string `toml:"db_path"`
|
|
|
|
MCPath string `toml:"mc_path"`
|
|
|
|
ImgurClientID string `toml:"imgur_client_id"`
|
2021-02-22 18:27:49 +00:00
|
|
|
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"`
|
2020-06-09 13:04:44 +00:00
|
|
|
|
|
|
|
StaffRoles []string `toml:"staff_roles"`
|
2020-06-12 16:38:18 +00:00
|
|
|
Echoes []Echo `toml:"echoes"`
|
2021-05-14 02:54:38 +00:00
|
|
|
Albums []Album `toml:"albums"`
|
2020-06-09 13:04:44 +00:00
|
|
|
MessageRoles []MessageRole `toml:"message_roles"`
|
|
|
|
RegisterRole string `toml:"register_role"`
|
|
|
|
RegisteredChannel string `toml:"registered_channel"`
|
2020-10-25 21:17:40 +00:00
|
|
|
LeaveChannel string `toml:"leave_channel"`
|
2020-06-09 13:04:44 +00:00
|
|
|
FiredRole string `toml:"fired_role"`
|
2020-07-02 20:00:22 +00:00
|
|
|
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"`
|
2020-07-02 21:52:20 +00:00
|
|
|
Compliment struct {
|
|
|
|
Verbs []string `toml:"verbs"`
|
|
|
|
Nouns []string `toml:"nouns"`
|
|
|
|
MinorThings []string `toml:"minor_things"`
|
|
|
|
} `toml:"compliment"`
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MessageRole struct {
|
2021-05-14 02:54:38 +00:00
|
|
|
ChannelID string `toml:"channel_id"`
|
|
|
|
MessageID string `toml:"message_id"`
|
2021-05-05 02:45:49 +00:00
|
|
|
Reactions []MessageReaction `toml:"reactions"`
|
|
|
|
}
|
|
|
|
type MessageReaction struct {
|
2021-05-14 02:54:38 +00:00
|
|
|
Emoji string `toml:"emoji"`
|
|
|
|
RoleID string `toml:"role_id"`
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 16:38:18 +00:00
|
|
|
type Echo struct {
|
2020-06-09 13:04:44 +00:00
|
|
|
Name string `toml:"name"`
|
|
|
|
Aliases []string `toml:"aliases"`
|
2020-06-12 16:38:18 +00:00
|
|
|
Message string `toml:"message"`
|
|
|
|
Help string `toml:"help"`
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 02:54:38 +00:00
|
|
|
type Album struct {
|
|
|
|
Name string `toml:"name"`
|
|
|
|
Aliases []string `toml:"aliases"`
|
|
|
|
AlbumID string `toml:"album_id"`
|
|
|
|
Help string `toml:"help"`
|
|
|
|
}
|
|
|
|
|
2020-06-09 13:04:44 +00:00
|
|
|
func Load(configPath string) (*Config, error) {
|
|
|
|
var err error
|
|
|
|
var configContent []byte
|
|
|
|
if len(configPath) == 0 {
|
2021-05-14 03:08:38 +00:00
|
|
|
configPath = "canopeas.toml"
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 21:17:40 +00:00
|
|
|
var cfg Config
|
|
|
|
tree, err := toml.LoadBytes(configContent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.Unmarshal(&cfg); err != nil {
|
2020-06-09 13:04:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-25 21:17:40 +00:00
|
|
|
return &cfg, nil
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|