all repos — paprika @ 83a983af647189c6127ba3437e8604fc5e4575d9

go rewrite of taigabot

Support setting loc and getting loc for @nick
Anirudh Oppiliappan x@icyphox.sh
Sat, 20 Nov 2021 21:38:17 +0530
commit

83a983af647189c6127ba3437e8604fc5e4575d9

parent

03ab5c9c09a15271cea0684189e708fe07754137

4 files changed, 68 insertions(+), 7 deletions(-)

jump to
M plugins/location.goplugins/location.go

@@ -4,6 +4,7 @@ import (

"fmt" "strings" + "git.icyphox.sh/paprika/plugins/location" "gopkg.in/irc.v3" )

@@ -20,8 +21,15 @@

func (Location) Execute(m *irc.Message) (string, error) { parsed := strings.SplitN(m.Trailing(), " ", 2) trigger := parsed[0] - location := parsed[1] 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"