-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (83 loc) · 2.21 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
package main
import (
"os"
"strconv"
"time"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
"google.golang.org/api/drive/v3"
)
func main() {
if os.Getenv("DAEMON") == "1" {
seconds, err := strconv.ParseInt(os.Getenv("DAEMON_SLEEP_SECONDS"), 10, 0)
if err != nil {
log.Fatalf("Invalid DAEMON_SLEEP_SECONDS: %v", err)
}
run()
ticker := time.NewTicker(time.Duration(seconds) * time.Second)
defer ticker.Stop()
go func() {
for range ticker.C {
log.Debugf("Woke up after %v seconds", os.Getenv("DAEMON_SLEEP_SECS"))
run()
}
}()
select {}
} else {
run()
}
}
func run() {
setupLogs()
token := os.Getenv("DISCORD_BOT_TOKEN")
dg := initDiscordGo(token)
log.Info("Discord init'ed")
driveService := initGDriveSvc(
os.Getenv("GOOGLE_CREDENTIALS_FILE"),
os.Getenv("GOOGLE_TOKEN_FILE"),
)
log.Info("GDrive init'd")
processedFilePath = os.Getenv("STATE_FILE")
urlFile := initProcessedEntities()
log.Info("State file init'ed")
initMetrics()
log.Info("Metrics init'd")
if os.Getenv("RUN_E2E") == "1" {
log.Warn("Running E2E")
validateCanDownloadFile(
dg,
driveService,
os.Getenv("E2E_CHANNEL_ID"),
os.Getenv("E2E_MESSAGE_ID"),
)
log.Info("E2E Completed")
os.Exit(0)
}
channel_ids := getChannelIds(dg, os.Getenv("DISCORD_GUILD_ID"))
for _, channel_id := range channel_ids {
scanChannel(dg, channel_id, driveService)
}
log.Infof("All files downloaded.")
defer dg.Close()
defer urlFile.Close()
lastRunSuccess.WithLabelValues().Set(1)
log.Infof("The application completed successfully.")
}
func validateCanDownloadFile(dg *discordgo.Session, driveService *drive.Service, channelID string, messageID string) error {
msgs, err := dg.ChannelMessages(channelID, 1, "", "", messageID)
if err != nil {
log.Fatalf("error fetching message with ID %s: %v", messageID, err)
}
log.Debugf("E2E: Got Message %v", msgs)
msg := msgs[0]
// Check if the message has attachments
if len(msg.Attachments) == 0 {
log.Fatalf("no attachments found in message with ID %s", messageID)
}
var messages []*discordgo.Message
messages = append(messages, msg)
log.Debugf("Scanning....")
scanMessages(driveService, messages)
log.Debugf("Scan completed")
return nil
}