-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (106 loc) · 3.27 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/SocialFeedsBot/shards/gateway"
"github.com/SocialFeedsBot/shards/internal/logger"
"github.com/SocialFeedsBot/shards/internal/shardmanager"
env "github.com/joho/godotenv"
"github.com/sirupsen/logrus"
)
func main() {
// Format the logger
logger.Format()
// Load config
if os.Getenv("DEV") == "true" {
logrus.Debug("Running in development environment")
e := env.Load(".env.dev")
if e != nil {
logrus.Error(e)
return
}
} else {
logrus.Debug("Running in production environment")
e := env.Load()
if e != nil {
logrus.Error(e)
return
}
}
// Connect to the gateway
gateway := gateway.Gateway{
Address: os.Getenv("WEBSOCKET"),
Secret: os.Getenv("SECRET"),
}
// Create shard manager
manager := shardmanager.New("Bot " + os.Getenv("TOKEN"))
session := gateway.CreateSessionWithShardManager(manager)
session.ShardManager.LogChannel = os.Getenv("MANAGER_LOG")
logrus.Info("Starting the shard manager")
err := session.ShardManager.Start()
if err != nil {
logrus.Fatal("Faled to start: ", err)
return
}
if os.Getenv("BETA") != "yes" {
go startStatInterval(manager)
}
// Keep the process running until kill signals
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// Cleanly close the shards
logrus.Info("Stopping running shards")
session.ShardManager.StopAll()
}
func startStatInterval(manager *shardmanager.Manager) {
client := &http.Client{}
ticker := time.NewTicker(60 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
guildCount := manager.GetFullStatus().NumGuilds
// prom
re, _ := http.NewRequest("POST", fmt.Sprintf("%v/gauge/set/guilds/%v", os.Getenv("PROMETHEUS_URL"), guildCount), bytes.NewBufferString(""))
client.Do(re)
// top.gg
topggValues := Stats{ServerCount: guildCount}
topggData, _ := json.Marshal(topggValues)
topgg, _ := http.NewRequest("POST", fmt.Sprintf("https://top.gg/api/bots/%v/stats", os.Getenv("CLIENT_ID")), bytes.NewBuffer(topggData))
topgg.Header.Add("Authorization", os.Getenv("STATS_TOPGG"))
topgg.Header.Add("Content-Type", "application/json")
client.Do(topgg)
// dbl.com
dblValues := Stats{Guilds: guildCount}
dblData, _ := json.Marshal(dblValues)
dbl, _ := http.NewRequest("POST", fmt.Sprintf("https://discordbotlist.com/api/v1/bots/%v/stats", os.Getenv("CLIENT_ID")), bytes.NewBuffer(dblData))
dbl.Header.Add("Authorization", os.Getenv("STATS_DBL"))
dbl.Header.Add("Content-Type", "application/json")
client.Do(dbl)
// discord.bots.gg
dbotsValues := Stats{GuildCount: guildCount}
dbotsData, _ := json.Marshal(dbotsValues)
dbots, _ := http.NewRequest("POST", fmt.Sprintf("https://discord.bots.gg/api/v1/bots/%v/stats", os.Getenv("CLIENT_ID")), bytes.NewBuffer(dbotsData))
dbots.Header.Add("Authorization", os.Getenv("STATS_DBOTS"))
dbots.Header.Add("Content-Type", "application/json")
client.Do(dbots)
case <-quit:
ticker.Stop()
return
}
}
}()
}
type Stats struct {
Guilds int `json:"guilds,omitempty"`
ServerCount int `json:"server_count,omitempty"`
GuildCount int `json:"guildCount,omitempty"`
}