-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaygpt.go
147 lines (124 loc) · 3.78 KB
/
gaygpt.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"log/slog"
"math/rand"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/itsvyle/hxi_bot/config"
)
type ServiceGayGPT struct {
config *config.ConfigSchemaJsonGayGPTServicesElem
discordSession *discordgo.Session
}
func CreateNewServiceGayGPT(botConfig *config.ConfigSchemaJsonGayGPTServicesElem) *ServiceGayGPT {
if botConfig.ReactTo == nil {
botConfig.ReactTo = make(config.ConfigSchemaJsonGayGPTServicesElemReactTo)
}
if botConfig.AutoReactTrigger == nil {
botConfig.AutoReactTrigger = make(config.ConfigSchemaJsonGayGPTServicesElemAutoReactTrigger)
}
botConfig.ReactTo.Init()
return &ServiceGayGPT{
config: botConfig,
}
}
func MatchWordLetters(word string, toMatch string) bool {
toMatch = strings.ToLower(toMatch)
step := 0
l := len(word)
for i, _ := range toMatch {
if toMatch[i] == ' ' { // want all the letters to be in order in a single given word
step = 0
}
if toMatch[i] == word[step] {
step++
if step >= l {
return true
}
}
}
return false
}
func (s *ServiceGayGPT) InitGayGPT() {
var err error
s.discordSession, err = discordgo.New("Bot " + s.config.BotToken)
if err != nil {
slog.With("error", err, "gayGPT", true).Error("Error creating Discord session")
panic("Error creating Discord session")
}
s.discordSession.ShouldReconnectOnError = true
s.discordSession.Identify.Intents = discordgo.IntentsGuildMessages
s.discordSession.AddHandler(readyBot)
err = s.discordSession.Open()
if err != nil {
slog.With("error", err, "gayGPT", true).Error("Error logging in to the discord session. Check that token is valid.")
panic("Error logging in to the discord session. Check that token is valid.")
}
var botRoles []string
if s.config.GuildId != nil {
botMember, err := s.discordSession.GuildMember(*s.config.GuildId, s.discordSession.State.User.ID)
if err == nil {
botRoles = make([]string, len(botMember.Roles))
copy(botRoles, botMember.Roles)
slog.With("roles", botRoles, "gayGPT", true).Info("Initialized bot roles")
}
}
rand.Seed(time.Now().UnixNano())
s.discordSession.AddHandler(func(session *discordgo.Session, message *discordgo.MessageCreate) {
if message.Author.ID == session.State.User.ID {
return
}
if message.Author.Bot {
return
}
s.config.ReactTo.ReactWithEmoji(session, message)
if message.Content != "" {
c := strings.ToLower(message.Content)
for trigger, reactWith := range s.config.AutoReactTrigger {
if MatchWordLetters(trigger, c) {
session.MessageReactionAdd(message.ChannelID, message.ID, reactWith)
}
}
}
mentionsMe := false
for _, user := range message.Mentions {
if user.ID == session.State.User.ID {
mentionsMe = true
break
}
}
if !mentionsMe {
for _, role := range message.MentionRoles {
for _, botRole := range botRoles {
if role == botRole {
mentionsMe = true
break
}
}
}
if !mentionsMe {
return
}
}
randIndex := rand.Intn(len(s.config.PossibleAnswers))
answer := s.config.PossibleAnswers[randIndex]
if message.ReferencedMessage != nil && message.ReferencedMessage.Author.ID != session.State.User.ID {
_, err = session.ChannelMessageSendReply(message.ChannelID, answer, message.ReferencedMessage.Reference())
err2 := session.ChannelMessageDelete(message.ChannelID, message.ID)
if err2 != nil {
slog.With("error", err2, "channelID", message.ChannelID).Error("Error deleting message")
}
} else {
_, err = session.ChannelMessageSendReply(message.ChannelID, answer, message.Reference())
}
if err != nil {
slog.With("error", err, "channelID", message.ChannelID).Error("Error sending message to channel")
return
}
})
}
func (s *ServiceGayGPT) Close() {
slog.With("gayGPT", true).Info("Closing connection...")
s.discordSession.Close()
}