forked from rssnyder/discord-stock-ticker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
holders.go
123 lines (103 loc) · 3.12 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
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"`
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
}
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("Error creating Discord session: %s\n", 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("error opening discord connection: %s\n", 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.UpdateGameStatus(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 = 3600
}
// 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)
for {
select {
case <-h.Close:
logger.Infof("Shutting down price watching for %s", h.Activity)
return
case <-ticker.C:
nickname := utils.GetHolders(h.Network, h.Address)
if h.Nickname {
for _, g := range guilds {
err = dg.GuildMemberNickname(g.ID, "@me", nickname)
if err != nil {
logger.Errorf("Error updating nickname: %s\n", err)
continue
} else {
logger.Debugf("Set nickname in %s: %s\n", g.Name, nickname)
}
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.UpdateGameStatus(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": "holders", "ticker": fmt.Sprintf("%s-%s", h.Network, h.Address), "guild": "None"}).SetToCurrentTime()
}
}
}
}
}