Skip to content

Commit

Permalink
add 0 command for discord
Browse files Browse the repository at this point in the history
  • Loading branch information
gadost committed Feb 8, 2022
1 parent a26699e commit 1cc2e21
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 41 deletions.
2 changes: 1 addition & 1 deletion alert/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions status/discord.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
43 changes: 43 additions & 0 deletions status/status.go
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 0 additions & 39 deletions status/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 1cc2e21

Please sign in to comment.