forked from rssnyder/discord-stock-ticker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circulating.go
211 lines (186 loc) · 6.34 KB
/
circulating.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
package main
import (
"fmt"
"math"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/go-redis/redis/v8"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/text/language"
"golang.org/x/text/message"
"github.com/rssnyder/discord-stock-ticker/utils"
)
type Circulating struct {
Ticker string `json:"ticker"`
Name string `json:"name"`
Nickname bool `json:"nickname"`
Frequency int `json:"frequency"`
CurrencySymbol string `json:"currency_symbol"`
Decimals int `json:"decimals"`
Activity string `json:"activity"`
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 (c *Circulating) label() string {
label := strings.ToLower(fmt.Sprintf("%s-%s", c.Name, c.CurrencySymbol))
if len(label) > 32 {
label = label[:32]
}
return label
}
func (c *Circulating) watchCirculating() {
var nilCache *redis.Client
// create a new discord session using the provided bot token.
dg, err := discordgo.New("Bot " + c.Token)
if err != nil {
logger.Errorf("Creating Discord session: %s", err)
lastUpdate.With(prometheus.Labels{"type": "marketcap", "ticker": c.Name, "guild": "None"}).Set(0)
return
}
// show as online
err = dg.Open()
if err != nil {
logger.Errorf("Opening discord connection: %s", err)
lastUpdate.With(prometheus.Labels{"type": "marketcap", "ticker": c.Name, "guild": "None"}).Set(0)
return
}
// Get guides for bot
guilds, err := dg.UserGuilds(100, "", "")
if err != nil {
logger.Errorf("Getting guilds: %s", err)
c.Nickname = false
}
if len(guilds) == 0 {
c.Nickname = false
}
// check for frequency override
if *frequency != 0 {
c.Frequency = *frequency
}
// Grab custom activity messages
var custom_activity []string
itr := 0
itrSeed := 0.0
if c.Activity != "" {
custom_activity = strings.Split(c.Activity, ";")
}
// perform management operations
if *managed {
setName(dg, c.label())
}
logger.Infof("Watching marketcap for %s", c.Name)
ticker := time.NewTicker(time.Duration(c.Frequency) * time.Second)
// continuously watch
for {
select {
case <-c.Close:
logger.Infof("Shutting down price watching for %s", c.Name)
return
case <-ticker.C:
logger.Debugf("Fetching crypto price for %s", c.Name)
var priceData utils.GeckoPriceResults
var fmtPrice string
// get the coin price data
if rdb == nilCache {
priceData, err = utils.GetCryptoPrice(c.Name)
} else {
priceData, err = utils.GetCryptoPriceCache(rdb, ctx, c.Name)
}
if err != nil {
logger.Errorf("Unable to fetch marketcap for %s: %s", c.Name, err)
continue
}
// set currency
if c.CurrencySymbol == "" {
c.CurrencySymbol = strings.ToUpper(priceData.Symbol)
}
// Check for custom decimal places
p := message.NewPrinter(language.English)
switch c.Decimals {
case 1:
fmtPrice = p.Sprintf("%.1f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
case 2:
fmtPrice = p.Sprintf("%.2f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
case 3:
fmtPrice = p.Sprintf("%.3f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
case 4:
fmtPrice = p.Sprintf("%.4f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
case 5:
fmtPrice = p.Sprintf("%.5f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
case 6:
fmtPrice = p.Sprintf("%.6f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
case 7:
fmtPrice = p.Sprintf("%.7f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
case 8:
fmtPrice = p.Sprintf("%.8f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
case 9:
fmtPrice = p.Sprintf("%.9f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
case 10:
fmtPrice = p.Sprintf("%.10f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
case 11:
fmtPrice = p.Sprintf("%.11f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
default:
fmtPrice = p.Sprintf("%.2f %s", priceData.MarketData.CirculatingSupply, c.CurrencySymbol)
switch {
case priceData.MarketData.CirculatingSupply < 1000000:
fmtPrice = p.Sprintf("%.2fk %s", priceData.MarketData.CirculatingSupply/1000, c.CurrencySymbol)
case priceData.MarketData.CirculatingSupply < 1000000000:
fmtPrice = p.Sprintf("%.2fM %s", priceData.MarketData.CirculatingSupply/1000000, c.CurrencySymbol)
case priceData.MarketData.CirculatingSupply < 1000000000000:
fmtPrice = p.Sprintf("%.2fB %s", priceData.MarketData.CirculatingSupply/1000000000, c.CurrencySymbol)
case priceData.MarketData.CirculatingSupply < 1000000000000000:
fmtPrice = p.Sprintf("%.2fT %s", priceData.MarketData.CirculatingSupply/1000000000000, c.CurrencySymbol)
}
}
// update nickname instead of activity
if c.Nickname {
nickname := fmt.Sprintf("%s %s", c.Ticker, fmtPrice)
// Update nickname in guilds
for _, g := range guilds {
err = dg.GuildMemberNickname(g.ID, "@me", nickname)
if err != nil {
logger.Errorf("Updating nickname: %s", err)
continue
}
logger.Debugf("Set nickname in %s: %s", g.Name, nickname)
lastUpdate.With(prometheus.Labels{"type": "marketcap", "ticker": c.Name, "guild": g.Name}).SetToCurrentTime()
time.Sleep(time.Duration(c.Frequency) * time.Second)
}
// Custom activity messages
if len(custom_activity) > 0 {
// Display the real activity once per cycle
if itr == len(custom_activity) {
itr = 0
itrSeed = 0.0
} else if math.Mod(itrSeed, 2.0) == 1.0 {
c.Activity = custom_activity[itr]
itr++
itrSeed++
} else {
c.Activity = custom_activity[itr]
itrSeed++
}
}
// set activity
err = dg.UpdateGameStatus(0, c.Activity)
if err != nil {
logger.Errorf("Unable to set activity: %s", err)
} else {
logger.Debugf("Set activity: %s", c.Activity)
}
} else {
// format activity
err = dg.UpdateGameStatus(0, c.Activity)
if err != nil {
logger.Errorf("Unable to set activity: %s", err)
} else {
logger.Debugf("Set activity: %s", c.Activity)
lastUpdate.With(prometheus.Labels{"type": "marketcap", "ticker": c.Name, "guild": "None"}).SetToCurrentTime()
}
}
}
}
}