...

Source file src/go.formulabun.club/discord/slashplayers/formatting.go

Documentation: go.formulabun.club/discord/slashplayers

     1  package slashplayers
     2  
     3  import (
     4  	"fmt"
     5  
     6  	bunString "go.formulabun.club/functional/strings"
     7  )
     8  
     9  func joinNames(names []string, verb string) string {
    10  	if len(names) == 0 {
    11  		return ""
    12  	}
    13  	if len(names) == 1 {
    14  		return fmt.Sprintf("%s is %s.", names[0], verb)
    15  	}
    16  	last := names[len(names)-1]
    17  	init := names[:len(names)-1]
    18  	return fmt.Sprintf("%s and %s are %s.", bunString.Join(init, ", "), last, verb)
    19  }
    20  
    21  func formatResponse(players, spectators []string) string {
    22  	playerPart := joinNames(players, "racing")
    23  	spectatorPart := joinNames(spectators, "watching")
    24  
    25  	if playerPart == "" && spectatorPart == "" {
    26  		return "Nobody is playing."
    27  	}
    28  
    29  	response := playerPart + " " + spectatorPart
    30  	response = bunString.Escape(response, '\\', '\\')
    31  	response = bunString.Escape(response, '*', '\\')
    32  	response = bunString.Escape(response, '_', '\\')
    33  	response = bunString.Escape(response, '|', '\\')
    34  	response = bunString.Escape(response, ':', '\\')
    35  	return response
    36  }
    37  

View as plain text