add spam stuff
This commit is contained in:
+93
-23
@@ -4,55 +4,88 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"hal/openai"
|
"hal/openai"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MAX_HISTORY = 50
|
const MAX_HISTORY = 50
|
||||||
|
|
||||||
var messagesHistory = []*openai.ChatMessage{}
|
const SPAM_PERIOD = 5 * time.Minute
|
||||||
|
|
||||||
|
var spams = []string{
|
||||||
|
":GroGroDebile:",
|
||||||
|
"Arrête de spam putain!",
|
||||||
|
"Wesh...",
|
||||||
|
"...",
|
||||||
|
"Flemme.",
|
||||||
|
"Laisse-moi tranquille!",
|
||||||
|
"Fdr",
|
||||||
|
":soxx:",
|
||||||
|
}
|
||||||
|
|
||||||
|
type userHistoryCount struct {
|
||||||
|
date time.Time
|
||||||
|
bannedUntilAt time.Time
|
||||||
|
count int
|
||||||
|
}
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
|
messagesHistory []*openai.ChatMessage
|
||||||
|
usersHistoryCount map[string]userHistoryCount
|
||||||
|
|
||||||
client *openai.Client
|
client *openai.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(token string) Handler {
|
func Init(token string) Handler {
|
||||||
return Handler{
|
return Handler{
|
||||||
client: openai.NewClient(token),
|
client: openai.NewClient(token),
|
||||||
|
|
||||||
|
messagesHistory: []*openai.ChatMessage{},
|
||||||
|
usersHistoryCount: map[string]userHistoryCount{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tramp: 161970441441902592
|
||||||
|
|
||||||
func (h Handler) OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func (h Handler) OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
isHal := m.Author.ID == s.State.User.ID
|
isHal := m.Author.ID == s.State.User.ID
|
||||||
|
|
||||||
addMessageToHistory(m.Message, isHal)
|
h.addMessageToHistory(m.Message, isHal)
|
||||||
|
|
||||||
if isHal {
|
if isHal || !containHal(m.Mentions, s.State.User.ID) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if containUser(m.Mentions, s.State.User.ID) {
|
userSpamTooMuch := h.updateUserHistoryCount(m.Author.ID)
|
||||||
res, err := h.client.Chat(messagesHistory)
|
|
||||||
if err != nil {
|
|
||||||
sendResponse(
|
|
||||||
s,
|
|
||||||
m.ChannelID,
|
|
||||||
fmt.Sprintf("X_X: %s", err.Error()),
|
|
||||||
)
|
|
||||||
|
|
||||||
log.Printf("\nfailed to query open ai with the following prompt [%s]. Error: %s", m.Content, err.Error())
|
if userSpamTooMuch {
|
||||||
return
|
sendResponse(s, m.ChannelID, getRandomSpam())
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(res.Choices) > 0 {
|
res, err := h.client.Chat(h.messagesHistory)
|
||||||
aiResponse := res.Choices[0].Message.Content
|
if err != nil {
|
||||||
sendResponse(s, m.ChannelID, aiResponse)
|
sendResponse(
|
||||||
}
|
s,
|
||||||
|
m.ChannelID,
|
||||||
|
fmt.Sprintf("X_X: %s", err.Error()),
|
||||||
|
)
|
||||||
|
|
||||||
|
log.Printf("\nfailed to query open ai with the following prompt [%s]. Error: %s", m.Content, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(res.Choices) > 0 {
|
||||||
|
aiResponse := res.Choices[0].Message.Content
|
||||||
|
sendResponse(s, m.ChannelID, aiResponse)
|
||||||
|
} else {
|
||||||
|
sendResponse(s, m.ChannelID, "la fatigue")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func addMessageToHistory(m *discordgo.Message, isHal bool) {
|
func (h Handler) addMessageToHistory(m *discordgo.Message, isHal bool) {
|
||||||
var role string
|
var role string
|
||||||
if isHal {
|
if isHal {
|
||||||
role = "system"
|
role = "system"
|
||||||
@@ -60,13 +93,13 @@ func addMessageToHistory(m *discordgo.Message, isHal bool) {
|
|||||||
role = "user"
|
role = "user"
|
||||||
}
|
}
|
||||||
|
|
||||||
messagesHistory = append(messagesHistory, &openai.ChatMessage{
|
h.messagesHistory = append(h.messagesHistory, &openai.ChatMessage{
|
||||||
Role: role,
|
Role: role,
|
||||||
Content: cleanMessage(m.Content),
|
Content: cleanMessage(m.Content),
|
||||||
})
|
})
|
||||||
|
|
||||||
if len(messagesHistory) > MAX_HISTORY {
|
if len(h.messagesHistory) > MAX_HISTORY {
|
||||||
messagesHistory = messagesHistory[1:]
|
h.messagesHistory = h.messagesHistory[1:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,7 +114,7 @@ func sendResponse(s *discordgo.Session, channelID string, response string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func containUser(users []*discordgo.User, userID string) bool {
|
func containHal(users []*discordgo.User, userID string) bool {
|
||||||
for _, u := range users {
|
for _, u := range users {
|
||||||
if u.ID == userID {
|
if u.ID == userID {
|
||||||
return true
|
return true
|
||||||
@@ -90,3 +123,40 @@ func containUser(users []*discordgo.User, userID string) bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h Handler) updateUserHistoryCount(userID string) bool {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
u, ok := h.usersHistoryCount[userID]
|
||||||
|
if !ok {
|
||||||
|
h.usersHistoryCount[userID] = userHistoryCount{
|
||||||
|
date: now,
|
||||||
|
count: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
u.count++
|
||||||
|
|
||||||
|
if now.Before(u.bannedUntilAt) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.date.Add(SPAM_PERIOD).After(now) {
|
||||||
|
u.count = 1
|
||||||
|
u.date = now
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.count > 5 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRandomSpam() string {
|
||||||
|
return spams[rand.Intn(len(spams))]
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user