forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
245 lines (195 loc) · 5.7 KB
/
logs.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
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
235
236
237
238
239
240
241
242
243
244
245
package logs
import (
"errors"
"fmt"
"github.com/jinzhu/gorm"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/configstore"
"golang.org/x/net/context"
"strconv"
"strings"
)
var (
ErrChannelBlacklisted = errors.New("Channel blacklisted from creating message logs")
)
type Plugin struct{}
func (p *Plugin) Name() string {
return "Logs"
}
func RegisterPlugin() {
//p := &Plugin{}
err := common.GORM.AutoMigrate(&MessageLog{}, &Message{}, &UsernameListing{}, &NicknameListing{}, GuildLoggingConfig{}).Error
if err != nil {
panic(err)
}
configstore.RegisterConfig(configstore.SQL, &GuildLoggingConfig{})
p := &Plugin{}
common.RegisterPlugin(p)
}
type GuildLoggingConfig struct {
configstore.GuildConfigModel
UsernameLoggingEnabled bool
NicknameLoggingEnabled bool
BlacklistedChannels string
ManageMessagesCanViewDeleted bool
EveryoneCanViewDeleted bool
ParsedBlacklistedchannels []int64 `gorm:"-"`
}
func (g *GuildLoggingConfig) PostFetch() {
split := strings.Split(g.BlacklistedChannels, ",")
for _, v := range split {
if v != "" && v != "0" {
parsed, err := strconv.ParseInt(v, 10, 64)
if err == nil {
g.ParsedBlacklistedchannels = append(g.ParsedBlacklistedchannels, parsed)
}
}
}
}
func (g *GuildLoggingConfig) GetName() string {
return "guild_logging_config"
}
// Returns either stored config, err or a default config
func GetConfig(guildID int64) (*GuildLoggingConfig, error) {
var general GuildLoggingConfig
err := configstore.Cached.GetGuildConfig(context.Background(), guildID, &general)
if err != nil {
if err == configstore.ErrNotFound {
return &GuildLoggingConfig{
UsernameLoggingEnabled: true,
NicknameLoggingEnabled: true,
}, nil
}
return nil, err
}
return &general, nil
}
type MessageLog struct {
gorm.Model
Messages []Message
ChannelName string
ChannelID string
GuildID string
Author string
AuthorID string
}
func (m *MessageLog) Link() string {
return fmt.Sprintf("https://%s/public/%s/logs/%d", common.Conf.Host, m.GuildID, m.ID)
}
type Message struct {
common.SmallModel
MessageLogID uint `gorm:"index"` // Foreign key, belongs to MessageLog
MessageID string `gorm:"index"`
Content string `gorm:"size:2000"`
Timestamp string
AuthorUsername string
AuthorDiscrim string
AuthorID string
Deleted bool
Color string `gorm:"-"`
}
func CreateChannelLog(config *GuildLoggingConfig, guildID, channelID int64, author string, authorID int64, count int) (*MessageLog, error) {
if config == nil {
var err error
config, err = GetConfig(guildID)
if err != nil {
return nil, err
}
}
if len(config.ParsedBlacklistedchannels) > 0 {
for _, v := range config.ParsedBlacklistedchannels {
if v == channelID {
return nil, ErrChannelBlacklisted
}
}
}
if count > 1000 {
panic("count > 1000")
}
cs := bot.State.Channel(true, channelID)
if cs == nil {
return nil, errors.New("Unknown channel")
}
// Make a light copy of the channel
channel := cs.Copy(true, false)
msgs, err := bot.GetMessages(channel.ID, count, true)
if err != nil {
return nil, err
}
logMsgs := make([]Message, len(msgs))
for k, v := range msgs {
if v.Author == nil || v.Timestamp == "" {
continue
}
body := v.Content
for _, attachment := range v.Attachments {
body += fmt.Sprintf(" (Attachment: %s)", attachment.URL)
}
if len(v.Embeds) > 0 {
body += fmt.Sprintf("(%d embeds is not shown)", len(v.Embeds))
}
// Strip out nul characters since postgres dont like them and discord dont filter them out (like they do in a lot of other places)
body = strings.Replace(body, string(0), "", -1)
logMsgs[k] = Message{
MessageID: discordgo.StrID(v.ID),
Content: body,
Timestamp: string(v.Timestamp),
AuthorUsername: v.Author.Username,
AuthorDiscrim: v.Author.Discriminator,
AuthorID: discordgo.StrID(v.Author.ID),
Deleted: v.Deleted,
}
}
log := &MessageLog{
Messages: logMsgs,
ChannelID: discordgo.StrID(channel.ID),
ChannelName: channel.Name,
Author: author,
AuthorID: discordgo.StrID(authorID),
GuildID: discordgo.StrID(channel.Guild.ID),
}
err = common.GORM.Create(log).Error
return log, err
return nil, nil
}
func GetChannelLogs(id int64) (*MessageLog, error) {
var result MessageLog
err := common.GORM.Where(id).First(&result).Error
if err != nil {
return nil, err
}
err = common.GORM.Where("message_log_id = ?", result.ID).Order("id desc").Find(&result.Messages).Error
// err = common.GORM.Model(&result).Related(&result.Messages, "MessageLogID").Error
return &result, err
}
func GetGuilLogs(guildID int64, before, after, limit int) ([]*MessageLog, error) {
var result []*MessageLog
var q *gorm.DB
if before != 0 {
q = common.GORM.Where("guild_id = ? AND id < ?", guildID, before)
} else if after != 0 {
q = common.GORM.Where("guild_id = ? AND id > ?", guildID, after)
} else {
q = common.GORM.Where("guild_id = ?", guildID)
}
err := q.Order("id desc").Limit(limit).Find(&result).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return []*MessageLog{}, nil
}
return nil, err
}
return result, err
}
func GetUsernames(userID int64, limit int) ([]UsernameListing, error) {
var listings []UsernameListing
err := common.GORM.Where(&UsernameListing{UserID: userID}).Order("id desc").Limit(limit).Find(&listings).Error
return listings, err
}
func GetNicknames(userID, guildID int64, limit int) ([]NicknameListing, error) {
var listings []NicknameListing
err := common.GORM.Where(&NicknameListing{UserID: userID, GuildID: discordgo.StrID(guildID)}).Order("id desc").Limit(limit).Find(&listings).Error
return listings, err
}