all repos — paprika @ 25de5b6c6af4b418eb4cb6ef74eb26540f50ddd6

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	split := strings.Split(response, "\n")
18
19	if plugins.IsReplyT(err) {
20		r := err.(*plugins.ReplyT)
21		r.ApplyFlags(m)
22
23		for _, line := range split {
24			c.WriteMessage(&irc.Message{
25				Command: m.Command,
26				Params: []string {
27					m.Params[0],
28					line,
29				},
30			})
31		}
32	} else if err != nil {
33		if !errors.Is(err, plugins.NoReply) {
34			log.Printf("error: %v", err)
35		}
36	} else {
37		for _, line := range split {
38			c.WriteMessage(&irc.Message{
39				Command: "PRIVMSG",
40				Params: []string{
41					m.Params[0],
42					line,
43				},
44			})
45		}
46	}
47}
48
49func ircHandler(c *irc.Client, m *irc.Message) {
50	switch m.Command {
51	case "001":
52		c.Write(config.SplitChannelList(config.C.Channels))
53	case "PRIVMSG":
54		handleChatMessage(c, m)
55	}
56}
57
58func main() {
59	conn, err := net.Dial("tcp", config.C.Host)
60	if err != nil {
61		log.Fatal(err)
62	}
63	defer conn.Close()
64
65	ircConfig := irc.ClientConfig{
66		Nick:    config.C.Nick,
67		Pass:    config.C.Pass,
68		User:    "paprikabot",
69		Name:    config.C.Nick,
70		Handler: irc.HandlerFunc(ircHandler),
71	}
72
73	database.DB.DB, err = database.Open()
74	if err != nil {
75		log.Fatal(err)
76	}
77	defer database.DB.Close()
78
79	client := irc.NewClient(conn, ircConfig)
80	err = client.Run()
81	if err != nil {
82		log.Fatal(err)
83	}
84}