-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
234 lines (199 loc) · 6.31 KB
/
Copy pathmessage.go
File metadata and controls
234 lines (199 loc) · 6.31 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package handler
import (
"context"
"errors"
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/taythebot/discord_chatbot"
"github.com/taythebot/discord_chatbot/ent"
"github.com/taythebot/discord_chatbot/ent/blacklist"
"github.com/taythebot/discord_chatbot/ent/message"
"github.com/taythebot/discord_chatbot/provider"
)
// messageErrorHandler for failed message processing
func messageErrorHandler(err error, s *discordgo.Session, m *discordgo.MessageCreate, logger zerolog.Logger) {
logger.Error().Err(err).Msg("Failed to process message")
s.ChannelMessageSendEmbedReply(m.ChannelID, &discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Title: "MessageCreate Error",
Color: discord_chatbot.RED,
Fields: []*discordgo.MessageEmbedField{
{Name: "Error", Value: fmt.Sprintf("```json\n%s\n```", err)},
{Name: "Message ID", Value: m.ID},
},
}, m.Reference())
}
// Function to split a message into chunks of a specified size, considering new lines
func splitMessage(message string, maxLength int) []string {
var chunks []string
for len(message) > maxLength {
// Find the last newline or space within the maxLength limit
var (
lastNewline = strings.LastIndex(message[:maxLength], "\n")
lastSpace = strings.LastIndex(message[:maxLength], " ")
)
// Choose the best split point: prefer newline over space
splitPoint := lastNewline
if lastNewline == -1 || (lastSpace > lastNewline) {
splitPoint = lastSpace
}
if splitPoint == -1 {
splitPoint = maxLength // No newline or space found, split at maxLength
}
chunks = append(chunks, message[:splitPoint])
message = message[splitPoint:]
}
chunks = append(chunks, message) // Add the remaining part
return chunks
}
// MessageCreate handler on new messages
func (r *Registry) MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
ctx := context.TODO()
defer ctx.Done()
// Ignore self, bots, and DMs
if m.Author.ID == s.State.User.ID || m.Author.Bot || m.GuildID == "" {
return
}
// Check if replied to
shouldProcess := m.ReferencedMessage != nil && m.ReferencedMessage.Author.ID == s.State.User.ID
// Check if pinged
if !shouldProcess {
for _, user := range m.Mentions {
if user.ID == s.State.User.ID {
shouldProcess = true
break
}
}
}
if !shouldProcess {
return
}
logger := log.With().
Str("handler", "messageCreate").
Str("msg", m.ID).
Str("channel", m.ChannelID).
Str("guild", m.GuildID).
Str("user", m.Author.ID).
Logger()
logger.Info().Msgf("Processing message '%s'", m.ID)
// Check if from blacklisted user
blacklistExists, err := r.DB.Blacklist.
Query().
Where(blacklist.UserID(m.Author.ID)).
Exist(ctx)
if err != nil {
messageErrorHandler(fmt.Errorf("failed to get user from blacklist: %w", err), s, m, logger)
return
} else if blacklistExists {
s.ChannelMessageSendEmbedReply(m.ChannelID, discord_chatbot.BlacklistedUserEmbed, m.Reference())
return
}
// Check if valid channelItem
channelItem, err := r.DB.Channel.Get(ctx, m.ChannelID)
if err != nil {
if ent.IsNotFound(err) {
s.ChannelMessageSendEmbedReply(m.ChannelID, discord_chatbot.InvalidChannelEmbed, m.Reference())
return
}
messageErrorHandler(fmt.Errorf("failed to get channel from database: %w", err), s, m, logger)
return
}
if channelItem == nil {
logger.Debug().Msg("Ignoring message from invalid channel")
return
}
// Send typing presence
if err := s.ChannelTyping(m.ChannelID); err != nil {
logger.Error().Err(err).Msg("Failed to send channel typing presence")
}
// Get all chats
messages, err := r.DB.Message.
Query().
Select(message.FieldUserName, message.FieldContent).
Where(message.ChannelID(m.ChannelID)).
Order(ent.Asc(message.FieldCreatedAt)).
All(ctx)
if err != nil {
messageErrorHandler(fmt.Errorf("failed to get previous messages from database: %w", err), s, m, logger)
return
}
// Request chat completion
request := provider.PostChatCompletionsRequest{
Model: channelItem.Model,
Stream: false,
Messages: make([]provider.PostChatCompletionsRequestMessage, 0, len(messages)),
Temperature: 0.7,
MaxTokens: 2000,
TopP: 1,
FrequencyPenalty: 0,
PresencePenalty: 0,
}
// Add system prompt
request.Messages = append(request.Messages, provider.PostChatCompletionsRequestMessage{
Role: provider.RoleSystem,
Content: channelItem.Prompt,
})
// Add previous messages
for _, message := range messages {
role := provider.RoleAssistant
if message.MessageID == "" {
role = provider.RoleUser
}
request.Messages = append(request.Messages, provider.PostChatCompletionsRequestMessage{
Role: role,
Content: fmt.Sprintf("%s: %s", message.UserName, message.Content),
})
}
// Get new reply
content := m.ContentWithMentionsReplaced()
request.Messages = append(request.Messages, provider.PostChatCompletionsRequestMessage{
Role: provider.RoleUser,
Content: fmt.Sprintf("%s: %s", m.Author.GlobalName, content),
})
response, err := r.Provider.PostChatCompletions(ctx, logger, request)
if err != nil {
messageErrorHandler(fmt.Errorf("failed to get chat completion: %w", err), s, m, logger)
return
}
if len(response.Choices) < 1 {
messageErrorHandler(errors.New("failed to get chat completion: no choices provided"), s, m, logger)
return
}
// Reply to user
reply := response.Choices[0].Message.Content
if len(reply) > 2000 {
// Split the reply into chunks
for _, chunk := range splitMessage(reply, 2000) {
if _, err := s.ChannelMessageSendReply(m.ChannelID, chunk, m.Reference()); err != nil {
messageErrorHandler(fmt.Errorf("failed to send message: %w", err), s, m, logger)
return
}
}
} else {
if _, err := s.ChannelMessageSendReply(m.ChannelID, reply, m.Reference()); err != nil {
messageErrorHandler(fmt.Errorf("failed to send message: %w", err), s, m, logger)
return
}
}
// Save messages
err = r.DB.Message.CreateBulk(
r.DB.Message.
Create().
SetMessageID(m.ID).
SetUserID(m.Author.ID).
SetUserName(m.Author.GlobalName).
SetContent(content).
SetChannelID(m.ChannelID),
r.DB.Message.
Create().
SetContent(reply).
SetChannelID(m.ChannelID),
).Exec(ctx)
if err != nil {
messageErrorHandler(fmt.Errorf("failed to save messages in database: %w", err), s, m, logger)
return
}
}