summaryrefslogtreecommitdiff
path: root/command.go
blob: 1dcb9f1c45335e2055fbedd0c11ee3ef7c733022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main

import (
	"fmt"
	"log"
	"time"

	irc "github.com/fluffle/goirc/client"
)

type Commander interface {
	irc.Handler
	fmt.Stringer
	Timeout() bool
	WithArgs(int) bool
}

type Command struct {
	Help string
	Arg  string
	Last time.Time
}

var commands = make(map[string]Commander)

func Register(cmd string, f Commander) {
	commands[cmd] = f
}

func (v Command) String() string { return v.Help }
func (v *Command) Timeout() bool {
	log.Println("timeout:", time.Since(v.Last))
	if time.Since(v.Last) > time.Minute {
		v.Last = time.Now()
		return false
	}
	return true
}
func (_ Command) WithArgs(n int) bool { return n == 1 }