meme: configify the default meme config

This commit is contained in:
Chris Sexton 2022-05-25 15:13:44 -04:00
parent 69cee328e7
commit 104f48b1c3
2 changed files with 19 additions and 9 deletions

View File

@ -170,13 +170,13 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin
allConfigs := p.c.GetMap("meme.memeconfigs", map[string]string{}) allConfigs := p.c.GetMap("meme.memeconfigs", map[string]string{})
configtxt, ok := allConfigs[format] configtxt, ok := allConfigs[format]
if !ok { if !ok {
config = defaultFormatConfig() config = p.defaultFormatConfig()
log.Debug().Msgf("Did not find %s in %+v", format, allConfigs) log.Debug().Msgf("Did not find %s in %+v", format, allConfigs)
} else { } else {
err = json.Unmarshal([]byte(configtxt), &config) err = json.Unmarshal([]byte(configtxt), &config)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Could not parse config for %s:\n%s", format, configtxt) 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 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{ return []memeText{
{XPerc: 0.5, YPerc: 0.05, Caps: true}, {XPerc: 0.5, YPerc: 0.1, Caps: true},
{XPerc: 0.5, YPerc: 0.95, Caps: true}, {XPerc: 0.5, YPerc: 0.9, Caps: true},
} }
} }
func defaultFormatConfigJSON() string { func (p *MemePlugin) defaultFormatConfigJSON() string {
c, _ := json.Marshal(defaultFormatConfig()) c, _ := json.Marshal(p.defaultFormatConfig())
return string(c) return string(c)
} }

View File

@ -51,7 +51,7 @@ func (p *MemePlugin) all(w http.ResponseWriter, r *http.Request) {
for n, u := range memes { for n, u := range memes {
config, ok := configs[n] config, ok := configs[n]
if !ok { if !ok {
b, _ := json.Marshal(defaultFormatConfig()) b, _ := json.Marshal(p.defaultFormatConfig())
config = string(b) config = string(b)
} }
realURL, err := url.Parse(u) realURL, err := url.Parse(u)
@ -126,7 +126,7 @@ func (p *MemePlugin) addMeme(w http.ResponseWriter, r *http.Request) {
checkError(err) checkError(err)
if values.Config == "" { if values.Config == "" {
values.Config = defaultFormatConfigJSON() values.Config = p.defaultFormatConfigJSON()
} }
configs := p.c.GetMap("meme.memeconfigs", map[string]string{}) configs := p.c.GetMap("meme.memeconfigs", map[string]string{})
configs[values.Name] = values.Config configs[values.Name] = values.Config