summaryrefslogtreecommitdiffstats
path: root/bot/frog.go
blob: ea5afbdc5e9be73c7f3ef2c549ed39316ca5560f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2015 Robert S. Gerus. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package bot

import (
	"encoding/json"
	"fmt"
	"math/rand"
	"strings"

	"github.com/arachnist/gorepost/irc"
)

type tip struct {
	Number int    `json:"number"`
	Tip    string `json:"tip"`
}

type tips struct {
	Tips []tip `json:"tips"`
}

func frog(output func(irc.Message), msg irc.Message) {
	var values tips

	if strings.Split(msg.Trailing, " ")[0] != ":frog" {
		return
	}

	data, err := httpGet("http://frog.tips/api/1/tips/")
	if err != nil {
		output(reply(msg, fmt.Sprint("error:", err)))
		return
	}

	err = json.Unmarshal(data, &values)
	if err != nil {
		output(reply(msg, fmt.Sprint("error:", err)))
		return
	}

	tip := values.Tips[rand.Intn(len(values.Tips))]

	output(reply(msg, fmt.Sprintf("frog tip #%d: %s", tip.Number, tip.Tip)))
}

func init() {
	addCallback("PRIVMSG", "frog", frog)
}