-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (86 loc) · 2.58 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
package main
import (
"context"
"os"
"strconv"
"time"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/webhook"
"github.com/disgoorg/snowflake/v2"
"github.com/enzofoucaud/exrond-notifier/config"
"github.com/enzofoucaud/exrond-notifier/exrond"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func init() {
// LOG
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
func main() {
log.Info().Msg("Starting bot")
c, err := config.GetConfig()
if err != nil {
log.Err(err).Msg("error getting config")
return
}
switch c.LogLevel {
case "INFO":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "DEBUG":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
default:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
for {
pairs, err := exrond.GetPairsQuery()
if err != nil {
log.Err(err).Msg("error getting pairs")
return
}
for _, pair := range pairs.Data.Pairs {
log.Debug().Msg("--------------")
log.Debug().Msg("Checking pair " + pair.FirstToken.Name + " " + pair.SecondToken.Name)
log.Debug().Msg("First token price: " + pair.FirstTokenPrice)
log.Debug().Msg("Second token price: " + pair.SecondTokenPrice)
// Check if pair is WAGMI
for _, token := range c.Tokens {
if token.Token == pair.SecondToken.Name {
pairFloat, _ := strconv.ParseFloat(pair.SecondTokenPrice, 64)
if token.IsBelow {
if pairFloat <= token.Price {
message := pair.SecondToken.Name + " alert: token is below " + pair.SecondTokenPrice
err := Discord(message, c.DiscordID, c.DiscordToken)
if err != nil {
log.Err(err).Msg("error sending discord notification")
}
log.Info().Msg("Notifier sent for " + pair.FirstToken.Name + " " + pair.SecondToken.Name)
}
}
if token.IsAbove {
if pairFloat >= token.Price {
message := pair.SecondToken.Name + " alert: token is above " + pair.SecondTokenPrice
err := Discord(message, c.DiscordID, c.DiscordToken)
if err != nil {
log.Err(err).Msg("error sending discord notification")
}
log.Info().Msg("Notifier sent for " + pair.FirstToken.Name + " " + pair.SecondToken.Name)
}
}
}
}
}
log.Debug().Msg("--------------")
time.Sleep(time.Duration(c.SleepTime) * time.Second)
}
}
func Discord(message, discordID, discordToken string) error {
client := webhook.New(snowflake.MustParse(discordID), discordToken)
defer client.Close(context.TODO())
if _, err := client.CreateMessage(discord.NewWebhookMessageCreateBuilder().
SetContent(message).
Build(),
); err != nil {
return err
}
return nil
}