-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrespond.go
61 lines (53 loc) · 1.38 KB
/
respond.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
//
// Hated *things* module
//
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"strings"
"github.com/bwmarrin/discordgo"
)
type phrase struct {
ID int `json:"id"`
Phrase string `json:"phrase"`
Response string `json:"response"`
Reaction string `json:"reaction"`
}
func respond(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore bot messages, good practice
if m.Author.ID == s.State.User.ID {
return
}
// Check if option enabled / config file exists
if config.Modules.Respond != true || config.RespondData == "" {
return
}
// Read from JSON list file
data, err := ioutil.ReadFile(config.RespondData)
if err != nil {
log.Println(fmt.Sprintf("[Error][Respond] Failed to open file: %s", err))
return
}
// Link JSON data slice to phrases
var phrases []phrase
err = json.Unmarshal(data, &phrases)
if err != nil {
log.Println(fmt.Sprintf("[Error][Respond] Could not unmarshal JSON data: %s", err))
return
}
// For every slice entry run following:
for _, entry := range phrases {
if strings.Contains(strings.ToLower(m.Content), entry.Phrase) {
if entry.Response != "" {
s.ChannelMessageSend(m.ChannelID, entry.Response)
} else if entry.Reaction != "" {
s.MessageReactionAdd(m.ChannelID, m.ID, entry.Reaction)
} else {
s.MessageReactionAdd(m.ChannelID, m.ID, "🤮")
}
}
}
}