From 7c1c2ffad83317bcab14e90f124a64882c99a5e4 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 28 Sep 2015 18:41:59 +0200 Subject: Store last city --- weather.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'weather.go') diff --git a/weather.go b/weather.go index bada901..cf5265d 100644 --- a/weather.go +++ b/weather.go @@ -8,17 +8,26 @@ import ( irc "github.com/fluffle/goirc/client" ) -type Weather struct{ Command } +type Weather struct { + Command + lastCity map[string]string +} func (_ Weather) WithArgs(_ int) bool { return true } -func (_ Weather) Handle(conn *irc.Conn, line *irc.Line) { - if q := strings.SplitN(line.Text(), " ", 2); len(q) == 2 { - if c, err := weather.ByCityName(q[1]); err != nil { - conn.Notice(line.Target(), err.Error()) - } else { - conn.Notice(line.Target(), fmt.Sprint(c)) - } +func (w *Weather) Handle(conn *irc.Conn, line *irc.Line) { + var city string + q := strings.SplitN(line.Text(), " ", 2) + if len(q) == 2 { + city = q[1] + } else if l, ok := w.lastCity[line.Nick]; ok { + city = l + } + if c, err := weather.ByCityName(city); err != nil { + conn.Notice(line.Target(), err.Error()) + } else { + conn.Notice(line.Target(), fmt.Sprint(c)) + w.lastCity[line.Nick] = city } } @@ -27,5 +36,5 @@ func (_ Weather) Help() string { } func init() { - Register("weather", &Weather{}) + Register("weather", &Weather{lastCity: make(map[string]string)}) } -- cgit v1.2.3