From dcec8549b4a91863d6f8820582bd757447a6b408 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 21 Sep 2015 23:27:29 +0200 Subject: Move Help() into interface{} --- blame.go | 11 ++++++----- command.go | 16 ++++++---------- duck.go | 11 ++++++----- help.go | 14 +++++++------- last.go | 10 +++++----- rfc.go | 10 +++++----- roulette.go | 10 +++++----- theo.go | 10 +++++----- top.go | 10 +++++----- urban.go | 11 ++++++----- weather.go | 11 ++++++----- 11 files changed, 62 insertions(+), 62 deletions(-) diff --git a/blame.go b/blame.go index 20fc560..94356cb 100644 --- a/blame.go +++ b/blame.go @@ -8,6 +8,7 @@ type Blame struct{ Command } func (_ Blame) Handle(conn *irc.Conn, line *irc.Line) { src := []string{ + "Author: Dimitri Sokolyuk ", "Source: http://cgit.dim13.org/bot.git", "Install: go get dim13.org/bot", } @@ -16,10 +17,10 @@ func (_ Blame) Handle(conn *irc.Conn, line *irc.Line) { } } +func (_ Blame) Help() string { + return "Blame author and return link to source code" +} + func init() { - Register("blame", &Blame{ - Command{ - Help: "Blame author and return link to source code", - }, - }) + Register("blame", &Blame{}) } diff --git a/command.go b/command.go index e4ee147..b2bdb59 100644 --- a/command.go +++ b/command.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" "strings" "time" @@ -11,15 +10,13 @@ import ( type Commander interface { irc.Handler - fmt.Stringer Timeout(string) bool WithArgs(int) bool + Help() string } type Command struct { - Help string - Arg string - Last map[string]time.Time + last map[string]time.Time } var commands = make(map[string]Commander) @@ -28,13 +25,12 @@ func Register(cmd string, f Commander) { commands[cmd] = f } -func (v Command) String() string { return v.Help } func (v *Command) Timeout(nick string) bool { - defer func() { v.Last[nick] = time.Now() }() - if v.Last == nil { - v.Last = make(map[string]time.Time) + defer func() { v.last[nick] = time.Now() }() + if v.last == nil { + v.last = make(map[string]time.Time) } - if last, ok := v.Last[nick]; ok { + if last, ok := v.last[nick]; ok { if to := time.Since(last); to < 5*time.Second { log.Println(nick, "timeout", to) return true diff --git a/duck.go b/duck.go index c3df947..1d126e1 100644 --- a/duck.go +++ b/duck.go @@ -10,6 +10,7 @@ import ( type Duck struct{ Command } func (_ Duck) WithArgs(_ int) bool { return true } + func (_ Duck) Handle(conn *irc.Conn, line *irc.Line) { if q := strings.SplitN(line.Text(), " ", 2); len(q) == 2 { if a, err := duck.Abstract(q[1]); err != nil { @@ -20,10 +21,10 @@ func (_ Duck) Handle(conn *irc.Conn, line *irc.Line) { } } +func (_ Duck) Help() string { + return "Perform nuckduckgo instant answer search" +} + func init() { - Register("define", &Duck{ - Command{ - Help: "Perform duckduckgo instant answer search", - }, - }) + Register("define", &Duck{}) } diff --git a/help.go b/help.go index c505d45..cc09f5a 100644 --- a/help.go +++ b/help.go @@ -12,18 +12,18 @@ type Help struct{ Command } func (_ Help) Handle(conn *irc.Conn, line *irc.Line) { var msg []string for k, v := range commands { - msg = append(msg, fmt.Sprintf("%-8s%v", k, v)) + msg = append(msg, fmt.Sprintf("%-8s%v", k, v.Help())) } sort.Sort(sort.StringSlice(msg)) for _, s := range msg { - conn.Notice(line.Nick, s) + conn.Notice(line.Target(), s) } } +func (_ Help) Help() string { + return "This help" +} + func init() { - Register("help", &Help{ - Command{ - Help: "This help", - }, - }) + Register("help", &Help{}) } diff --git a/last.go b/last.go index cf9879b..e7486ee 100644 --- a/last.go +++ b/last.go @@ -23,15 +23,15 @@ func (_ Last) Handle(conn *irc.Conn, line *irc.Line) { }) } +func (_ Last) Help() string { + return "Renurn last 10 messages" +} + func Push(line *irc.Line) { buffer.Value = line buffer = buffer.Next() } func init() { - Register("last", &Last{ - Command{ - Help: "Return last 10 messages", - }, - }) + Register("last", &Last{}) } diff --git a/rfc.go b/rfc.go index 273cc0f..3b916bb 100644 --- a/rfc.go +++ b/rfc.go @@ -29,6 +29,10 @@ func (_ RFC) Handle(conn *irc.Conn, line *irc.Line) { } } +func (_ RFC) Help() string { + return "search rfc by keyword" +} + func ExtractRFC(conn *irc.Conn, line *irc.Line) { for _, v := range strings.Fields(line.Text()) { var id int @@ -61,9 +65,5 @@ func init() { } } } - Register("rfc", &RFC{ - Command{ - Help: "search rfc by keyword", - }, - }) + Register("rfc", &RFC{}) } diff --git a/roulette.go b/roulette.go index d70374e..961b8e0 100644 --- a/roulette.go +++ b/roulette.go @@ -50,12 +50,12 @@ func (_ RR) Handle(conn *irc.Conn, line *irc.Line) { } } +func (_ RR) Help() string { + return "Russian roulette" +} + func init() { rand.Seed(time.Now().Unix()) reload() - Register("!", &RR{ - Command{ - Help: "Russian roulette", - }, - }) + Register("!", &RR{}) } diff --git a/theo.go b/theo.go index 2da46e2..fae369b 100644 --- a/theo.go +++ b/theo.go @@ -11,10 +11,10 @@ func (_ Theo) Handle(conn *irc.Conn, line *irc.Line) { conn.Notice(line.Target(), theo.Theo()) } +func (_ Theo) Help() string { + return "Quote Theo De Raadt" +} + func init() { - Register("theo", &Theo{ - Command{ - Help: "Quote Theo De Raadt", - }, - }) + Register("theo", &Theo{}) } diff --git a/top.go b/top.go index 80c40d9..bcccc64 100644 --- a/top.go +++ b/top.go @@ -17,10 +17,10 @@ func (_ Top) Handle(conn *irc.Conn, line *irc.Line) { conn.Notice(line.Target(), s) } +func (_ Top) Help() string { + return "Top 10 flooder (msg private to see top 100)" +} + func init() { - Register("top", &Top{ - Command{ - Help: "Top 10 flooder (msg private to see top 100)", - }, - }) + Register("top", &Top{}) } diff --git a/urban.go b/urban.go index 49007fb..91abc6c 100644 --- a/urban.go +++ b/urban.go @@ -11,6 +11,7 @@ import ( type Urban struct{ Command } func (_ Urban) WithArgs(_ int) bool { return true } + func (_ Urban) Handle(conn *irc.Conn, line *irc.Line) { if q := strings.SplitN(line.Text(), " ", 2); len(q) == 2 { if u, err := urban.QueryTop(q[1]); err != nil { @@ -22,10 +23,10 @@ func (_ Urban) Handle(conn *irc.Conn, line *irc.Line) { } } +func (_ Urban) Help() string { + return "Perform urban dictionary search" +} + func init() { - Register("urban", &Urban{ - Command{ - Help: "Perform urban dictionary search", - }, - }) + Register("urban", &Urban{}) } diff --git a/weather.go b/weather.go index 1c9792b..bada901 100644 --- a/weather.go +++ b/weather.go @@ -11,6 +11,7 @@ import ( type Weather struct{ Command } 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 { @@ -21,10 +22,10 @@ func (_ Weather) Handle(conn *irc.Conn, line *irc.Line) { } } +func (_ Weather) Help() string { + return "Fetch current weather" +} + func init() { - Register("weather", &Weather{ - Command{ - Help: "Fetch current weather", - }, - }) + Register("weather", &Weather{}) } -- cgit v1.2.3