-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
holders.go
136 lines (114 loc) · 3.5 KB
/
holders.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
package main
import (
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/prometheus/client_golang/prometheus"
"github.com/rssnyder/discord-stock-ticker/utils"
)
// Holders represents the json for holders
type Holders struct {
Network string `json:"network"`
Address string `json:"address"`
Activity string `json:"activity"`
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 (h *Holders) label() string {
label := strings.ToLower(fmt.Sprintf("%s-%s", h.Network, h.Address))
if len(label) > 32 {
label = label[:32]
}
return label
}
// Shutdown sends a signal to shut off the goroutine
func (h *Holders) Shutdown() {
h.close <- 1
}
func (h *Holders) watchHolders() {
// create a new discord session using the provided bot token.
dg, err := discordgo.New("Bot " + h.Token)
if err != nil {
logger.Errorf("Creating Discord session (%s): %s", h.ClientID, err)
lastUpdate.With(prometheus.Labels{"type": "holders", "ticker": fmt.Sprintf("%s-%s", h.Network, h.Address), "guild": "None"}).Set(0)
return
}
// show as online
err = dg.Open()
if err != nil {
logger.Errorf("Opening discord connection (%s): %s", h.ClientID, err)
lastUpdate.With(prometheus.Labels{"type": "holders", "ticker": fmt.Sprintf("%s-%s", h.Network, h.Address), "guild": "None"}).Set(0)
return
}
// set activity as desc
if h.Nickname {
err = dg.UpdateWatchStatus(0, h.Activity)
if err != nil {
logger.Errorf("Unable to set activity: %s\n", err)
} else {
logger.Debugf("Set activity")
}
}
// get guides for bot
guilds, err := dg.UserGuilds(100, "", "")
if err != nil {
logger.Errorf("Error getting guilds: %s\n", err)
h.Nickname = false
}
if len(guilds) == 0 {
h.Nickname = false
}
// check for frequency override
// set to one hour to avoid lockout
if *frequency != 0 {
h.Frequency = *frequency
}
// perform management operations
if *managed {
setName(dg, h.label())
}
logger.Infof("Watching holders for %s", h.Address)
ticker := time.NewTicker(time.Duration(h.Frequency) * time.Second)
h.close = make(chan int, 1)
for {
select {
case <-h.close:
logger.Infof("Shutting down price watching for %s", h.Activity)
return
case <-ticker.C:
holders, err := utils.GetHolders(h.Network, h.Address, h.APIToken)
if err != nil {
logger.Errorf("Error getting holders for %s/%s %s", h.Network, h.Address, err)
continue
}
displayName := fmt.Sprintf("%d", holders)
if h.Nickname {
for _, g := range guilds {
err = dg.GuildMemberNickname(g.ID, "@me", displayName)
if err != nil {
logger.Errorf("Error updating nickname: %s\n", err)
continue
} else {
logger.Debugf("Set nickname in %s: %s\n", g.Name, displayName)
}
lastUpdate.With(prometheus.Labels{"type": "holders", "ticker": fmt.Sprintf("%s-%s", h.Network, h.Address), "guild": g.Name}).SetToCurrentTime()
time.Sleep(time.Duration(h.Frequency) * time.Second)
}
} else {
err = dg.UpdateWatchStatus(0, displayName)
if err != nil {
logger.Errorf("Unable to set activity: %s\n", err)
} else {
logger.Debugf("Set activity: %s\n", displayName)
lastUpdate.With(prometheus.Labels{"type": "holders", "ticker": fmt.Sprintf("%s-%s", h.Network, h.Address), "guild": "None"}).SetToCurrentTime()
}
}
}
}
}