This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemeQueue.go
226 lines (185 loc) · 5.32 KB
/
memeQueue.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
"regexp"
"strconv"
"sync"
"time"
"github.com/bwmarrin/discordgo"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
// QueueObj data structure for the queue
type QueueObj struct {
Interval string `json:"interval"`
Time int64 `json:"time"`
Type string `json:"type"`
SubReddits []string `json:"subreddit"`
NSFW bool `json:"nsfw"`
ID primitive.ObjectID `bson:"_id,omitempty"`
Channel string `json:"channelid"`
}
func queueThread(discord *discordgo.Session) {
fmt.Println("Starting Queue Processing thread.")
fmt.Println("Generating lock file.")
testData, err := lockFileCreate()
if err != nil {
panic(err)
}
fmt.Println("Thread started.")
for {
var wg sync.WaitGroup
fileEqual, err := lockFileEqu(testData)
if err != nil {
fmt.Println(err)
break
}
if !fileEqual {
fmt.Println("New processing thread started, killing old thread.")
break
}
keys, err := GetAllQueueChannels()
if err != nil {
fmt.Println("Error running worker queue: ", err.Error())
continue
}
for _, key := range keys {
if key != "" {
wg.Add(1)
go queueWorker(discord, key, &wg)
}
}
wg.Wait()
// This uses less CPU than the loop with timer
// And the end user will see no difference
// One of the few times I will use a sleep
time.Sleep(time.Second * 10)
}
fmt.Println("Queue processing thread killed.")
}
func queueWorker(discord *discordgo.Session, channel string, wg *sync.WaitGroup) {
defer wg.Done()
var interval time.Duration
maxTime := time.Hour * 168
minTime := time.Minute * 15
queueItem, err := GetMemeQueue(channel)
if err == mongo.ErrNoDocuments {
return
}
if err != nil {
fmt.Println("Error getting from Queue: ", err.Error())
}
if queueItem.Time <= time.Now().Unix() && !QueueState[channel] {
if err != nil {
// will only send the error to the channel if its time
// for the channel to get a meme. This should happen infrequently
// or never
errSendRoutine(discord, channel, err)
return
}
QueueState[channel] = true
// will always set the QueueState for the
// given channel to false no matter where the
// function ends.
defer resetQueueState(channel)
fmt.Println("Posting in", channel, "from queue.")
switch queueItem.Type {
case "text":
getTextPost(discord, channel, queueItem.NSFW, queueItem.SubReddits, "hot")
case "link":
getLinkPost(discord, channel, queueItem.NSFW, queueItem.SubReddits, "hot")
default:
getMediaPost(discord, channel, queueItem.NSFW, queueItem.SubReddits, "hot")
}
letters, err := regexp.Compile("[^a-zA-Z]+")
if err != nil {
fmt.Println("Error setting Queue: ", err.Error())
errSendRoutine(discord, channel, err)
return
}
numbers, err := regexp.Compile("[^0-9]+")
if err != nil {
fmt.Println("Error setting Queue: ", err.Error())
errSendRoutine(discord, channel, err)
return
}
multiplier, err := strconv.ParseFloat(numbers.ReplaceAllString(queueItem.Interval, ""), 64)
if err != nil {
fmt.Println("Error setting Queue: ", err.Error())
errSendRoutine(discord, channel, err)
DeleteMemeQueue(channel)
return
}
duration := letters.ReplaceAllString(queueItem.Interval, "")
if err != nil {
fmt.Println("Error setting Queue: ", err.Error())
errSendRoutine(discord, channel, err)
DeleteMemeQueue(channel)
return
}
if multiplier <= 0 {
multiplier = 1
discord.ChannelMessageSend(channel, "Time cannot be negative or zero, setting to one.")
}
switch duration {
case "s":
interval = time.Second * time.Duration(multiplier)
case "m":
interval = time.Minute * time.Duration(multiplier)
case "d":
interval = time.Hour * 24 * time.Duration(multiplier)
case "w":
interval = time.Hour * 168 * time.Duration(multiplier)
default:
interval = time.Hour * time.Duration(multiplier)
}
if interval > maxTime {
interval = maxTime
queueItem.Interval = "1w"
discord.ChannelMessageSend(channel, "The maximum interval between memes is one week, so the interval will be set to that. For slower memes, check Facebook or a newspaper.")
}
if interval < minTime {
interval = minTime
queueItem.Interval = "15m"
discord.ChannelMessageSend(channel, "There is minimum time interval between posts of 15 minutes, setting the interval to that. These servers aren't free, you know.")
}
queueItem.Time = time.Now().Unix() + int64(interval.Seconds())
err = UpdateMemeQueueTime(channel, queueItem.Time)
if err != nil {
fmt.Println("Error setting Queue: ", err.Error())
errSendRoutine(discord, channel, err)
} else {
fmt.Println("Done.")
}
}
}
func lockFileEqu(input []byte) (bool, error) {
data, err := ioutil.ReadFile("./thread.lock")
if err != nil {
return false, err
}
if bytes.Equal(input, data) {
return true, nil
}
return false, nil
}
func lockFileExists() bool {
info, err := os.Stat("./thread.lock")
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func lockFileCreate() ([]byte, error) {
fileData := make([]byte, 8)
rand.Read(fileData) // skipcq
err := ioutil.WriteFile("./thread.lock", fileData, 0644)
return fileData, err
}
func resetQueueState(channel string) {
QueueState[channel] = false
}