diff --git a/alert/discord.go b/alert/discord.go index a22bc84..b0587e3 100644 --- a/alert/discord.go +++ b/alert/discord.go @@ -20,7 +20,7 @@ func DiscordSend(s string, m string) { log.Println("error opening connection,", err) return } - dg.ChannelMessageSend(fmt.Sprint(conf.MainConfig.Discord.ChannelID), "***___"+s+"___***"+": \n"+m) + dg.ChannelMessageSend(fmt.Sprint(conf.MainConfig.Discord.ChannelID), "***"+s+"***"+": \n"+m) dg.Close() diff --git a/main.go b/main.go index 5a6ef77..a0e81e9 100644 --- a/main.go +++ b/main.go @@ -14,10 +14,15 @@ var wgMain sync.WaitGroup func main() { cfg, chains := conf.ConfLoad() if conf.MainConfig.Telegram.Enabled { - log.Println("Telegram command handler started.") + log.Println("Telegram commands handler bot started.") wgMain.Add(1) go status.TelegramHandler() } + if conf.MainConfig.Discord.Enabled { + log.Println("Discord commands handler bot started.") + wgMain.Add(1) + go status.DiscordHandler() + } if conf.MainConfig.Settings.GithubReleaseMonitor { log.Println("Github repositories monitor started.") wgMain.Add(1) diff --git a/status/discord.go b/status/discord.go new file mode 100644 index 0000000..b69e2ab --- /dev/null +++ b/status/discord.go @@ -0,0 +1,40 @@ +package status + +import ( + "fmt" + "log" + + "github.com/bwmarrin/discordgo" + "github.com/gadost/telescope/conf" +) + +func DiscordHandler() { + dg, err := discordgo.New("Bot " + conf.MainConfig.Discord.Token) + if err != nil { + log.Println("error creating Discord session,", err) + return + } + dg.AddHandler(messageCreate) + + // Open a websocket connection to Discord and begin listening. + err = dg.Open() + if err != nil { + fmt.Println("error opening connection,", err) + return + } + + // Cleanly close down the Discord session. + defer dg.Close() +} + +func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + + // Ignore all messages created by the bot itself + if m.Author.ID == s.State.User.ID { + return + } + // If the message is "ping" reply with "Pong!" + if m.Content == "$status" { + s.ChannelMessageSend(m.ChannelID, StatusCollection()) + } +} diff --git a/status/status.go b/status/status.go index 6c12ae3..f11d107 100644 --- a/status/status.go +++ b/status/status.go @@ -1 +1,44 @@ package status + +import ( + "strconv" + "strings" + + "github.com/gadost/telescope/watcher" +) + +// StatusCollection collect status from Chain struct +func StatusCollection() string { + var collection string + var cu string + var badRPC []string + collection = collection + "*Status:*\n\n" + + for i := range watcher.Chains.Chain { + for _, k := range watcher.Chains.Chain[i].Node { + if k.Status.SyncInfo.LatestBlockHeight > 0 { + collection += "*Net:* `" + k.Status.NodeInfo.Network + "`\n*Moniker:* `" + k.Status.NodeInfo.Moniker + "`\n" + if k.Status.SyncInfo.CatchingUp { + cu = "Yes" + } else { + cu = "No" + } + + collection += "*CatchingUp:* `" + cu + "`\n" + collection += "*Last known height:* `" + strconv.Itoa(int(k.Status.SyncInfo.LatestBlockHeight)) + "`\n" + collection += "*Last known block time :* `" + k.Status.SyncInfo.LatestBlockTime.Format("2006-01-02 15:04:05") + "`\n" + collection += "`_________________________`\n" + } else { + if k.MonitoringEnabled { + badRPC = append(badRPC, k.RPC) + } + } + } + } + + if len(badRPC) > 0 { + collection += "*🔴Unreachable RPCs:*\n`" + strings.Join(badRPC, "\n") + "`" + } + + return collection +} diff --git a/status/telegram.go b/status/telegram.go index 296de42..b1d267e 100644 --- a/status/telegram.go +++ b/status/telegram.go @@ -2,51 +2,12 @@ package status import ( "log" - "strconv" - "strings" "time" "github.com/gadost/telescope/conf" - "github.com/gadost/telescope/watcher" tele "gopkg.in/telebot.v3" ) -// StatusCollection collect status from Chain struct -func StatusCollection() string { - var collection string - var cu string - var badRPC []string - collection = collection + "*Status:*\n\n" - - for i := range watcher.Chains.Chain { - for _, k := range watcher.Chains.Chain[i].Node { - if k.Status.SyncInfo.LatestBlockHeight > 0 { - collection += "*Net:* `" + k.Status.NodeInfo.Network + "`\n*Moniker:* `" + k.Status.NodeInfo.Moniker + "`\n" - if k.Status.SyncInfo.CatchingUp { - cu = "Yes" - } else { - cu = "No" - } - - collection += "*CatchingUp:* `" + cu + "`\n" - collection += "*Last known height:* `" + strconv.Itoa(int(k.Status.SyncInfo.LatestBlockHeight)) + "`\n" - collection += "*Last known block time :* `" + k.Status.SyncInfo.LatestBlockTime.Format("2006-01-02 15:04:05") + "`\n" - collection += "`_________________________`\n" - } else { - if k.MonitoringEnabled { - badRPC = append(badRPC, k.RPC) - } - } - } - } - - if len(badRPC) > 0 { - collection += "*🔴Unreachable RPCs:*\n`" + strings.Join(badRPC, "\n") + "`" - } - - return collection -} - // TelegramHandler start telegram command handler func TelegramHandler() { var pref = tele.Settings{