From 4c1a02e9fb891f150cc92e19f9555ae352aef2e9 Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Sat, 23 Jul 2022 09:24:22 -0400 Subject: [PATCH] cowboy: reuse some code --- plugins/cowboy/cowboy.go | 11 +++-------- plugins/emojy/discordslash.go | 2 +- plugins/emojy/emojy.go | 8 ++++---- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/plugins/cowboy/cowboy.go b/plugins/cowboy/cowboy.go index 01a085f..a112ecb 100644 --- a/plugins/cowboy/cowboy.go +++ b/plugins/cowboy/cowboy.go @@ -182,14 +182,9 @@ func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *disco p.c.Set("cowboy.lastEmojy", name) - list = p.b.DefaultConnector().GetEmojiList(true) - for k, v := range list { - if v == name { - msg = fmt.Sprintf("You replaced %s with a new emojy %s <:%s:%s>, pardner!", lastEmojy, name, name, k) - goto resp - } - } - msg = "Something probably went wrong. I don't see your new emojy, pardner." + list = emojy.InvertEmojyList(p.b.DefaultConnector().GetEmojiList(true)) + msg = fmt.Sprintf("You replaced %s with a new emojy %s <:%s:%s>, pardner!", + lastEmojy, name, name, list[name]) resp: s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ diff --git a/plugins/emojy/discordslash.go b/plugins/emojy/discordslash.go index 505b7b8..630b910 100644 --- a/plugins/emojy/discordslash.go +++ b/plugins/emojy/discordslash.go @@ -53,7 +53,7 @@ func (p *EmojyPlugin) overlayCB(s *discordgo.Session, i *discordgo.InteractionCr p.c.Set("emojy.lastEmojy", name) - list = invertEmojyList(p.b.DefaultConnector().GetEmojiList(true)) + list = InvertEmojyList(p.b.DefaultConnector().GetEmojiList(true)) msg = fmt.Sprintf("You replaced %s with a new emojy %s <:%s:%s>, pardner!", lastEmojy, name, name, list[name]) resp: diff --git a/plugins/emojy/emojy.go b/plugins/emojy/emojy.go index d4ae3e5..93b7d9c 100644 --- a/plugins/emojy/emojy.go +++ b/plugins/emojy/emojy.go @@ -133,7 +133,7 @@ func (p *EmojyPlugin) register() { } func (p *EmojyPlugin) RmEmojy(c bot.Connector, name string) error { - onServerList := invertEmojyList(p.b.GetEmojiList(false)) + onServerList := InvertEmojyList(p.b.GetEmojiList(false)) // Call a non-existent emojy a successful remove if _, ok := onServerList[name]; !ok { return fmt.Errorf("could not find emojy %s", name) @@ -155,7 +155,7 @@ func (p *EmojyPlugin) rmEmojyHandler(r bot.Request, name string) bool { } func (p *EmojyPlugin) AddEmojy(c bot.Connector, name string) error { - onServerList := invertEmojyList(p.b.GetEmojiList(false)) + onServerList := InvertEmojyList(p.b.GetEmojiList(false)) if _, ok := onServerList[name]; ok { return fmt.Errorf("emojy already exists") } @@ -199,7 +199,7 @@ type EmojyCount struct { OnServer bool `json:"onServer"` } -func invertEmojyList(emojy map[string]string) map[string]string { +func InvertEmojyList(emojy map[string]string) map[string]string { out := map[string]string{} for k, v := range emojy { out[v] = k @@ -209,7 +209,7 @@ func invertEmojyList(emojy map[string]string) map[string]string { func (p *EmojyPlugin) allCounts() (map[string][]EmojyCount, error) { out := map[string][]EmojyCount{} - onServerList := invertEmojyList(p.b.GetEmojiList(true)) + onServerList := InvertEmojyList(p.b.GetEmojiList(true)) q := `select emojy, count(observed) as count from emojyLog group by emojy order by count desc` result := []EmojyCount{} err := p.db.Select(&result, q)