-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
96 lines (86 loc) · 2.35 KB
/
events.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package guilded
import (
"strings"
"github.com/Clinet/clinet_cmds"
"github.com/Clinet/clinet_services"
"github.com/JoshuaDoes/guildrone"
)
func guildedReady(session *guildrone.Session, event *guildrone.Ready) {
Log.Trace("--- guildedReady(", event, ") ---")
for Guilded == nil {
//Wait for Guilded to finish connecting on our end
if Guilded != nil {
break
}
}
Guilded.User = event.User
Log.Info("Logged into Guilded as ", Guilded.User, "!")
}
func guildedChatMessageCreated(session *guildrone.Session, event *guildrone.ChatMessageCreated) {
//Log.Trace("--- guildedChatMessageCreated(", event, ") ---", event.Message.ID, event.ServerID, event.Message.ChannelID, event.Message.CreatedBy)
if event == nil || event.Message.Content == "" {
return
}
msg := &services.Message{
UserID: event.Message.CreatedBy,
MessageID: event.Message.ID,
ChannelID: event.Message.ChannelID,
ServerID: event.ServerID,
Content: event.Message.Content,
Context: event.Message,
}
//Determine interaction type and how it was called
convo := false
prefix := "@" + Guilded.botName
if strings.Contains(msg.Content, prefix) {
convo = true
msg.Content = strings.ReplaceAll(msg.Content, " " + prefix + " ", " ")
msg.Content = strings.ReplaceAll(msg.Content, " " + prefix, "")
msg.Content = strings.ReplaceAll(msg.Content, prefix + " ", "")
msg.Content = strings.ReplaceAll(msg.Content, prefix, "")
}
cmdResps := make([]*cmds.CmdResp, 0)
if convo {
resps, err := convoHandler(msg, Guilded)
if err != nil {
Log.Error(err)
msgErr := msg
msgErr.Content = err.Error()
Guilded.MsgSend(msgErr, nil)
return
}
cmdResps = resps
} else {
_, resps, err := cmds.CmdHandler(msg, Guilded)
if err != nil {
Log.Error(err)
msgErr := msg
msgErr.Content = err.Error()
Guilded.MsgSend(msgErr, nil)
return
}
cmdResps = resps
}
if len(cmdResps) == 0 {
Log.Warn("no cmdresp")
return
}
for i := 0; i < len(cmdResps); i++ {
if cmdResps[i] == nil {
Log.Warn("ignoring empty cmdresp")
continue
}
cmdResps[i].OnReady(func(r *cmds.CmdResp) {
Log.Trace("Response to message: " + r.String())
r.Context = event.Message
r.ServerID = event.ServerID
r.ChannelID = event.Message.ChannelID
msg, err := Guilded.MsgSend(r.Message, nil)
if err != nil {
Log.Error(err)
return
}
Log.Trace("Sent message: ", msg)
})
}
}