-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.go
75 lines (65 loc) · 1.99 KB
/
irc.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"; "log"
"regexp"
"github.com/lrstanley/girc"
)
var IEmojiRegex *regexp.Regexp
var IMentionRegex *regexp.Regexp
var IUserDChannels map[string]string // map IRC username → Discord Channel
var IBot *girc.Client
func IOnConnect(c *girc.Client, e girc.Event) {
ch := Config.IRCChannel
c.Cmd.Join(ch)
log.Printf("Joining %s\n", ch)
}
func IOnPrivMsg(c *girc.Client, e girc.Event) {
// Delegate channel messages to discord bot so he can do webhook stuff
if e.IsFromChannel() { DIRCMessage(e.Source.Name, e.Last()); return }
// private message, channel switch
// get ready for wrong
reply := "Canal indisponível. Opções são:"
msg := e.Last()
for hname, _ := range Config.Hooks {
if hname == msg {
IUserDChannels[e.Source.Name] = hname
c.Cmd.Messagef(e.Source.Name, "Você está postando em %s", hname)
log.Printf("[IRC] Moved %s's messages to %s", e.Source.Name, hname)
return
}
reply = fmt.Sprintf("%s %s ;", reply, hname)
}
c.Cmd.Message(e.Source.Name, reply)
}
func IReplEmoji(input string) string {
if DEmoji == nil { return input }
for _, e := range DEmoji {
if e.Name == input[1:len(input)-1] {
prefix := ""
if e.Animated { prefix = "a" }
return fmt.Sprintf("<%s:%s:%s>", prefix, e.Name, e.ID)
}
}
return input
}
func IReplMention(input string) string {
if DNameToID == nil { return input }
u := string(IMentionRegex.ExpandString([]byte{}, "$u", input, IMentionRegex.FindStringSubmatchIndex(input)))
if DNameToID[u].ID != "" {
return fmt.Sprintf("<@!%s>", DNameToID[u].ID)
} else {
return input
}
}
func IInit() {
IEmojiRegex = regexp.MustCompile(":[[:alnum:]_]+:")
IMentionRegex = regexp.MustCompile("^(?P<u>[[:alnum:]_]+):|@(?P<u>[[:alnum:]_]+)")
IUserDChannels = make(map[string]string)
// Init bot
IBot = girc.New(Config.IRCSettings)
IBot.Handlers.Add(girc.CONNECTED, IOnConnect)
IBot.Handlers.Add(girc.PRIVMSG, IOnPrivMsg)
if err := IBot.Connect(); err != nil {
panic(fmt.Sprintf("Can't connect to IRC, %s", err))
}
}