all repos — paprika @ b89432ecb9085662a2370a96d7541f93a307a42c

go rewrite of taigabot

plugins/plugins.go (view raw)

 1package plugins
 2
 3import (
 4	"strings"
 5
 6	"gopkg.in/irc.v3"
 7)
 8
 9type Plugin interface {
10	Triggers() []string
11	Execute(m *irc.Message) (string, error)
12}
13
14var Plugins = make(map[string]Plugin)
15
16func Register(p Plugin) {
17	for _, t := range p.Triggers() {
18		Plugins[t] = p
19	}
20}
21
22// Checks for triggers in a message and executes its
23// corresponding plugin, returning the response/error.
24func ProcessTrigger(m *irc.Message) (string, error) {
25	var (
26		response string
27		err      error
28	)
29	for trigger, plugin := range Plugins {
30		if strings.HasPrefix(m.Trailing(), trigger) {
31			response, err = plugin.Execute(m)
32			if err != nil {
33				return "", err
34			}
35			return response, nil
36		}
37	}
38	return "", nil
39}