forked from rssnyder/discord-stock-ticker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas.go
135 lines (113 loc) · 3.33 KB
/
gas.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
package main
import (
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/prometheus/client_golang/prometheus"
"github.com/rssnyder/discord-stock-ticker/utils"
)
// Gas represents the gas data
type Gas struct {
Network string `json:"network"`
Nickname bool `json:"nickname"`
Frequency int `json:"frequency"`
APIToken string `json:"api_token"`
ClientID string `json:"client_id"`
Token string `json:"discord_bot_token"`
close chan int `json:"-"`
}
// label returns a human readble id for this bot
func (g *Gas) label() string {
label := strings.ToLower(g.Network)
if len(label) > 32 {
label = label[:32]
}
return label
}
// Shutdown sends a signal to shut off the goroutine
func (g *Gas) Shutdown() {
g.close <- 1
}
// watchGasPrice gets gas prices and rotates through levels
func (g *Gas) watchGasPrice() {
// create a new discord session using the provided bot token.
dg, err := discordgo.New("Bot " + g.Token)
if err != nil {
logger.Errorf("Creating Discord session (%s): %s", g.ClientID, err)
lastUpdate.With(prometheus.Labels{"type": "gas", "ticker": g.Network, "guild": "None"}).Set(0)
return
}
// show as online
err = dg.Open()
if err != nil {
logger.Errorf("Opening discord connection (%s): %s", g.ClientID, err)
lastUpdate.With(prometheus.Labels{"type": "gas", "ticker": g.Network, "guild": "None"}).Set(0)
return
}
// Get guides for bot
guilds, err := dg.UserGuilds(100, "", "")
if err != nil {
logger.Errorf("Error getting guilds: %s\n", err)
g.Nickname = false
}
if len(guilds) == 0 {
g.Nickname = false
}
// check for frequency override
// set to ten min to avoid lockout
if *frequency != 0 {
g.Frequency = 600
}
// perform management operations
if *managed {
setName(dg, g.label())
}
logger.Infof("Watching gas price for %s", g.Network)
ticker := time.NewTicker(time.Duration(g.Frequency) * time.Second)
g.close = make(chan int, 1)
// watch gas price
for {
select {
case <-g.close:
logger.Infof("Shutting down price watching for %s\n", g.Network)
return
case <-ticker.C:
// get gas prices
gasPrices, err := utils.GetGasPrices(g.Network, g.APIToken)
if err != nil {
logger.Errorf("Error getting rates: %s\n", err)
continue
}
nickname := fmt.Sprintf("⚡ %d 🤔 %d 🐌 %d", gasPrices.Instant, gasPrices.Fast, gasPrices.Standard)
// change nickname
if g.Nickname {
for _, gu := range guilds {
err = dg.GuildMemberNickname(gu.ID, "@me", nickname)
if err != nil {
logger.Errorf("Error updating nickname: %s\n", err)
continue
} else {
logger.Debugf("Set nickname in %s: %s\n", gu.Name, nickname)
}
lastUpdate.With(prometheus.Labels{"type": "gas", "ticker": g.Network, "guild": gu.Name}).SetToCurrentTime()
time.Sleep(time.Duration(g.Frequency) * time.Second)
}
err = dg.UpdateWatchStatus(0, "Fast, Avg, Slow")
if err != nil {
logger.Errorf("Unable to set activity: %s\n", err)
} else {
logger.Debugf("Set activity")
}
} else {
err = dg.UpdateWatchStatus(0, nickname)
if err != nil {
logger.Errorf("Unable to set activity: %s\n", err)
} else {
logger.Debugf("Set activity: %s\n", nickname)
lastUpdate.With(prometheus.Labels{"type": "gas", "ticker": g.Network, "guild": "None"}).SetToCurrentTime()
}
}
}
}
}