/
cursed_handlers.go
129 lines (122 loc) · 3.96 KB
/
cursed_handlers.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
package listen
import (
"context"
"errors"
"log/slog"
"slices"
"strconv"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/prometheus/client_golang/prometheus"
"github.com/dmtaylor/costanza/config"
"github.com/dmtaylor/costanza/internal/util"
)
const cursedChannelLogEventName = "cursed_channel"
const cursedWordLogEventName = "cursed_post"
var cursedChannelBaseLabels = prometheus.Labels{gatewayEventTypeLabel: messageCreateGatewayEvent, eventNameLabel: cursedChannelLogEventName}
var cursedWordBaseLabels = prometheus.Labels{gatewayEventTypeLabel: messageCreateGatewayEvent, eventNameLabel: cursedWordLogEventName}
func (s *Server) logCursedChannelStat(sess *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot || m.Author.ID == sess.State.User.ID {
return
}
if _, found := config.GlobalConfig.Discord.ListenChannelSet[m.GuildID]; !found {
return
}
ctx := util.ContextFromDiscordMessageCreate(context.Background(), m)
var err error
if s.m.enabled {
start := time.Now()
defer func() {
s.m.eventDuration.With(cursedChannelBaseLabels).Observe(time.Since(start).Seconds())
if err != nil {
var timeout = "false"
if errors.Is(err, context.DeadlineExceeded) {
timeout = "true"
}
s.m.eventErrors.With(prometheus.Labels{gatewayEventTypeLabel: messageCreateGatewayEvent, eventNameLabel: cursedChannelLogEventName, isTimeoutLabel: timeout}).Inc()
} else {
s.m.eventSuccess.With(cursedChannelBaseLabels).Inc()
}
}()
}
guildId, err := strconv.ParseUint(m.GuildID, 10, 64)
if err != nil {
slog.ErrorContext(ctx, "error logging cursed channel: "+err.Error())
return
}
userId, err := strconv.ParseUint(m.Author.ID, 10, 64)
if err != nil {
slog.ErrorContext(ctx, "error logging activity: "+err.Error())
return
}
channelId, err := strconv.ParseUint(m.ChannelID, 10, 64)
if err != nil {
slog.ErrorContext(ctx, "error logging activity: "+err.Error())
return
}
cursedChannels, err := s.app.CursedChannelCache.Get(ctx, guildId)
if err != nil {
slog.ErrorContext(ctx, "failed to get cursed channel list: "+err.Error())
return
}
if slices.Index(cursedChannels, channelId) != -1 {
err = s.app.Stats.LogCursedChannelPost(ctx, guildId, userId, time.Now().Format("2006-01"))
if err != nil {
slog.ErrorContext(ctx, "failed to update cursed channel log: "+err.Error())
return
}
}
}
func (s *Server) logCursedPostStat(sess *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot || m.Author.ID == sess.State.User.ID {
return
}
if _, found := config.GlobalConfig.Discord.ListenChannelSet[m.GuildID]; !found {
return
}
ctx := util.ContextFromDiscordMessageCreate(context.Background(), m)
var err error
if s.m.enabled {
start := time.Now()
defer func() {
s.m.eventDuration.With(cursedWordBaseLabels).Observe(time.Since(start).Seconds())
if err != nil {
var timeout = "false"
if errors.Is(err, context.DeadlineExceeded) {
timeout = "true"
}
s.m.eventErrors.With(prometheus.Labels{gatewayEventTypeLabel: messageCreateGatewayEvent, eventNameLabel: cursedWordLogEventName, isTimeoutLabel: timeout}).Inc()
} else {
s.m.eventSuccess.With(cursedWordBaseLabels).Inc()
}
}()
}
guildId, err := strconv.ParseUint(m.GuildID, 10, 64)
if err != nil {
slog.ErrorContext(ctx, "error logging cursed channel: "+err.Error())
return
}
userId, err := strconv.ParseUint(m.Author.ID, 10, 64)
if err != nil {
slog.ErrorContext(ctx, "error logging activity: "+err.Error())
return
}
cursedWords, err := s.app.CursedWordCache.Get(ctx, guildId)
if err != nil {
slog.ErrorContext(ctx, "failed to get cursed word list: "+err.Error())
return
}
msg := strings.ToLower(m.Message.Content)
count := 0
for _, word := range cursedWords {
count += strings.Count(msg, word)
}
if count > 0 {
err = s.app.Stats.LogCursedPost(ctx, guildId, userId, time.Now().Format("2006-01"), count)
if err != nil {
slog.ErrorContext(ctx, "failed to update cursed post log: "+err.Error())
return
}
}
}