cowboy: reuse some code

This commit is contained in:
Chris Sexton 2022-07-23 09:24:22 -04:00
parent 274d8f6966
commit 4c1a02e9fb
3 changed files with 8 additions and 13 deletions

View File

@ -182,14 +182,9 @@ func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *disco
p.c.Set("cowboy.lastEmojy", name) p.c.Set("cowboy.lastEmojy", name)
list = p.b.DefaultConnector().GetEmojiList(true) list = emojy.InvertEmojyList(p.b.DefaultConnector().GetEmojiList(true))
for k, v := range list { msg = fmt.Sprintf("You replaced %s with a new emojy %s <:%s:%s>, pardner!",
if v == name { lastEmojy, name, name, list[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."
resp: resp:
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{

View File

@ -53,7 +53,7 @@ func (p *EmojyPlugin) overlayCB(s *discordgo.Session, i *discordgo.InteractionCr
p.c.Set("emojy.lastEmojy", name) 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]) msg = fmt.Sprintf("You replaced %s with a new emojy %s <:%s:%s>, pardner!", lastEmojy, name, name, list[name])
resp: resp:

View File

@ -133,7 +133,7 @@ func (p *EmojyPlugin) register() {
} }
func (p *EmojyPlugin) RmEmojy(c bot.Connector, name string) error { 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 // Call a non-existent emojy a successful remove
if _, ok := onServerList[name]; !ok { if _, ok := onServerList[name]; !ok {
return fmt.Errorf("could not find emojy %s", name) 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 { 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 { if _, ok := onServerList[name]; ok {
return fmt.Errorf("emojy already exists") return fmt.Errorf("emojy already exists")
} }
@ -199,7 +199,7 @@ type EmojyCount struct {
OnServer bool `json:"onServer"` 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{} out := map[string]string{}
for k, v := range emojy { for k, v := range emojy {
out[v] = k out[v] = k
@ -209,7 +209,7 @@ func invertEmojyList(emojy map[string]string) map[string]string {
func (p *EmojyPlugin) allCounts() (map[string][]EmojyCount, error) { func (p *EmojyPlugin) allCounts() (map[string][]EmojyCount, error) {
out := map[string][]EmojyCount{} 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` q := `select emojy, count(observed) as count from emojyLog group by emojy order by count desc`
result := []EmojyCount{} result := []EmojyCount{}
err := p.db.Select(&result, q) err := p.db.Select(&result, q)