all repos — paprika @ 8ccd2df8f599777dfee1ef536c6c23be98f1498f

go rewrite of taigabot

plugins/weather.go (view raw)

 1package plugins
 2
 3import (
 4	"fmt"
 5	"strings"
 6
 7	"git.icyphox.sh/taigobot/plugins/weather"
 8	"gopkg.in/irc.v3"
 9)
10
11func init() {
12	Register(Weather{})
13}
14
15type Weather struct{}
16
17func (Weather) Triggers() []string {
18	return []string{
19		".w",
20		".weather",
21	}
22}
23
24func (Weather) Execute(m *irc.Message) (string, error) {
25	parsed := strings.SplitN(m.Trailing(), " ", 2)
26	if len(parsed) != 2 {
27		return fmt.Sprintf("Usage: %s <location>", parsed[0]), nil
28	}
29	query := parsed[1]
30	li, err := weather.GetLocationInfo(query)
31	if err != nil {
32		return "Error getting location info", err
33	}
34
35	coordinates := li.Features[0].Geometry.Coordinates
36	label := li.Features[0].Properties.Geocoding.Label
37	info, err := weather.GetWeather(coordinates, label)
38	if err != nil {
39		return "Error getting weather data", err
40	}
41	return info, nil
42}