-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentinels.go
93 lines (80 loc) · 2.22 KB
/
sentinels.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
package main
import (
"context"
"fmt"
"regexp"
"time"
"github.com/Sadzeih/valcompbot/config"
"github.com/vartanbeno/go-reddit/v2/reddit"
)
var (
lastSENsince time.Time
lastSENsinceFmt = "The last Sentinels thread was %d days ago. 0 days since the last Sentinels thread."
sentinelsRegex = regexp.MustCompile(`(?i)\b((SEN(tinels)?)|(tenz|sacy|pancada|zekken|marved|kaplan|johnqt))\b`)
)
func DaysSinceLastSentinelsPost(c *reddit.Client) error {
lastSENpost, err := LookForLastSentinelsPost(c)
if err != nil {
return err
}
if lastSENpost != nil {
lastSENsince = lastSENpost.Created.Time
}
newPosts, errs, closeChan := c.Stream.Posts(config.Get().RedditSubreddit, reddit.StreamDiscardInitial)
defer closeChan()
for {
select {
case err := <-errs:
return err
case post := <-newPosts:
if sentinelsRegex.MatchString(post.Title) {
str := fmt.Sprintf(lastSENsinceFmt, int(time.Since(lastSENsince).Hours()/24))
if lastSENsince.IsZero() && lastSENpost == nil {
str = "I don't know when the last Sentinels post was (but it can't have been that long ago...). 0 days since the last Sentinels post."
}
com, _, err := c.Comment.Submit(context.Background(), post.FullID, str)
if err != nil {
return err
}
_, err = c.Moderation.DistinguishAndSticky(context.Background(), com.FullID)
if err != nil {
return err
}
lastSENsince = time.Now()
}
}
}
}
func LookForLastSentinelsPost(c *reddit.Client) (*reddit.Post, error) {
// Check 100 latest posts
posts, _, err := c.Subreddit.NewPosts(context.Background(), config.Get().RedditSubreddit, &reddit.ListOptions{
Limit: 100,
})
if err != nil {
return nil, err
}
for _, post := range posts {
if sentinelsRegex.MatchString(post.Title) {
return post, nil
}
}
if len(posts) < 100 {
return nil, nil
}
// Check 300 more in case a thread has not been found
for i := 0; i < 3; i++ {
posts, _, err = c.Subreddit.NewPosts(context.Background(), config.Get().RedditSubreddit, &reddit.ListOptions{
Limit: 100,
Before: posts[len(posts)-1].FullID,
})
if err != nil {
return nil, err
}
for _, post := range posts {
if sentinelsRegex.MatchString(post.Title) {
return post, nil
}
}
}
return nil, nil
}