Skip to content

Commit

Permalink
feay: stnced command
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Jan 6, 2024
1 parent 8876272 commit 837bd99
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
12 changes: 12 additions & 0 deletions client/client_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ func (cm *Mgr) IsValidator(address string) (bool, error) {
return false, errors.New("unable to get validator info")
}

func (cm *Mgr) GetValidatorInfo(address string) (*pactus.GetValidatorResponse, error) {
for _, c := range cm.clients {
val, err := c.GetValidatorInfo(address)
if err != nil {
continue
}
return val, nil
}

return nil, errors.New("unable to get validator info")
}

func (cm *Mgr) Close() {
for addr, c := range cm.clients {
if err := c.Close(); err != nil {
Expand Down
55 changes: 35 additions & 20 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,27 +153,10 @@ func (b *Bot) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {

parts := strings.Split(strings.Split(peerInfo.Address, "/")[2], "/")
ip := parts[0]
fmt.Println(ip)
geoData := getGeoIP(ip)

notSyncedMsg := "this peer is not synced with network, gRPC is disabled or doesn't have public IP address."
syncedMsg := "**this peer is synced with network**"

isSynced := notSyncedMsg
c, err := client.NewClient(strings.Split(peerInfo.Address, "/")[2] + ":50052")
if err != nil {
isSynced = notSyncedMsg
}
lastBlockTime, _, err := c.LastBlockTime()
if err != nil {
isSynced = notSyncedMsg
}
currentTime := time.Now().Unix()

if (uint32(currentTime) - lastBlockTime) < 15 {
isSynced = syncedMsg
}

val, err := c.GetValidatorInfo(trimmedAddress)
val, err := b.cm.GetValidatorInfo(trimmedAddress)
if err != nil {
msg := p.Sprintf("An error occurred %v\n", err)
_, _ = s.ChannelMessageSendReply(m.ChannelID, msg, m.Reference())
Expand All @@ -185,7 +168,6 @@ func (b *Bot) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
msg += p.Sprintf("IP address: %v\n", peerInfo.Address)
msg += p.Sprintf("Agent: %v\n", peerInfo.Agent)
msg += p.Sprintf("Moniker: %v\n", peerInfo.Moniker)
msg += p.Sprintf("IsSynced: %v\n", isSynced)
msg += p.Sprintf("Country: %v\n", geoData.CountryName)
msg += p.Sprintf("City: %v\n", geoData.City)
msg += p.Sprintf("Region Name: %v\n", geoData.RegionName)
Expand All @@ -199,6 +181,38 @@ func (b *Bot) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
_, _ = s.ChannelMessageSendReply(m.ChannelID, msg, m.Reference())
return
}
if strings.Contains(strings.ToLower(m.Content), "synced") {
trimmedPrefix := strings.TrimPrefix(strings.ToLower(m.Content), "peer-info")
trimmedAddress := strings.Trim(trimmedPrefix, " ")

peerInfo, _, err := b.cm.GetPeerInfo(trimmedAddress)
if err != nil {
msg := p.Sprintf("An error occurred %v\n", err)
_, _ = s.ChannelMessageSendReply(m.ChannelID, msg, m.Reference())
return
}

notSyncedMsg := "this peer is not synced with network, gRPC is disabled or doesn't have public IP address."
syncedMsg := "**this peer is synced with network**"

msg := notSyncedMsg
c, err := client.NewClient(strings.Split(peerInfo.Address, "/")[2] + ":50052")
if err != nil {
msg = notSyncedMsg
}
lastBlockTime, _, err := c.LastBlockTime()
if err != nil {
msg = notSyncedMsg
}
currentTime := time.Now().Unix()

if (uint32(currentTime) - lastBlockTime) < 15 {
msg = syncedMsg
}

_, _ = s.ChannelMessageSendReply(m.ChannelID, msg, m.Reference())
return
}

if strings.ToLower(m.Content) == "my-referral" {
referrals := b.referralStore.GetAllReferrals()
Expand Down Expand Up @@ -252,6 +266,7 @@ func help(s *discordgo.Session, m *discordgo.MessageCreate) {
"To see the faucet address, simply type: `address`\n" +
"To get network information, simply type: `network`\n" +
"To get network health status, simply type: `health`\n" +
"To get your node syncing status, simply type: `synced [validator address]`\n" +
"To get peer information, simply type: `peer-info [validator address]`\n" +
"To get your referral information, simply type: `my-referral`\n" +
"To request faucet for test network *with referral code*: simply type `faucet-referral [validator address] [referral code]`\nreferral faucet will get 100 tPAC's\n" +
Expand Down

0 comments on commit 837bd99

Please sign in to comment.