Skip to content

Commit

Permalink
Add stats slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanGuigou committed Nov 23, 2024
1 parent c60ed38 commit 4a32c5b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
22 changes: 16 additions & 6 deletions go-is-gud/presence.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
)

type Presence struct {
logger *zap.SugaredLogger
db *pgxpool.Pool
ctx context.Context
cache *cache.Cache
client rpc.Presence
isActive bool
logger *zap.SugaredLogger
db *pgxpool.Pool
ctx context.Context
cache *cache.Cache
client rpc.Presence
isActive bool
startedAt time.Time
}

func New(logger *zap.SugaredLogger, ctx context.Context, db *pgxpool.Pool) *Presence {
Expand All @@ -34,10 +35,19 @@ func New(logger *zap.SugaredLogger, ctx context.Context, db *pgxpool.Pool) *Pres
p.isActive = false
}()

p.startedAt = time.Now().UTC()
p.logger.Info("presence service ready")
return p
}

func (p *Presence) Stats() time.Duration {
if !p.isActive {
return 0
}

return time.Now().UTC().Sub(p.startedAt)
}

func (p *Presence) IsHealthy() bool {
return p.isActive
}
Expand Down
35 changes: 35 additions & 0 deletions go-is-gud/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func (slash *Slash) commandHandler(s *discordgo.Session, i *discordgo.Interactio
{
meCommand(slash.logger, s, i.Interaction, event, ratelimitMe, slash.presence)
}
case "stats":
{
statsCommand(slash.logger, s, i.Interaction, ratelimitMe, slash.presence)
}
case "profile":
{
profileCommand(slash.logger, s, i.Interaction)
Expand Down Expand Up @@ -196,6 +200,37 @@ func meCommand(logger *zap.SugaredLogger, s *discordgo.Session, i *discordgo.Int
"content", content)
}

func statsCommand(logger *zap.SugaredLogger, s *discordgo.Session, i *discordgo.Interaction, ratelimit ratelimit.Limiter, presence *Presence) {
err := s.InteractionRespond(i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
},
})

if err != nil {
logger.Error(err)
return
}

ratelimit.Take()
var content string
uptime := presence.Stats()
content = fmt.Sprintf("```\nUptime: %s\n```", uptime)
_, err = s.FollowupMessageCreate(i, true, &discordgo.WebhookParams{
Content: content,
})

if err != nil {
logger.Error(err)
return
}

logger.Infow("slash command completed",
"name", "status",
"content", content)
}

func profileCommand(logger *zap.SugaredLogger, s *discordgo.Session, i *discordgo.Interaction) {
err := s.InteractionRespond(i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseModal,
Expand Down

0 comments on commit 4a32c5b

Please sign in to comment.