diff --git a/CHANGELOG.md b/CHANGELOG.md index f42b93e..54cbc6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.3.0 + +- Add random fact plugin + ## 1.2.0 - Add quote of the day plugin diff --git a/main.go b/main.go index 63a620c..c1a7622 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( _ "github.com/shanedabes/ircbot/plugins/choose" _ "github.com/shanedabes/ircbot/plugins/clock" _ "github.com/shanedabes/ircbot/plugins/donger" + _ "github.com/shanedabes/ircbot/plugins/fact" _ "github.com/shanedabes/ircbot/plugins/lastfm" _ "github.com/shanedabes/ircbot/plugins/qod" _ "github.com/shanedabes/ircbot/plugins/qwantz" diff --git a/plugins/fact/fact.go b/plugins/fact/fact.go new file mode 100644 index 0000000..9a20122 --- /dev/null +++ b/plugins/fact/fact.go @@ -0,0 +1,38 @@ +package fact + +import ( + "github.com/go-chat-bot/bot" + "github.com/go-chat-bot/plugins/web" +) + +const ( + url = "https://uselessfacts.jsph.pl/random.json?language=en" +) + +type json struct { + Text string `json:"text"` +} + +func (j json) String() string { + return j.Text +} + +func fact(command *bot.Cmd) (msg string, err error) { + j := &json{} + err = web.GetJSON(url, j) + + if err != nil { + return "", err + } + + return j.String(), nil +} + +func init() { + bot.RegisterCommand( + "fact", + "Post a random fact from uselessfacts.jsph.pl", + "", + fact, + ) +} diff --git a/plugins/fact/fact_test.go b/plugins/fact/fact_test.go new file mode 100644 index 0000000..0a843c3 --- /dev/null +++ b/plugins/fact/fact_test.go @@ -0,0 +1,17 @@ +package fact + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +var ( + j = json{ + Text: "fact", + } +) + +func TestString(t *testing.T) { + assert.Equal(t, j.String(), "fact") +}