all repos — paprika @ b89432ecb9085662a2370a96d7541f93a307a42c

go rewrite of taigabot

Support setting location for user (#7)

* Init location plugin

* Support setting loc and getting loc for @nick
Anirudh Oppiliappan x@icyphox.sh
Sun, 21 Nov 2021 08:09:00 +0530
commit

b89432ecb9085662a2370a96d7541f93a307a42c

parent

9120dc31af6e8c5fd862b591f9f4efa90be347ef

4 files changed, 94 insertions(+), 6 deletions(-)

jump to
A plugins/location.go

@@ -0,0 +1,35 @@

+package plugins + +import ( + "fmt" + "strings" + + "git.icyphox.sh/paprika/plugins/location" + "gopkg.in/irc.v3" +) + +func init() { + Register(Location{}) +} + +type Location struct{} + +func (Location) Triggers() []string { + return []string{".loc", ".location"} +} + +func (Location) Execute(m *irc.Message) (string, error) { + parsed := strings.SplitN(m.Trailing(), " ", 2) + trigger := parsed[0] + if len(parsed) != 2 { + return fmt.Sprintf("Usage: %s <location>", trigger), nil + } + loc := parsed[1] + + err := location.SetLocation(loc, m.Prefix.Name) + if err != nil { + return "Error setting location", err + } + + return "Successfully set location", nil +}
A plugins/location/location.go

@@ -0,0 +1,25 @@

+package location + +import ( + "fmt" + + "git.icyphox.sh/paprika/database" +) + +func SetLocation(loc, nick string) error { + err := database.DB.Set( + []byte(fmt.Sprintf("loc/%s", nick)), + []byte(loc), + ) + + return err +} + +func GetLocation(nick string) (string, error) { + loc, err := database.DB.Get([]byte(fmt.Sprintf("loc/%s", nick))) + if err != nil { + return "", err + } + + return string(loc), nil +}
M plugins/weather.goplugins/weather.go

@@ -1,10 +1,11 @@

package plugins import ( - "fmt" "strings" + "git.icyphox.sh/paprika/plugins/location" "git.icyphox.sh/paprika/plugins/weather" + "github.com/dgraph-io/badger/v3" "gopkg.in/irc.v3" )

@@ -23,15 +24,42 @@ }

func (Weather) Execute(m *irc.Message) (string, error) { parsed := strings.SplitN(m.Trailing(), " ", 2) + var loc string if len(parsed) != 2 { - return fmt.Sprintf("Usage: %s <location>", parsed[0]), nil + var err error + // Check if they've already set their location + loc, err = location.GetLocation(m.Prefix.Name) + if err == badger.ErrKeyNotFound { + return "Location not set. Use '.loc <location>' to set it.", nil + } else if err != nil { + return "Error getting location", err + } } - query := parsed[1] - li, err := weather.GetLocationInfo(query) + + // They're either querying for a location or @nick. + if len(loc) == 0 { + if strings.HasPrefix(parsed[1], "@") { + // Strip '@' + var err error + loc, err = location.GetLocation(parsed[1][1:]) + if err == badger.ErrKeyNotFound { + return "Location not set. Use '.loc <location>' to set it.", nil + } else if err != nil { + return "Error getting location. Try again.", err + } + } else { + loc = parsed[1] + } + } + + li, err := location.GetLocationInfo(loc) if err != nil { - return "Error getting location info", err + return "Error getting location info. Try again.", err } + if len(li.Features) == 0 { + return "Error getting location info. Try again.", nil + } coordinates := li.Features[0].Geometry.Coordinates label := li.Features[0].Properties.Geocoding.Label info, err := weather.GetWeather(coordinates, label)
M plugins/weather/geocode.goplugins/location/geocode.go

@@ -1,4 +1,4 @@

-package weather +package location import ( "encoding/json"