all repos — paprika @ 4713a97cc5df6d8f4d9bd48f83fa1d8429ac58d4

go rewrite of taigabot

plugins/weather.go (view raw)

 1package plugins
 2
 3import (
 4	"strings"
 5
 6	"git.icyphox.sh/paprika/plugins/location"
 7	"git.icyphox.sh/paprika/plugins/time"
 8	"git.icyphox.sh/paprika/plugins/weather"
 9	"github.com/dgraph-io/badger/v3"
10	"gopkg.in/irc.v3"
11)
12
13func init() {
14	Register(Weather{})
15}
16
17type Weather struct{}
18
19func (Weather) Triggers() []string {
20	return []string{
21		".w",
22		".weather",
23		".t",
24		".time",
25	}
26}
27
28func (Weather) Execute(m *irc.Message) (string, error) {
29	parsed := strings.SplitN(m.Trailing(), " ", 2)
30
31	// TODO: AAAAAAAAAAAAAAAAAAAAAAAAAAA
32	found := false
33	trigger := parsed[0]
34	for _, v := range (Weather{}).Triggers() {
35		if trigger == v {
36			found = true
37			break
38		}
39	}
40	if !found {
41		return "", NoReply
42	}
43
44	var loc string
45	if len(parsed) != 2 {
46		var err error
47		// Check if they've already set their location
48		loc, err = location.GetLocation(m.Prefix.Name)
49		if err == badger.ErrKeyNotFound {
50			return "Location not set. Use '.loc <location>' to set it.", nil
51		} else if err != nil {
52			return "Error getting location", err
53		}
54	}
55
56	// They're either querying for a location or @nick.
57	if len(loc) == 0 {
58		if strings.HasPrefix(parsed[1], "@") {
59			// Strip '@'
60			var err error
61			loc, err = location.GetLocation(parsed[1][1:])
62			if err == badger.ErrKeyNotFound {
63				return "Location not set. Use '.loc <location>' to set it.", nil
64			} else if err != nil {
65				return "Error getting location. Try again.", err
66			}
67		} else {
68			loc = parsed[1]
69		}
70	}
71
72	li, err := location.GetLocationInfo(loc)
73	if err != nil {
74		return "Error getting location info. Try again.", err
75	}
76
77	if len(li.Features) == 0 {
78		return "Error getting location info. Try again.", nil
79	}
80	coordinates := li.Features[0].Geometry.Coordinates
81	label := li.Features[0].Properties.Geocoding.Label
82
83	switch trigger {
84	case ".t", ".time":
85		time, err := time.GetTime(coordinates, label)
86		if err != nil {
87			return "Error getting time data", err
88		}
89		return time, nil
90	case ".w", ".weather":
91
92		info, err := weather.GetWeather(coordinates, label)
93		if err != nil {
94			return "Error getting weather data", err
95		}
96		return info, nil
97	}
98	return "", nil
99}