all repos — paprika @ 776fcd43ff010c8e1b0081ef9d7bcfc4d4073d2b

go rewrite of taigabot

main.go (view raw)

 1package main
 2
 3import (
 4	"errors"
 5	"log"
 6	"net"
 7	"strings"
 8
 9	"git.icyphox.sh/paprika/config"
10	"git.icyphox.sh/paprika/database"
11	"git.icyphox.sh/paprika/plugins"
12	"gopkg.in/irc.v3"
13)
14
15func handleChatMessage(c *irc.Client, m *irc.Message) {
16	response, err := plugins.ProcessTrigger(m)
17	
18	if errors.Is(err, plugins.NoReply) {
19		return
20	}
21
22	cmd := "PRIVMSG"
23	if errors.Is(err, plugins.IsNotice) {
24		err = nil
25		cmd = "NOTICE"
26	}
27	msg := irc.Message {Command: cmd}
28	target := m.Params[0]
29	split := strings.Split(response, "\n")
30
31	if errors.Is(err, plugins.IsRaw) {
32		for _, line := range split {
33			c.Write(line)
34		}
35	} else if err != nil {
36		log.Printf("error: %v", err)
37	} else {
38		for _, line := range split {
39			msg.Params = []string{target, line}
40			c.WriteMessage(&msg)
41		}
42		return
43	}
44}
45
46func ircHandler(c *irc.Client, m *irc.Message) {
47	switch m.Command {
48	case "001":
49		c.Write(config.SplitChannelList(config.C.Channels))
50	case "PRIVMSG":
51		handleChatMessage(c, m)
52	}
53}
54
55func main() {
56	conn, err := net.Dial("tcp", config.C.Host)
57	if err != nil {
58		log.Fatal(err)
59	}
60	defer conn.Close()
61
62	ircConfig := irc.ClientConfig{
63		Nick:    config.C.Nick,
64		Pass:    config.C.Pass,
65		User:    "paprikabot",
66		Name:    config.C.Nick,
67		Handler: irc.HandlerFunc(ircHandler),
68	}
69
70	database.DB.DB, err = database.Open()
71	if err != nil {
72		log.Fatal(err)
73	}
74	defer database.DB.Close()
75
76	client := irc.NewClient(conn, ircConfig)
77	err = client.Run()
78	if err != nil {
79		log.Fatal(err)
80	}
81}