From 16a5f224487b377fa81231b16e33a31d8f4c0e68 Mon Sep 17 00:00:00 2001 From: Daniel Aberger Date: Wed, 14 Apr 2021 15:07:13 +0200 Subject: [PATCH] use yaml config file instead of flags --- config.sample.yaml | 8 +++++++ go.mod | 1 + go.sum | 3 +++ main.go | 59 +++++++++++++++++++++++++++++++--------------- 4 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 config.sample.yaml diff --git a/config.sample.yaml b/config.sample.yaml new file mode 100644 index 0000000..a0dc956 --- /dev/null +++ b/config.sample.yaml @@ -0,0 +1,8 @@ +token: "" +shard: "" +shardcount: "" +owner: "" +port: 8080 +redirecturl: "https://bot.example.org:1337" +ci: 0 +cs: "" \ No newline at end of file diff --git a/go.mod b/go.mod index b30fa57..d08ebe3 100644 --- a/go.mod +++ b/go.mod @@ -9,4 +9,5 @@ require ( github.com/gorilla/sessions v1.2.1 github.com/sirupsen/logrus v1.8.1 golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 + gopkg.in/yaml.v2 v2.2.2 ) diff --git a/go.sum b/go.sum index d327476..db87b58 100644 --- a/go.sum +++ b/go.sum @@ -114,8 +114,10 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -370,6 +372,7 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/main.go b/main.go index ec7553b..730fd84 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "bytes" "encoding/binary" - "flag" "fmt" "io" "io/ioutil" @@ -20,6 +19,7 @@ import ( "github.com/bwmarrin/discordgo" humanize "github.com/dustin/go-humanize" log "github.com/sirupsen/logrus" + "gopkg.in/yaml.v2" ) var ( @@ -609,33 +609,54 @@ func deleteCommandMessage(s *discordgo.Session, channelID string, messageID stri } } +type config struct { + Token string `yaml:"token"` + Shard string `yaml:"shard"` + ShardCount string `yaml:"shardcount"` + Owner string `yaml:"owner"` + Port int `yaml:"port"` + RedirectURL string `yaml:"redirecturl"` + Ci int `yaml:"ci"` + Cs string `yaml:"cs"` +} + +func loadConfigFile() *config { + config := &config{} + configFile, err := os.Open("config.yaml") + if err != nil { + log.Warningln("Could not load config file.", err) + return nil + } + defer configFile.Close() + + d := yaml.NewDecoder(configFile) + + if err := d.Decode(&config); err != nil { + return nil + } + + return config +} + func main() { + config := loadConfigFile() var ( - Token = flag.String("t", "", "Discord Authentication Token") - Shard = flag.String("s", "", "Shard ID") - ShardCount = flag.String("c", "", "Number of shards") - Owner = flag.String("o", "", "Owner ID") - Port = flag.Int("p", 0, "Web server port") - RedirectURL = flag.String("r", "", "Address where the web server will be available without slash at the end. For example: \"http://bot.example.org:12345\"") - Ci = flag.Int("ci", 0, "ClientID") - Cs = flag.String("cs", "", "ClientSecret") - err error + err error ) - flag.Parse() // create SoundCollections by scanning the audio folder createCollections() // Start Webserver if a valid port is provided and if ClientID and ClientSecret are set - if *Port != 0 && *Port >= 1 && *Ci != 0 && *Cs != "" && *RedirectURL != "" { - log.Infoln("Starting web server on port " + strconv.Itoa(*Port)) - go startWebServer(strconv.Itoa(*Port), strconv.Itoa(*Ci), *Cs, *RedirectURL) + if config.Port != 0 && config.Port >= 1 && config.Ci != 0 && config.Cs != "" && config.RedirectURL != "" { + log.Infoln("Starting web server on port " + strconv.Itoa(config.Port)) + go startWebServer(strconv.Itoa(config.Port), strconv.Itoa(config.Ci), config.Cs, config.RedirectURL) } else { log.Infoln("Required web server arguments missing or invalid. Skipping web server start.") } - if *Owner != "" { - OWNER = *Owner + if config.Owner != "" { + OWNER = config.Owner } // Preload all the sounds @@ -646,7 +667,7 @@ func main() { // Create a discord session log.Info("Starting discord session...") - discord, err = discordgo.New("Bot " + *Token) + discord, err = discordgo.New("Bot " + config.Token) if err != nil { log.WithFields(log.Fields{ "error": err, @@ -655,8 +676,8 @@ func main() { } // Set sharding info - discord.ShardID, _ = strconv.Atoi(*Shard) - discord.ShardCount, _ = strconv.Atoi(*ShardCount) + discord.ShardID, _ = strconv.Atoi(config.Shard) + discord.ShardCount, _ = strconv.Atoi(config.ShardCount) if discord.ShardCount <= 0 { discord.ShardCount = 1