gambot/discord.go
2025-01-20 22:39:01 -08:00

73 lines
1.5 KiB
Go

package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"strings"
)
var commands = []*discordgo.ApplicationCommand{
{
Name: "gambo",
Description: "print gambo data",
},
}
var token, appID, guildID string
func handleMessage(s *discordgo.Session, i *discordgo.InteractionCreate) {
log.Println("Received gambo command")
c, err := content()
if err != nil {
log.Println(err)
return
}
embed := &discordgo.MessageEmbed{
Title: "== Degen Hall of Fame ==",
Description: c,
}
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{embed},
},
})
if err != nil {
log.Printf("could not respond to interaction: %s", err)
}
}
func content() (string, error) {
players, err := getPlayers()
if err != nil {
return "", err
}
var b strings.Builder
b.WriteString("**All Hail Gambolord " + players[0].Name + "!**\n")
b.WriteString("## Current Rankings\n")
m := 0
for _, player := range players {
if len(player.Name) > m {
m = len(player.Name)
}
}
b.WriteString("```\n")
for _, player := range players {
formattedVal := player.FormatVal()
if player.Val > 0 {
b.WriteString(fmt.Sprintf("%-*s +%*s\n", m+2, player.Name, len(formattedVal), formattedVal))
} else {
b.WriteString(fmt.Sprintf("%-*s %*s\n", m+2, player.Name, len(formattedVal), formattedVal))
}
}
b.WriteString("```\n")
return b.String(), nil
}