Skip to content

Commit

Permalink
ping: add uptime and improve
Browse files Browse the repository at this point in the history
  • Loading branch information
AshokShau committed Jul 28, 2024
1 parent 694b694 commit 1361526
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
68 changes: 68 additions & 0 deletions FallenSub/modules/misc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package modules

import (
"fmt"
"strings"
"time"
)

// getFormattedDuration returns a formatted string representing the duration
func getFormattedDuration(diff time.Duration) string {
seconds := int(diff.Seconds())
minutes := seconds / 60
hours := minutes / 60
days := hours / 24
weeks := days / 7
months := days / 30

// Calculate remaining values after each unit is accounted for
remainingSeconds := seconds % 60
remainingMinutes := minutes % 60
remainingHours := hours % 24
remainingDays := days % 7

var text string

// Format months
if months != 0 {
text += fmt.Sprintf("%d months ", months)
}

// Format weeks
if weeks != 0 {
text += fmt.Sprintf("%d weeks ", weeks)
}

// Format days
if remainingDays != 0 {
text += fmt.Sprintf("%d days ", remainingDays)
}

// Format hours
if remainingHours != 0 {
text += fmt.Sprintf("%d hours ", remainingHours)
}

// Format minutes
if remainingMinutes != 0 {
text += fmt.Sprintf("%d minutes ", remainingMinutes)
}

// Format seconds
if remainingSeconds != 0 || text == "" { // Include seconds if there's no larger unit or if there are remaining seconds
text += fmt.Sprintf("%d seconds", remainingSeconds)
}

// Trim any trailing space
text = trimSuffix(text, " ")

return text
}

// trimSuffix removes the suffix from the string if it exists
func trimSuffix(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
return s[:len(s)-len(suffix)]
}
return s
}
21 changes: 19 additions & 2 deletions FallenSub/modules/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"
)

var StartTime = time.Now()

// start is the handler for the /start command
func start(b *gotgbot.Bot, ctx *ext.Context) error {
if ctx.EffectiveChat.Type == gotgbot.ChatTypePrivate {
Expand Down Expand Up @@ -44,11 +46,26 @@ func start(b *gotgbot.Bot, ctx *ext.Context) error {

// ping is the handler for the /ping command
func ping(b *gotgbot.Bot, ctx *ext.Context) error {
msg := ctx.EffectiveMessage
startTime := time.Now()

if msg, err := ctx.EffectiveMessage.Reply(b, "Pong!", nil); err != nil {
rest, err := msg.Reply(b, "<code>Pinging</code>", &gotgbot.SendMessageOpts{ParseMode: "HTML"})
if err != nil {
return logError(fmt.Sprintf("[Ping] Error sending message - %v", err), err)
} else if _, _, err := msg.EditText(b, "Pong! "+time.Since(startTime).String(), nil); err != nil {
}

// Calculate latency
elapsedTime := time.Since(startTime)

// Calculate uptime
uptime := time.Since(StartTime)
formattedUptime := getFormattedDuration(uptime)

location, _ := time.LoadLocation("Asia/Kolkata")
responseText := fmt.Sprintf("Pinged in %vms (Latency: %.2fs) at %s\n\nUptime: %s", elapsedTime.Milliseconds(), elapsedTime.Seconds(), time.Now().In(location).Format(time.RFC1123), formattedUptime)

_, _, err = rest.EditText(b, responseText, nil)
if err != nil {
return logError(fmt.Sprintf("[Ping] Error editing message - %v", err), err)
}

Expand Down

0 comments on commit 1361526

Please sign in to comment.