Add metrics
This commit is contained in:
@ -1,5 +1,13 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"nignoggobot/metrics"
|
||||
"strings"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
var AdminIDs = []int64{126131628}
|
||||
|
||||
func IsAdmin(userID int64) bool {
|
||||
@ -10,3 +18,75 @@ func IsAdmin(userID int64) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type StatsCommand struct{}
|
||||
|
||||
func (StatsCommand) Name() string { return "stats" }
|
||||
func (StatsCommand) Help() string { return "Show bot usage stats (admin only)." }
|
||||
func (StatsCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
if !IsAdmin(update.Message.From.ID) {
|
||||
return
|
||||
}
|
||||
stats := metrics.GetStats()
|
||||
var sb strings.Builder
|
||||
sb.WriteString("Command usage (all time):\n")
|
||||
for name, stat := range stats.Commands {
|
||||
sb.WriteString(fmt.Sprintf("/%s: %d\n", name, stat.AllTime))
|
||||
}
|
||||
sb.WriteString("\nCommand usage (last 30 days):\n")
|
||||
for name, stat := range stats.Commands {
|
||||
total := 0
|
||||
for _, v := range stat.Last30 {
|
||||
total += v
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("/%s: %d\n", name, total))
|
||||
}
|
||||
groups, privs, blocked := 0, 0, 0
|
||||
for _, chat := range stats.Chats {
|
||||
if chat.IsGroup {
|
||||
groups++
|
||||
} else {
|
||||
privs++
|
||||
}
|
||||
if chat.Blocked {
|
||||
blocked++
|
||||
}
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("\nGroups: %d\nPrivate chats: %d\nBlocked/muted: %d\n", groups, privs, blocked))
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, sb.String())
|
||||
bot.Send(msg)
|
||||
}
|
||||
|
||||
type BroadcastCommand struct{}
|
||||
|
||||
func (BroadcastCommand) Name() string { return "broadcast" }
|
||||
func (BroadcastCommand) Help() string {
|
||||
return "Broadcast a message to all chats (admin only). Usage: /broadcast <message>"
|
||||
}
|
||||
func (BroadcastCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
if !IsAdmin(update.Message.From.ID) {
|
||||
return
|
||||
}
|
||||
args := strings.TrimSpace(update.Message.CommandArguments())
|
||||
if args == "" {
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Usage: /broadcast <message>")
|
||||
bot.Send(msg)
|
||||
return
|
||||
}
|
||||
count := 0
|
||||
metrics.BroadcastMessage(func(chatID int64) error {
|
||||
msg := tgbotapi.NewMessage(chatID, args)
|
||||
_, err := bot.Send(msg)
|
||||
if err == nil {
|
||||
count++
|
||||
}
|
||||
return err
|
||||
})
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("Broadcast sent to %d chats.", count))
|
||||
bot.Send(msg)
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(StatsCommand{})
|
||||
Register(BroadcastCommand{})
|
||||
}
|
||||
|
Reference in New Issue
Block a user