From 104f48b1c3720fd6d576dbfb6fa4080e56df389e Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Wed, 25 May 2022 15:13:44 -0400 Subject: [PATCH] meme: configify the default meme config --- plugins/meme/meme.go | 24 +++++++++++++++++------- plugins/meme/webHandlers.go | 4 ++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/plugins/meme/meme.go b/plugins/meme/meme.go index a617d08..cc911a6 100644 --- a/plugins/meme/meme.go +++ b/plugins/meme/meme.go @@ -170,13 +170,13 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin allConfigs := p.c.GetMap("meme.memeconfigs", map[string]string{}) configtxt, ok := allConfigs[format] if !ok { - config = defaultFormatConfig() + config = p.defaultFormatConfig() log.Debug().Msgf("Did not find %s in %+v", format, allConfigs) } else { err = json.Unmarshal([]byte(configtxt), &config) if err != nil { log.Error().Err(err).Msgf("Could not parse config for %s:\n%s", format, configtxt) - config = defaultFormatConfig() + config = p.defaultFormatConfig() } } @@ -343,15 +343,25 @@ func (p *MemePlugin) findFontSize(config []memeText, fontLocation string, w, h i return fontSize } -func defaultFormatConfig() []memeText { +func (p *MemePlugin) defaultFormatConfig() []memeText { + allConfigs := p.c.GetMap("meme.memeconfigs", map[string]string{}) + if configtxt, ok := allConfigs["default"]; ok { + var config []memeText + err := json.Unmarshal([]byte(configtxt), &config) + if err != nil { + goto ret + } + return config + } +ret: return []memeText{ - {XPerc: 0.5, YPerc: 0.05, Caps: true}, - {XPerc: 0.5, YPerc: 0.95, Caps: true}, + {XPerc: 0.5, YPerc: 0.1, Caps: true}, + {XPerc: 0.5, YPerc: 0.9, Caps: true}, } } -func defaultFormatConfigJSON() string { - c, _ := json.Marshal(defaultFormatConfig()) +func (p *MemePlugin) defaultFormatConfigJSON() string { + c, _ := json.Marshal(p.defaultFormatConfig()) return string(c) } diff --git a/plugins/meme/webHandlers.go b/plugins/meme/webHandlers.go index 8b2de30..8198660 100644 --- a/plugins/meme/webHandlers.go +++ b/plugins/meme/webHandlers.go @@ -51,7 +51,7 @@ func (p *MemePlugin) all(w http.ResponseWriter, r *http.Request) { for n, u := range memes { config, ok := configs[n] if !ok { - b, _ := json.Marshal(defaultFormatConfig()) + b, _ := json.Marshal(p.defaultFormatConfig()) config = string(b) } realURL, err := url.Parse(u) @@ -126,7 +126,7 @@ func (p *MemePlugin) addMeme(w http.ResponseWriter, r *http.Request) { checkError(err) if values.Config == "" { - values.Config = defaultFormatConfigJSON() + values.Config = p.defaultFormatConfigJSON() } configs := p.c.GetMap("meme.memeconfigs", map[string]string{}) configs[values.Name] = values.Config