-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
116 lines (93 loc) · 2.13 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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
const endpoint = "https://api.coinbase.com/v2/prices/eth-usd/spot"
const shards = 6
func worker(id int, token string) {
discord, err := discordgo.New("Bot " + token)
if err != nil {
log.Fatalf("Error creating discord session: %v", err)
}
discord.ShardCount = shards
discord.ShardID = id
err = discord.Open()
if err != nil {
log.Fatalf("Error opening discord ws: %v", err)
}
defer discord.Close()
for {
res, err := getPrice()
if err != nil {
log.Printf("Error getting price for shard %d: %v \n", id, err)
} else {
fmt.Printf("WorkerId %v got %v \n", id, "$"+res)
err = discord.UpdateWatchStatus(0, "$"+res)
if err != nil {
log.Printf("Error updating discord status for shard %d: %v \n", id, err)
}
}
time.Sleep(30 * time.Second)
}
}
func main() {
fmt.Println("hello world 🌍👋")
token := getEnvOrDie("TOKEN")
wg := sync.WaitGroup{}
for shardId := 0; shardId < shards; shardId++ {
wg.Add(1)
go worker(shardId, token)
}
wg.Wait()
}
func getEnvOrDie(key string) string {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading env: %v", err)
}
value, ok := os.LookupEnv(key)
if !ok {
log.Fatalf("Could not find %v in .env", key)
}
return value
}
type Response struct {
Data struct {
Base string `json:"base"`
Currency string `json:"currency"`
Amount string `json:"amount"`
} `json:"data"`
}
func getPrice() (string, error) {
res, err := http.Get(endpoint)
if res != nil {
defer res.Body.Close()
}
if err != nil {
return "", fmt.Errorf("failed to fetch: %v", err)
}
jsonPayload, err := decodeJson[Response](res.Body)
if err != nil {
return "", fmt.Errorf("failed to decode json: %v", err)
}
amount, err := strconv.ParseFloat(jsonPayload.Data.Amount, 64)
if err != nil {
return "", fmt.Errorf("invalid amount format: %v", err)
}
return fmt.Sprintf("%.0f", amount), nil
}
func decodeJson[T any](r io.Reader) (T, error) {
var v T
err := json.NewDecoder(r).Decode(&v)
return v, err
}