format claims list as table

absences
William Perron 2 years ago
parent 601aa1076b
commit ed2dcc1969

@ -8,6 +8,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strconv"
"strings" "strings"
"syscall" "syscall"
@ -29,7 +30,7 @@ var (
store *themis.Store store *themis.Store
) )
var () type Handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
func main() { func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT) ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT)
@ -58,25 +59,20 @@ func main() {
} }
commands := []*discordgo.ApplicationCommand{ commands := []*discordgo.ApplicationCommand{
{
Name: "themis",
Description: "Call dibs on EU4 provinces",
Type: discordgo.ChatApplicationCommand,
Options: []*discordgo.ApplicationCommandOption{
{ {
Name: "ping", Name: "ping",
Description: "Ping Themis", Description: "Ping Themis",
Type: discordgo.ApplicationCommandOptionSubCommand, Type: discordgo.ChatApplicationCommand,
}, },
{ {
Name: "list-claims", Name: "list-claims",
Description: "List current claims", Description: "List current claims",
Type: discordgo.ApplicationCommandOptionSubCommand, Type: discordgo.ChatApplicationCommand,
}, },
{ {
Name: "claim", Name: "claim",
Description: "Take a claim on provinces", Description: "Take a claim on provinces",
Type: discordgo.ApplicationCommandOptionSubCommand, Type: discordgo.ChatApplicationCommand,
Options: []*discordgo.ApplicationCommandOption{ Options: []*discordgo.ApplicationCommandOption{
{ {
Name: "claim-type", Name: "claim-type",
@ -90,15 +86,9 @@ func main() {
}, },
}, },
}, },
},
},
} }
handlers := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ handlers := map[string]Handler{
"themis": func(s *discordgo.Session, i *discordgo.InteractionCreate) { "ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
switch options[0].Name {
case "ping":
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
@ -108,7 +98,8 @@ func main() {
if err != nil { if err != nil {
log.Println("[error] failed to respond to command:", err) log.Println("[error] failed to respond to command:", err)
} }
case "list-claims": },
"list-claims": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
claims, err := store.ListClaims(ctx) claims, err := store.ListClaims(ctx)
if err != nil { if err != nil {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
@ -124,9 +115,9 @@ func main() {
sb := strings.Builder{} sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("There are currently %d claims:\n", len(claims))) sb.WriteString(fmt.Sprintf("There are currently %d claims:\n", len(claims)))
for _, c := range claims { sb.WriteString("```\n")
sb.WriteString(fmt.Sprintf("%s\n", c)) sb.WriteString(formatClaimsTable(claims))
} sb.WriteString("```\n")
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
@ -137,8 +128,9 @@ func main() {
if err != nil { if err != nil {
log.Println("[error] failed to respond to command:", err) log.Println("[error] failed to respond to command:", err)
} }
case "claim": },
opts := options[0].Options "claim": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
opts := i.ApplicationCommandData().Options
claimType, err := themis.ClaimTypeFromString(opts[0].StringValue()) claimType, err := themis.ClaimTypeFromString(opts[0].StringValue())
if err != nil { if err != nil {
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
@ -183,17 +175,6 @@ func main() {
if err != nil { if err != nil {
log.Println("[error] failed to respond to command:", err) log.Println("[error] failed to respond to command:", err)
} }
default:
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Oops, I don't know any `%s` action", options[0].Name),
},
})
if err != nil {
log.Println("[error] failed to respond to command:", err)
}
}
}, },
} }
@ -245,7 +226,7 @@ func touchDbFile(path string) error {
return nil return nil
} }
func registerHandlers(sess *discordgo.Session, handlers map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)) { func registerHandlers(sess *discordgo.Session, handlers map[string]Handler) {
sess.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) { sess.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator) log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
}) })
@ -255,3 +236,32 @@ func registerHandlers(sess *discordgo.Session, handlers map[string]func(s *disco
} }
}) })
} }
const TABLE_PATTERN = "| %-*s | %-*s | %-*s | %-*s |\n"
func formatClaimsTable(claims []themis.Claim) string {
sb := strings.Builder{}
maxLengths := []int{2, 6, 4, 4} // id, player, type, name
for _, c := range claims {
sid := strconv.Itoa(c.ID)
if len(sid) > maxLengths[0] {
maxLengths[0] = len(sid)
}
if len(c.Player) > maxLengths[1] {
maxLengths[1] = len(c.Player)
}
if len(c.Type) > maxLengths[2] {
maxLengths[2] = len(c.Type)
}
if len(c.Name) > maxLengths[3] {
maxLengths[3] = len(c.Name)
}
}
sb.WriteString(fmt.Sprintf(TABLE_PATTERN, maxLengths[0], "ID", maxLengths[1], "Player", maxLengths[2], "Type", maxLengths[3], "Name"))
sb.WriteString(fmt.Sprintf(TABLE_PATTERN, maxLengths[0], strings.Repeat("-", maxLengths[0]), maxLengths[1], strings.Repeat("-", maxLengths[1]), maxLengths[2], strings.Repeat("-", maxLengths[2]), maxLengths[3], strings.Repeat("-", maxLengths[3])))
for _, c := range claims {
sb.WriteString(fmt.Sprintf(TABLE_PATTERN, maxLengths[0], strconv.Itoa(c.ID), maxLengths[1], c.Player, maxLengths[2], c.Type, maxLengths[3], c.Name))
}
return sb.String()
}

Loading…
Cancel
Save