...

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

Documentation: go.formulabun.club/discord/slashplayers

     1  package slashplayers
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"time"
     9  
    10  	dContext "go.formulabun.club/discord/context"
    11  	"go.formulabun.club/discord/env"
    12  	"go.formulabun.club/functional/array"
    13  	translator "go.formulabun.club/translator/client"
    14  
    15  	"github.com/bwmarrin/discordgo"
    16  )
    17  
    18  var trClient *translator.APIClient
    19  var logger = log.New(os.Stdout, "/players | ", log.LstdFlags)
    20  var request translator.ApiPlayerinfoGetRequest
    21  
    22  var command = &discordgo.ApplicationCommand{
    23  	Name:        "players",
    24  	Description: "Get player info",
    25  }
    26  
    27  func Start(c *dContext.DiscordContext) {
    28  	config := translator.NewConfiguration()
    29  	trClient = translator.NewAPIClient(config)
    30  
    31  	logger.Println("Trying to get a connection to the translator service.")
    32  	request = trClient.DefaultApi.PlayerinfoGet(context.Background())
    33  	_, _, err := request.Execute()
    34  	for err != nil {
    35  		<-time.After(time.Second * 5)
    36  		_, _, err = request.Execute()
    37  		logger.Println(err)
    38  	}
    39  	logger.Println("Connection gotten, registering command.")
    40  
    41  	command, err = c.S.ApplicationCommandCreate(env.APPLICATIONID, env.TESTGUILD, command)
    42  	if err != nil {
    43  		logger.Fatal(err)
    44  	}
    45  
    46  	destroy := c.S.AddHandler(reply)
    47  
    48  	for _ = range c.Cancel {
    49  	}
    50  
    51  	logger.Println("Destroying command")
    52  	destroy()
    53  }
    54  func reply(s *discordgo.Session, interaction *discordgo.InteractionCreate) {
    55  	if interaction.Interaction.Type != discordgo.InteractionApplicationCommand {
    56  		return
    57  	}
    58  	if interaction.ApplicationCommandData().ID != command.ID {
    59  		return
    60  	}
    61  
    62  	logger.Println("Interaction created")
    63  
    64  	data, _, err := request.Execute()
    65  
    66  	if err != nil {
    67  		logger.Printf("request failed: %s\n", err)
    68  		respondError(s, interaction.Interaction)
    69  		return
    70  	}
    71  
    72  	players := array.Filter(data, func(p translator.PlayerInfoEntry) bool {
    73  		return *p.Team != 255
    74  	})
    75  	playerNames := array.Map(players, func(p translator.PlayerInfoEntry) string {
    76  		return p.GetName()
    77  	})
    78  	spectators := array.Filter(data, func(p translator.PlayerInfoEntry) bool {
    79  		return *p.Team == 255
    80  	})
    81  	spectatorNames := array.Map(spectators, func(p translator.PlayerInfoEntry) string {
    82  		return p.GetName()
    83  	})
    84  	response := formatResponse(playerNames, spectatorNames)
    85  
    86  	logger.Printf("Sending %s\n", response)
    87  	respondPlayers(s, interaction.Interaction, response)
    88  	logger.Println("Interaction Ending")
    89  }
    90  
    91  func respondError(s *discordgo.Session, interaction *discordgo.Interaction) {
    92  	s.InteractionRespond(interaction, &discordgo.InteractionResponse{
    93  		Type: discordgo.InteractionResponseChannelMessageWithSource,
    94  		Data: &discordgo.InteractionResponseData{
    95  			Content: "Could not contact the srb2kart server. Seek help!",
    96  			Flags:   discordgo.MessageFlagsEphemeral,
    97  		},
    98  	})
    99  }
   100  
   101  func respondPlayers(s *discordgo.Session, interaction *discordgo.Interaction, response string) {
   102  	s.InteractionRespond(interaction, &discordgo.InteractionResponse{
   103  		Type: discordgo.InteractionResponseChannelMessageWithSource,
   104  		Data: &discordgo.InteractionResponseData{
   105  			Content: fmt.Sprintf("%s", response),
   106  		},
   107  	})
   108  }
   109  

View as plain text