-
Notifications
You must be signed in to change notification settings - Fork 0
/
memes.go
168 lines (160 loc) · 4.97 KB
/
memes.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
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"go.etcd.io/bbolt"
"rkfg.me/ns2query/db"
)
var (
ErrAlreadyAnnounced = fmt.Errorf("meme already announced")
)
func hasMeme(m *discordgo.Message) bool {
return len(m.Attachments) > 0 || strings.Contains(m.Content, "https://") || strings.Contains(m.Content, "http://")
}
func chooseMemeOfTheDay(s *discordgo.Session, memeChannelID string, deadlineHour int, dayLen int) ([]*discordgo.Message, int, error) {
notBefore := time.Now().UTC().Truncate(time.Hour*24).AddDate(0, 0, -2).Add(time.Hour * time.Duration(deadlineHour))
notAfter := notBefore.Add(time.Hour * time.Duration(dayLen))
log.Printf("OWL time from %s to %s", notBefore.Format(time.Stamp), notAfter.Format(time.Stamp))
allMessages := []*discordgo.Message{}
beforeID := ""
for {
messages, err := s.ChannelMessages(memeChannelID, 100, beforeID, "", "")
if err != nil {
return nil, 0, err
}
allMessages = append(allMessages, messages...)
if len(allMessages) == 0 {
return nil, 0, nil
}
if len(messages) == 0 || messages[len(messages)-1].Timestamp.Before(notBefore) {
break
}
beforeID = messages[len(messages)-1].ID
}
maxUpvotes := 0
winners := []*discordgo.Message{}
lastWinnerID := ""
err := bdb.View(func(tx *bbolt.Tx) error {
memesBucket := db.NewMemesBucket(tx)
status, err := memesBucket.GetValue(memeChannelID)
if err != nil {
return err
}
lastWinnerID = status.LastWinnerID
return nil
})
if err != nil {
log.Printf("Error looking for last winner ID: %s", err)
} else {
log.Printf("Last winner message ID: %s", lastWinnerID)
}
for _, m := range allMessages {
if !hasMeme(m) || m.Timestamp.Before(notBefore) || m.Timestamp.After(notAfter) || m.ID == lastWinnerID || m.Author.ID == s.State.User.ID {
continue
}
url := m.Content
if len(m.Attachments) > 0 {
url = m.Attachments[0].URL
}
log.Printf("Considering message %s", url)
for _, r := range m.Reactions {
if r.Emoji.Name == "\U0001F44D" {
if r.Count > maxUpvotes {
maxUpvotes = r.Count
winners = []*discordgo.Message{}
}
if r.Count >= maxUpvotes {
winners = append(winners, m)
}
}
}
}
return winners, maxUpvotes, nil
}
func announceMOTD(s *discordgo.Session, channelID string, deadlineHour int, dayLen int) error {
if !config.Threads[channelID].Meme {
return nil
}
winners, upvotes, err := chooseMemeOfTheDay(s, channelID, deadlineHour, dayLen)
if err != nil {
return err
}
if len(winners) == 0 {
return nil
}
winner := winners[0]
winnerURL := winner.Content
if len(winner.Attachments) > 0 {
winnerURL = winner.Attachments[0].URL
}
if len(winners) > 1 {
winnerURL = fmt.Sprintf("%s (tied between %d best memes)", winnerURL, len(winners))
}
response := fmt.Sprintf("Meme of the day from <#%s> (%d upvotes): %s", channelID, upvotes, winnerURL)
targetChannelID := config.Threads[channelID].AnnounceWinnerTo
if targetChannelID == "" {
s.ChannelMessageSend(channelID, response)
} else {
s.ChannelMessageSend(targetChannelID, response)
}
bdb.Update(func(tx *bbolt.Tx) error {
memesBucket := db.NewMemesBucket(tx)
return memesBucket.PutValue(channelID,
db.MemeStatus{
LastAnnouncementDay: time.Now().Day(),
LastWinnerID: winner.ID,
})
})
return nil
}
func competition(s *discordgo.Session, channelID string, t thread) {
if t.CompetitionLength == 0 {
t.CompetitionLength = 24
}
log.Printf("Running meme competition in channel %s, announcing at %d:00 (UTC+0) to channel %s, "+
"considering all memes since %d:00 (UTC+0) of the prior day for the next %d hours", channelID, t.CompetitionAnnouncement,
t.AnnounceWinnerTo, t.CompetitionDeadline, t.CompetitionLength)
s.ThreadJoin(channelID)
for {
now := time.Now().UTC()
nextAnnouncement := now.Truncate(time.Hour * 24).Add(time.Hour * time.Duration(t.CompetitionAnnouncement))
if now.Hour() >= t.CompetitionAnnouncement { // next announcement is tomorrow
nextAnnouncement = nextAnnouncement.Add(time.Hour * 24)
}
if now.Hour() == t.CompetitionAnnouncement {
err := bdb.View(func(tx *bbolt.Tx) error {
memesBucket := db.NewMemesBucket(tx)
status, err := memesBucket.GetValue(channelID)
if err != nil {
return err
}
if status.LastAnnouncementDay == now.Day() {
return ErrAlreadyAnnounced
}
return nil
})
if err == ErrAlreadyAnnounced {
log.Printf("Meme from channel %s has already been announced to %s",
channelID, t.AnnounceWinnerTo)
} else {
if err != nil && err != db.ErrNotFound {
log.Printf("Error querying meme announcement status: %s", err)
} else {
announceMOTD(s, channelID, t.CompetitionDeadline, t.CompetitionLength)
}
}
}
log.Printf("Next announcement time: %s, sleeping until then", nextAnnouncement.Format("02-01-2006 15:04:05"))
time.Sleep(time.Until(nextAnnouncement))
}
}
func startCompetitions(s *discordgo.Session) {
for id, t := range config.Threads {
if t.Meme && t.Competition {
go competition(s, id, t)
}
}
}