-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
62 lines (49 loc) · 1.23 KB
/
handler.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
package main
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
)
type Handler interface {
GetResponse(msg string) string
}
func (babur *Bot) MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore messages from the bot itself
if m.Author.ID == s.State.User.ID {
return
}
// Log messages
if len(m.Message.Content) > 120 {
fmt.Printf("MSG: %s ...\n", m.Message.Content[:120])
} else {
fmt.Printf("MSG: %s\n", m.Message.Content)
}
// try to roll dice
if response := babur.dice.GetResponse(m.Message.Content); response != "" {
sendMessage(s, m, response)
return
}
// llm conversation
if strings.Contains(m.Message.Content, babur.botId) {
answer, err := babur.llm.Question(m.Message.Content)
if err != nil {
sendMessage(s, m, err.Error())
return
}
if answer == "" {
return
}
sendMessage(s, m, answer)
return
}
}
func sendMessage(s *discordgo.Session, m *discordgo.MessageCreate, content string) {
// Mention the user in the first line
msg := fmt.Sprintf("<@%s> \n%s", m.Message.Author.ID, content)
// Log reponses
fmt.Printf("RESPONSE: %s\n", msg)
if _, err := s.ChannelMessageSend(m.ChannelID, msg); err != nil {
fmt.Println("ERROR: Send message: ", err)
return
}
}