Compare commits

..

2 Commits

Author SHA1 Message Date
Chris Sexton 8817df15f0 nerdepedia: fix failing tests and add config 2020-10-24 14:12:13 -04:00
Chris Sexton af5717e9c1 meme: maybe fix the bully meme 2020-10-24 13:47:45 -04:00
3 changed files with 84 additions and 43 deletions

View File

@ -102,7 +102,7 @@ func (p *MemePlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, a
return true
}
func (p *MemePlugin) bully(c bot.Connector, format, id string) image.Image {
func (p *MemePlugin) bully(c bot.Connector, format, id string) (image.Image, string) {
bullyIcon := ""
for _, bully := range p.c.GetArray("meme.bully", []string{}) {
@ -114,13 +114,14 @@ func (p *MemePlugin) bully(c bot.Connector, format, id string) image.Image {
}
formats := p.c.GetMap("meme.memes", defaultFormats)
format = randEntry(formats)
log.Debug().Msgf("After bullying:\nFormat: %s", format)
break
}
}
if u, err := c.Profile(id); bullyIcon == "" && err == nil {
if u.IconImg != nil {
return u.IconImg
return u.IconImg, format
}
bullyIcon = u.Icon
}
@ -133,7 +134,7 @@ func (p *MemePlugin) bully(c bot.Connector, format, id string) image.Image {
if err != nil {
log.Error().Err(err).Msg("error downloading bully icon")
}
return bullyImg
return bullyImg, format
}
func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID string, from *user.User, text string) {
@ -191,7 +192,7 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin
}
}
bullyImg := p.bully(c, format, from.ID)
bullyImg, format := p.bully(c, format, from.ID)
id, w, h, err := p.genMeme(format, bullyImg, config)
if err != nil {

View File

@ -21,6 +21,18 @@ const (
closingTagSuffix = "\" />"
)
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
var (
client HTTPClient
)
func init() {
client = &http.Client{}
}
type NerdepediaPlugin struct {
bot bot.Bot
config *config.Config
@ -37,26 +49,50 @@ func New(b bot.Bot) *NerdepediaPlugin {
return np
}
func defaultSites() map[string]string {
starWars := "http://starwars.wikia.com/wiki/Special:Random"
starTrek := "http://memory-alpha.wikia.com/wiki/Special:Random"
dune := "http://dune.wikia.com/wiki/Special:Random"
lotr := "http://lotr.wikia.com/wiki/Special:Random"
pokemon := "http://pokemon.wikia.com/wiki/Special:Random"
return map[string]string{
"may the force be with you": starWars,
"help me obi-wan": starWars,
"beam me up scotty": starTrek,
"live long and prosper": starTrek,
"bless the maker": dune,
"i must not fear": dune,
"the spice must flow": dune,
"my precious": lotr,
"one ring to rule them all": lotr,
"one does not simply walk into mordor": lotr,
"pikachu i choose you": pokemon,
"gotta catch em all": pokemon,
}
}
// Message responds to the bot hook on recieving messages.
// This function returns true if the plugin responds in a meaningful way to the users message.
// Otherwise, the function returns false and the bot continues execution of other plugins.
func (p *NerdepediaPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
lowerCase := strings.ToLower(message.Body)
query := ""
if lowerCase == "may the force be with you" || lowerCase == "help me obi-wan" {
query = "http://starwars.wikia.com/wiki/Special:Random"
} else if lowerCase == "beam me up scotty" || lowerCase == "live long and prosper" {
query = "http://memory-alpha.wikia.com/wiki/Special:Random"
} else if lowerCase == "bless the maker" || lowerCase == "i must not fear" || lowerCase == "the spice must flow" {
query = "http://dune.wikia.com/wiki/Special:Random"
} else if lowerCase == "my precious" || lowerCase == "one ring to rule them all" || lowerCase == "one does not simply walk into mordor" {
query = "http://lotr.wikia.com/wiki/Special:Random"
} else if lowerCase == "pikachu i choose you" || lowerCase == "gotta catch em all" {
query = "http://pokemon.wikia.com/wiki/Special:Random"
queries := p.config.GetMap("nerdepedia.sites", defaultSites())
for k, v := range queries {
if lowerCase == k {
query = v
break
}
}
if query != "" {
resp, err := http.Get(query)
req, _ := http.NewRequest(http.MethodGet, query, nil)
resp, err := client.Do(req)
if err != nil {
return false
}

View File

@ -3,16 +3,42 @@
package nerdepedia
import (
"github.com/velour/catbase/plugins/cli"
"bytes"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/plugins/cli"
"github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
)
var body = []byte(`
<meta name="description" content="Refresher Reading was a recurring feature appearing in Star Wars Insider. 20 Things You Didn&#039;t Know About the Tantive IV 20 Things You Didn&#039;t Know About the Mos Eisley Cantina 20 Things You Didn&#039;t Know About the Massassi Temples"/>
<link rel="canonical" href="https://starwars.fandom.com/wiki/Refresher_Reading"/>`)
type MockClient struct {
Status int
Body io.ReadCloser
Err error
}
func (cl MockClient) Do(req *http.Request) (*http.Response, error) {
log.Debug().Msgf("Returning mock response")
return &http.Response{
StatusCode: cl.Status,
Body: cl.Body,
}, cl.Err
}
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
isCmd := strings.HasPrefix(payload, "!")
if isCmd {
@ -30,34 +56,12 @@ func TestWars(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
client = MockClient{
Status: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(body)),
Err: nil,
}
res := c.message(makeMessage("help me obi-wan"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
}
func TestTrek(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("live long and prosper"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
}
func TestDune(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("bless the maker"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
}
func TestPoke(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("gotta catch em all"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
}