mirror of https://github.com/velour/catbase.git
go fmt because apparently we're all too amazing to handle ill-formatted code, wimps
This commit is contained in:
parent
3cb54e6262
commit
fcfec95c2f
|
@ -4,12 +4,12 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"math/rand"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ type Impossible struct {
|
||||||
title string
|
title string
|
||||||
content []string
|
content []string
|
||||||
updated time.Time
|
updated time.Time
|
||||||
testing bool
|
testing bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(b bot.Bot) *Impossible {
|
func New(b bot.Bot) *Impossible {
|
||||||
|
@ -35,7 +35,7 @@ func New(b bot.Bot) *Impossible {
|
||||||
title: "",
|
title: "",
|
||||||
content: []string{},
|
content: []string{},
|
||||||
updated: getTodaysMidnight().Add(time.Hour * -24),
|
updated: getTodaysMidnight().Add(time.Hour * -24),
|
||||||
testing: false,
|
testing: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Register(i, bot.Help, i.help)
|
b.Register(i, bot.Help, i.help)
|
||||||
|
@ -51,7 +51,7 @@ func newTesting(b bot.Bot) *Impossible {
|
||||||
title: "",
|
title: "",
|
||||||
content: []string{},
|
content: []string{},
|
||||||
updated: getTodaysMidnight().Add(time.Hour * -24),
|
updated: getTodaysMidnight().Add(time.Hour * -24),
|
||||||
testing: true,
|
testing: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Register(i, bot.Help, i.help)
|
b.Register(i, bot.Help, i.help)
|
||||||
|
@ -72,12 +72,13 @@ func (p *Impossible) message(c bot.Connector, kind bot.Kind, message msg.Message
|
||||||
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("The last impossible wikipedia article was: \"%s\"", p.title))
|
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("The last impossible wikipedia article was: \"%s\"", p.title))
|
||||||
messaged = true
|
messaged = true
|
||||||
}
|
}
|
||||||
for !p.refreshImpossible() {}
|
for !p.refreshImpossible() {
|
||||||
|
}
|
||||||
|
|
||||||
if p.testing {
|
if p.testing {
|
||||||
p.b.Send(c, bot.Message, message.Channel, p.title)
|
p.b.Send(c, bot.Message, message.Channel, p.title)
|
||||||
messaged = true
|
messaged = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lowercase := strings.ToLower(message.Body)
|
lowercase := strings.ToLower(message.Body)
|
||||||
|
@ -87,7 +88,8 @@ func (p *Impossible) message(c bot.Connector, kind bot.Kind, message msg.Message
|
||||||
} else if strings.Contains(lowercase, strings.ToLower(p.title)) {
|
} else if strings.Contains(lowercase, strings.ToLower(p.title)) {
|
||||||
messaged = true
|
messaged = true
|
||||||
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
|
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
|
||||||
for !p.refreshImpossible() {}
|
for !p.refreshImpossible() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return messaged
|
return messaged
|
||||||
|
@ -103,12 +105,12 @@ func (p *Impossible) refreshImpossible() bool {
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
titleRegex := regexp.MustCompile(`id="firstHeading"[^>]*(?P<Title>[^<]*)`)
|
titleRegex := regexp.MustCompile(`id="firstHeading"[^>]*(?P<Title>[^<]*)`)
|
||||||
results := titleRegex.FindStringSubmatch(string(body))
|
results := titleRegex.FindStringSubmatch(string(body))
|
||||||
title := results[1][1:] //remove the leading <
|
title := results[1][1:] //remove the leading <
|
||||||
|
|
||||||
if title == "" {
|
if title == "" {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
p.title = title
|
p.title = title
|
||||||
p.content = []string{}
|
p.content = []string{}
|
||||||
|
@ -138,7 +140,7 @@ func (p *Impossible) refreshImpossible() bool {
|
||||||
p.content = append(p.content, censored)
|
p.content = append(p.content, censored)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTodaysMidnight() time.Time {
|
func getTodaysMidnight() time.Time {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package impossible
|
package impossible
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/velour/catbase/plugins/cli"
|
"github.com/velour/catbase/plugins/cli"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -37,23 +37,23 @@ func makePlugin(t *testing.T) (*Impossible, *bot.MockBot) {
|
||||||
func TestNothing(t *testing.T) {
|
func TestNothing(t *testing.T) {
|
||||||
p, mb := makePlugin(t)
|
p, mb := makePlugin(t)
|
||||||
p.message(makeMessage("hi"))
|
p.message(makeMessage("hi"))
|
||||||
p.message(makeMessage("nothing"))
|
p.message(makeMessage("nothing"))
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Len(t, mb.Messages, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHint(t *testing.T) {
|
func TestHint(t *testing.T) {
|
||||||
p, mb := makePlugin(t)
|
p, mb := makePlugin(t)
|
||||||
p.message(makeMessage("hi"))
|
p.message(makeMessage("hi"))
|
||||||
p.message(makeMessage("!hint"))
|
p.message(makeMessage("!hint"))
|
||||||
assert.Len(t, mb.Messages, 2)
|
assert.Len(t, mb.Messages, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCorrect(t *testing.T) {
|
func TestCorrect(t *testing.T) {
|
||||||
p, mb := makePlugin(t)
|
p, mb := makePlugin(t)
|
||||||
p.message(makeMessage("hi"))
|
p.message(makeMessage("hi"))
|
||||||
p.message(makeMessage(mb.Messages[0]))
|
p.message(makeMessage(mb.Messages[0]))
|
||||||
|
|
||||||
congrats := fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", mb.Messages[0])
|
congrats := fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", mb.Messages[0])
|
||||||
|
|
||||||
assert.Contains(t, mb.Messages[1], congrats)
|
assert.Contains(t, mb.Messages[1], congrats)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue