This repository has been archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pin.go
308 lines (260 loc) · 7.5 KB
/
pin.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"database/sql"
"github.com/bwmarrin/discordgo"
"gitlab.com/NatoBoram/Go-Miiko/wheel"
)
func pin(s *discordgo.Session, g *discordgo.Guild, c *discordgo.Channel, m *discordgo.Message) {
// DM?
if c.Type == discordgo.ChannelTypeDM {
return
}
// Get the reactions
var singleReactionCount int
for _, reaction := range m.Reactions {
singleReactionCount = wheel.MaxInt(singleReactionCount, reaction.Count)
}
// Minimum reactions
minReactions, err := getMinimumReactions(g, c)
if err != nil {
printDiscordError("Couldn't get the minimum reactions for a channel.", g, c, m, nil, err)
}
// Check the reactions
if singleReactionCount >= minReactions {
// Pin it!
err = s.ChannelMessagePin(c.ID, m.ID)
if err != nil {
printDiscordError("Couldn't pin a popular message!", g, c, m, nil, err)
// Check the amount of pins in that channel
messages, err := s.ChannelMessagesPinned(c.ID)
if err != nil {
printDiscordError("Couldn't obtain the amount of pins in a channel.", g, c, m, nil, err)
return
}
// Upgrade the minimum
if len(messages) >= 50 {
err = addMinimumReactions(c)
if err != nil {
printDiscordError("Couldn't add to the minimum reactions of a channel", g, c, m, nil, err)
return
}
purgePin(s, g, c, m, messages)
}
return
}
// Check if already in the database
_, _, err := selectPin(m)
if err == sql.ErrNoRows {
// Status
err = setStatus(s, "épingler "+m.Author.Username)
if err != nil {
printDiscordError("Couldn't set the status to pinning someone.", g, c, m, nil, err)
}
// Throw it in the hall of fame
go savePin(s, g, m)
// Not previously pinned, time to insert it!
_, err = insertPin(g, c, m)
if err != nil {
printDiscordError("Couldn't insert a pin.", g, c, m, nil, err)
}
} else if err != nil {
printDiscordError("Couldn't select a pin.", g, c, m, nil, err)
}
}
}
func purgePin(s *discordgo.Session, g *discordgo.Guild, c *discordgo.Channel, m *discordgo.Message, messages []*discordgo.Message) {
err := setStatus(s, "augmenter la difficulté de "+c.Name)
if err != nil {
printDiscordError("Couldn't set the status to upping difficulty for a channel.", g, c, m, nil, err)
}
// Check minimum
channelMin, err := selectMinimumReactions(c)
if err != nil {
printDiscordError("Couldn't add to the minimum reactions of a channel", g, c, m, nil, err)
}
// For each messages
for _, message := range messages {
// Check pins
var singleReactionCount int
for _, reaction := range message.Reactions {
singleReactionCount = wheel.MaxInt(singleReactionCount, reaction.Count)
}
// Unpin
if channelMin > singleReactionCount {
err := s.ChannelMessageUnpin(c.ID, message.ID)
if err != nil {
printDiscordError("Couldn't unpin a previously popular message", g, c, message, nil, err)
continue
}
}
// Delete pin
_, err := deletePin(message)
if err != nil {
printDiscordError("Couldn't remove a pin from the database.", g, c, message, nil, err)
}
}
}
func savePin(s *discordgo.Session, g *discordgo.Guild, m *discordgo.Message) (saved bool) {
// Only text messages are transferred. Delete empty messages.
if m.Type != discordgo.MessageTypeDefault || m.Content == "" {
return
}
// Get the hall of fame
halloffame, err := getFameChannel(s, g)
if err != nil || m.ChannelID == halloffame.ID {
printDiscordError("Couldn't get the hall of fame.", g, nil, m, nil, err)
return
}
// Just in case `GuildMember` fails
var (
colour = colourNPC
name = m.Author.Username
)
// Get Member
member, err := s.GuildMember(g.ID, m.Author.ID)
if err != nil {
// Probably just a member that's dead. Nothing to output.
// printDiscordError("Couldn't get a pinned member.", g, nil, m, nil, err)
} else {
// Get colour
colour, _ = getColour(s, g, member)
// Get name
if member == nil {
name = m.Author.Username
} else if member.Nick == "" {
name = member.User.Username
} else {
name = member.Nick
}
}
// Create Embed
embed := &discordgo.MessageEmbed{
Color: colour,
Author: &discordgo.MessageEmbedAuthor{
URL: "https://canary.discordapp.com/channels/" + g.ID + "/" + m.ChannelID + "/" + m.ID + "/",
Name: name,
IconURL: m.Author.AvatarURL(""),
},
URL: "https://canary.discordapp.com/channels/" + g.ID + "/" + m.ChannelID + "/" + m.ID + "/",
Title: "Message",
Description: m.Content,
}
var (
smallest *discordgo.MessageAttachment
largest *discordgo.MessageAttachment
)
// For all attachments
for _, attachment := range m.Attachments {
// Fields
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: "Attachement",
Value: "[" + attachment.Filename + "](" + attachment.URL + ")",
Inline: true,
})
// Check if image
if attachment.Height == 0 || attachment.Width == 0 {
continue
}
// Play with sizes
if smallest == nil && largest == nil {
if attachment.Width > attachment.Height || (attachment.Height > 80 && attachment.Width > 80) {
largest = attachment
} else {
smallest = attachment
}
} else if smallest == nil {
if attachment.Height*attachment.Width > largest.Height*largest.Width {
smallest, largest = largest, attachment
} else {
smallest = attachment
}
} else if largest == nil {
if attachment.Height*attachment.Width < smallest.Height*smallest.Width {
smallest, largest = attachment, smallest
} else {
largest = attachment
}
} else {
if attachment.Height*attachment.Width < smallest.Height*smallest.Width {
smallest = attachment
} else if attachment.Height*attachment.Width > largest.Height*largest.Width {
largest = attachment
}
}
}
// Thumbnail. 80x80.
if smallest != nil {
embed.Thumbnail = &discordgo.MessageEmbedThumbnail{
URL: smallest.URL,
Width: smallest.Width,
Height: smallest.Height,
}
}
// Image. 400x300.
if largest != nil {
embed.Image = &discordgo.MessageEmbedImage{
URL: largest.URL,
Width: largest.Width,
Height: largest.Height,
}
}
// Channel
embed.Fields = append(embed.Fields,
&discordgo.MessageEmbedField{
Name: "Salon",
Value: "<#" + m.ChannelID + ">",
Inline: true,
},
)
// Author
embed.Fields = append(embed.Fields,
&discordgo.MessageEmbedField{
Name: "Auteur",
Value: "<@" + m.Author.ID + ">",
Inline: true,
},
)
// Emoji Field
if len(m.Reactions) > 0 {
emojiField := &discordgo.MessageEmbedField{
Name: "Réactions",
Value: " ",
Inline: true,
}
// For each reactions
for _, reaction := range m.Reactions {
// Use only emojis
if reaction.Emoji.ID == "" {
emojiField.Value += reaction.Emoji.Name + " "
}
}
// Protect against empty reactions
if emojiField.Value != " " {
embed.Fields = append(embed.Fields, emojiField)
}
}
// Get the timestamp of a message.
timestamp, err := m.Timestamp.Parse()
if err != nil {
printDiscordError("Couldn't parse the timestamp of a message. Please update DiscordGo.", g, nil, m, nil, err)
return
}
// Footer
embed.Footer = &discordgo.MessageEmbedFooter{
IconURL: discordgo.EndpointGuildIcon(g.ID, g.Icon),
Text: wheel.ToFrenchDate(timestamp),
}
// Send embed
_, err = s.ChannelMessageSendEmbed(halloffame.ID, embed)
if err != nil {
printDiscordError("Couldn't send an embed.", g, nil, m, nil, err)
return
}
// Save it in the database
_, err = insertMessagesFamed(g, m)
if err != nil {
printDiscordError("Couldn't insert a famed message in the hall of fame!", g, nil, m, nil, err)
return
}
return true
}