diff --git a/FallenSub/modules/misc.go b/FallenSub/modules/misc.go new file mode 100644 index 0000000..8eceb43 --- /dev/null +++ b/FallenSub/modules/misc.go @@ -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 +} diff --git a/FallenSub/modules/start.go b/FallenSub/modules/start.go index 0a1f3bb..8301aac 100644 --- a/FallenSub/modules/start.go +++ b/FallenSub/modules/start.go @@ -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 { @@ -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, "Pinging", &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) }