-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvos.go
87 lines (78 loc) · 2.54 KB
/
convos.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
package discord
import (
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/Clinet/clinet_cmds"
"github.com/Clinet/clinet_convos"
)
func convoHandler(session *discordgo.Session, message *discordgo.Message, channel *discordgo.Channel) (cmdResps []*cmds.CmdResp, err error) {
if message == nil {
return nil, cmds.ErrCmdEmptyMsg
}
if message.Author.Bot {
return nil, nil
}
content := message.Content
if content == "" {
return nil, nil
}
//Determine interaction type and how it was called
prefix := ""
convo := false
if strings.Contains(content, "<@" + Discord.User.ID + ">") {
prefix = "<@" + Discord.User.ID + ">"
convo = true
} else if strings.Contains(content, "<@!" + Discord.User.ID + ">") {
prefix = "<@!" + Discord.User.ID + ">"
convo = true
}
if !convo {
return nil, nil
}
content = strings.ReplaceAll(content, " " + prefix + " ", " ")
content = strings.ReplaceAll(content, " " + prefix, "")
content = strings.ReplaceAll(content, prefix + " ", "")
content = strings.ReplaceAll(content, prefix, "")
//Start typing!
stopTyping := false
go func(shouldStop *bool, channelID string) {
for {
if shouldStop == nil || *shouldStop {
break
}
err := session.ChannelTyping(channelID)
if err != nil {
break
}
time.Sleep(time.Second * 2) //2 seconds is about long enough?
}
}(&stopTyping, message.ChannelID)
cmdResps = make([]*cmds.CmdResp, 0)
conversation := convos.NewConversation()
if oldConversation, err := Discord.Storage.ServerGet(channel.GuildID, "conversations_" + message.Author.ID); err == nil {
switch oldConversation.(type) {
case convos.Conversation:
conversation = oldConversation.(convos.Conversation)
default:
Log.Trace("Skipping broken conversation record")
}
}
conversationState := conversation.QueryText(content)
if len(conversationState.Errors) > 0 {
for _, csErr := range conversationState.Errors {
Log.Error(csErr)
}
}
if conversationState.Response != nil {
//TODO: Dynamically build either an embed response or a simple conversation response
cmdResps = append(cmdResps, cmds.NewCmdRespMsg(conversationState.Response.TextSimple))
Discord.Storage.ServerSet(channel.GuildID, "conversations_" + message.Author.ID, conversation)
} else {
//TODO: Make a nice error message for failed queries in a conversation
cmdResps = append(cmdResps, cmds.NewCmdRespMsg("Erm... well this is awkward. I don't have an answer for that."))
Discord.Storage.ServerDel(channel.GuildID, "conversations_" + message.Author.ID)
}
stopTyping = true
return cmdResps, nil
}