-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.go
154 lines (139 loc) · 4.99 KB
/
activity.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
148
149
150
151
152
153
154
/*
*
* Copyright 2023 casiusbot authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package main
import (
"encoding/csv"
"io"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/dvaumoron/casiusbot/common"
)
type memberActivity struct {
userId string
timestamp time.Time
vocal bool
}
type activityData struct {
messageCount int
lastMessage time.Time
lastVocal time.Time
}
func bgManageActivity(session *discordgo.Session, saveTickReceiver <-chan bool, dataSender chan<- common.MultipartMessage, activityPath string, dateFormat string, cmdName string, infos common.GuildAndConfInfo) chan<- memberActivity {
activityChannel := make(chan memberActivity)
go manageActivity(session, saveTickReceiver, dataSender, activityPath, dateFormat, cmdName, infos, activityChannel)
return activityChannel
}
func manageActivity(session *discordgo.Session, saveTickReceiver <-chan bool, dataSender chan<- common.MultipartMessage, activityPath string, dateFormat string, cmdName string, infos common.GuildAndConfInfo, activityChannelReceiver <-chan memberActivity) {
activities := loadActivities(activityPath, dateFormat)
activityFileName := filepath.Base(activityPath)
errorMsg := strings.ReplaceAll(infos.Msgs.ErrGlobalCmd, common.CmdPlaceHolder, cmdName)
for {
select {
case mActivity := <-activityChannelReceiver:
activity := activities[mActivity.userId]
if mActivity.vocal {
activity.lastVocal = mActivity.timestamp
} else {
activity.messageCount++
activity.lastMessage = mActivity.timestamp
}
activities[mActivity.userId] = activity
case sendFile := <-saveTickReceiver:
var builder strings.Builder
writer := csv.NewWriter(&builder)
// header
writer.Write([]string{"userId", "userName", "userNickname", "messageCount", "lastMessage", "lastVocal", "lastActivity"})
for _, idNames := range loadMemberIdAndNames(session, infos) {
activity := activities[idNames[0]]
lastMessage := activity.lastMessage.Format(dateFormat)
lastVocal := activity.lastVocal.Format(dateFormat)
lastActivity := lastMessage
if activity.lastMessage.Before(activity.lastVocal) {
lastActivity = lastVocal
}
writer.Write([]string{idNames[0], idNames[1], idNames[2], strconv.Itoa(activity.messageCount), lastMessage, lastVocal, lastActivity})
}
writer.Flush()
data := builder.String()
if sendFile {
dataSender <- common.MultipartMessage{FileName: activityFileName, FileData: data, ErrorMsg: errorMsg}
}
os.WriteFile(activityPath, []byte(data), 0644)
}
}
}
func loadActivities(activityPath string, dateFormat string) map[string]activityData {
activities := map[string]activityData{}
file, err := os.Open(activityPath)
if err != nil {
log.Println("Loading saved activities failed :", err)
return activities
}
defer file.Close()
reader := csv.NewReader(file)
reader.Read() // skip header
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Println("Parsing saved activities failed :", err)
return map[string]activityData{} // after error, restart monitoring with empty data
}
messageCount, err := strconv.Atoi(record[3])
if err != nil {
log.Println("Parsing message count failed :", err)
return map[string]activityData{} // after error, restart monitoring with empty data
}
lastMessage, err := time.Parse(dateFormat, record[4])
if err != nil {
log.Println("Parsing last message date failed :", err)
return map[string]activityData{} // after error, restart monitoring with empty data
}
lastVocal, err := time.Parse(dateFormat, record[5])
if err != nil {
log.Println("Parsing last vocal date failed :", err)
return map[string]activityData{} // after error, restart monitoring with empty data
}
activities[record[0]] = activityData{
messageCount: messageCount, lastMessage: lastMessage, lastVocal: lastVocal,
}
}
return activities
}
func loadMemberIdAndNames(session *discordgo.Session, infos common.GuildAndConfInfo) [][3]string {
guildMembers, err := session.GuildMembers(infos.GuildId, "", common.MemberCallLimit)
if err != nil {
log.Println("Cannot retrieve guild members (4) :", err)
return nil
}
names := make([][3]string, 0, len(guildMembers))
for _, member := range guildMembers {
userId := member.User.ID
if userId != infos.OwnerId && !common.IdInSet(member.Roles, infos.AdminitrativeRoleIds) {
names = append(names, [3]string{userId, member.User.Username, common.ExtractNick(member)})
}
}
return names
}