-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
235 lines (205 loc) · 5.88 KB
/
main.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
package main
/*
* Twitter Tweets
* RSS
* Instagram
* Spotify Artist Releases
* Flickr
*M System Monitor
*L Twitter Trends
*L NASA APOD
*L Plex Titles
*L Twitch Chat Track
*L Twitch Live
*L Spotify Playlist Changes
*/
import (
"fmt"
"log"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/fatih/color"
"github.com/hako/durafmt"
)
var (
// General
loop chan os.Signal
timeLaunched time.Time
)
func init() {
loop = make(chan os.Signal, 1)
timeLaunched = time.Now()
//#region Initialize Logging
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile | log.Lmsgprefix)
log.SetOutput(color.Output) // TODO: log to file option
log.Println(color.HiCyanString(wrapHyphensW(fmt.Sprintf("Welcome to %s v%s", projectLabel, projectVersion))))
l := logInstructions{
Location: "INITITALIZATION",
Task: "entry tasks",
Inline: false,
Color: color.CyanString,
}
//#endregion
//TODO: Github Update Check
// Load Configs
l.Task = "loadConfig"
settingsErrors := loadConfig()
if len(settingsErrors) > 0 {
log.Println(l.SetFlag(&lError).Log("Detected errors in settings..."))
l.Clear()
for section, err := range settingsErrors {
if err != nil {
log.Println(l.SetFlag(&lError).Log("\"%s\" ERROR: %s", section, err))
l.Clear()
}
}
}
// Load Database
l.Task = "loadDatabase"
if databaseError := loadDatabase(); databaseError != nil {
log.Println(l.SetFlag(&lError).Log("ERROR: %s", databaseError))
l.Clear()
}
}
var (
instagramAccount_Channel = make(chan feedThread)
rssFeed_Channel = make(chan feedThread)
twitterAccount_Channel = make(chan feedThread)
)
func getFeedCountLabel(filterGroup int) string {
feedCount := getFeedCount(filterGroup)
if feedCount == 0 {
return "no feeds"
} else if feedCount == 1 {
return "1 feed"
} else { // 2+
return fmt.Sprintf("%d feeds", feedCount)
}
}
func dataKeyReplacement(input string) string {
//TODO: Case-insensitive key replacement. -- If no streamlined way to do it, convert to lower to find substring location but replace normally
if strings.Contains(input, "{{") && strings.Contains(input, "}}") {
timeNow := time.Now()
keys := [][]string{
{"{{goVersion}}", runtime.Version()},
{"{{dgVersion}}", discordgo.VERSION},
{"{{dfbVersion}}", projectVersion},
{"{{apiVersion}}", discordgo.APIVersion},
{"{{numServers}}", fmt.Sprint(len(discord.State.Guilds))},
{"{{numAdmins}}", fmt.Sprint(len(discordConfig.Admins))},
{"{{timeNowShort}}", timeNow.Format("3:04pm")},
{"{{timeNowShortTZ}}", timeNow.Format("3:04pm MST")},
{"{{timeNowMid}}", timeNow.Format("3:04pm MST 1/2/2006")},
{"{{timeNowLong}}", timeNow.Format("3:04:05pm MST - January 2, 2006")},
{"{{timeNowShort24}}", timeNow.Format("15:04")},
{"{{timeNowShortTZ24}}", timeNow.Format("15:04 MST")},
{"{{timeNowMid24}}", timeNow.Format("15:04 MST 2/1/2006")},
{"{{timeNowLong24}}", timeNow.Format("15:04:05 MST - 2 January, 2006")},
{"{{uptime}}", durafmt.ParseShort(time.Since(timeLaunched)).String()},
{"{{linkCount}}", fmt.Sprint(refCount())},
{"{{feedCount}}", string(getFeedCountLabel(feed0))},
}
for _, key := range keys {
if strings.Contains(input, key[0]) {
input = strings.ReplaceAll(input, key[0], key[1])
}
}
}
return input
}
func main() {
l := logInstructions{
Location: "MAIN",
Task: "startup",
Inline: true,
Color: color.GreenString,
}
if err := openDiscord(); err != nil {
log.Println(l.SetFlag(&lError).Log("DISCORD LOGIN ERROR: %s", err))
l.ClearFlag()
}
if discordConfig.DeleteCommands {
deleteSlashCommands()
}
go addSlashCommands()
for api, err := range openAPIs() {
if err != nil {
log.Println(l.SetFlag(&lError).Log("API LOGIN ERROR: (%s): %s", api, err))
l.ClearFlag()
}
}
log.Println(l.Log("Startup finished, took %s...", uptime()))
// Start Presence Loop
if discordConfig.Presence != nil && len(discordConfig.Presence) > 0 {
go func() {
for {
runDiscordPresences() // no need to sleep because the function does after each rotation.
}
}()
}
// Spawn Feeds
l.Task = "spawning feeds"
catalogFeeds()
feedsClone := feeds
for k := range feedsClone {
go startFeed(&feeds[k])
}
go func() {
for {
select {
case instagramAccount_Triggered := <-instagramAccount_Channel:
{
if err := handleInstagramAccount(instagramAccount_Triggered.Config.(configModuleInstagramAcc)); err != nil {
log.Println(l.SetTask("handleInstagramAccount").SetFlag(&lError).Log(
"Error handling Instagram Account: %s", err.Error()))
l.Clear()
}
}
case rssFeed_Triggered := <-rssFeed_Channel:
{
if err := handleRssFeed(rssFeed_Triggered.Config.(configModuleRssFeed)); err != nil {
log.Println(l.SetTask("handleRssFeed").SetFlag(&lError).Log(
"Error handling RSS Feed: %s", err.Error()))
l.Clear()
}
}
case twitterAccount_Triggered := <-twitterAccount_Channel:
{
if err := handleTwitterAcc(twitterAccount_Triggered.Config.(configModuleTwitterAcc)); err != nil {
log.Println(l.SetTask("handleTwitterAcc").SetFlag(&lError).Log(
"Error handling Twitter Account: %s", err.Error()))
l.Clear()
}
}
}
time.Sleep(100 * time.Millisecond) // don't wanna loop infinitely with no delay
}
}()
l.Task = "running"
// Infinite loop until interrupted
signal.Notify(loop, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt, os.Kill)
<-loop
l.Task = "exit"
if discordConfig.DeleteCommands {
deleteSlashCommands()
}
log.Println(l.Log("Logging out of discord..."))
discord.Close()
log.Println(l.LogC(color.HiRedString, "Exiting..."))
}
func openAPIs() map[string]error {
errors := make(map[string]error)
var tmperr error
if tmperr = openInstagram(); tmperr != nil {
errors["login-instagram"] = tmperr
}
if tmperr = openTwitter(); tmperr != nil {
errors["login-twitter"] = tmperr
}
return errors
}