...

Source file src/go.formulabun.club/discord/slashrecords/download/download.go

Documentation: go.formulabun.club/discord/slashrecords/download

     1  package download
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"path"
     9  
    10  	"github.com/bwmarrin/discordgo"
    11  	"github.com/bwmarrin/snowflake"
    12  	"go.formulabun.club/discord/context"
    13  	"go.formulabun.club/replays/store"
    14  	"go.formulabun.club/srb2kart/conversion"
    15  )
    16  
    17  var logger = log.New(os.Stdout, "download button ", log.LstdFlags)
    18  
    19  func ReplyFunction(c *context.DiscordContext) func(*discordgo.Session, *discordgo.InteractionCreate) {
    20  	return func(s *discordgo.Session, i *discordgo.InteractionCreate) {
    21  		if i.Type != discordgo.InteractionMessageComponent {
    22  			return
    23  		}
    24  		reply(c, s, i)
    25  	}
    26  }
    27  
    28  func reply(c *context.DiscordContext, s *discordgo.Session, interaction *discordgo.InteractionCreate) {
    29  	if interaction.Type != discordgo.InteractionMessageComponent {
    30  		return
    31  	}
    32  	interactionData := interaction.MessageComponentData()
    33  	id, err := snowflake.ParseString(interactionData.CustomID)
    34  	if err != nil {
    35  		logger.Print("bad id")
    36  		return
    37  	}
    38  	data, ok := pendingDownloads[id]
    39  	if !ok {
    40  		response := badButton()
    41  		s.InteractionRespond(interaction.Interaction, &response)
    42  		return
    43  	}
    44  
    45  	replays, err := getReplays(c, data)
    46  	if err != nil {
    47  		response := errorResponse(err)
    48  		s.InteractionRespond(interaction.Interaction, &response)
    49  		return
    50  	}
    51  	response, err := makeResponse(replays)
    52  	if err != nil {
    53  		response := errorResponse(err)
    54  		s.InteractionRespond(interaction.Interaction, &response)
    55  		return
    56  	}
    57  
    58  	s.InteractionRespond(interaction.Interaction, &response)
    59  }
    60  
    61  func getReplays(context *context.DiscordContext, replay store.Replay) ([]store.Replay, error) {
    62  	return context.ReplayDB.FindReplay(replay)
    63  }
    64  
    65  func makeResponse(replays []store.Replay) (response discordgo.InteractionResponse, err error) {
    66  	if len(replays) == 0 {
    67  		err = errors.New("No replays to create a response with")
    68  		return
    69  	}
    70  	filename := path.Join("/data", fmt.Sprintf("%v", replays[0].ReplayID))
    71  	file, err := os.Open(filename)
    72  	if err != nil {
    73  		return
    74  	}
    75  	mapID, _ := conversion.NumberToMapId(uint(replays[0].GameMap))
    76  	dFile := discordgo.File{
    77  		Name:        fmt.Sprintf("MAP%s-guest.lmp", mapID),
    78  		ContentType: "application/*",
    79  		Reader:      file,
    80  	}
    81  
    82  	response = discordgo.InteractionResponse{
    83  		Type: discordgo.InteractionResponseChannelMessageWithSource,
    84  		Data: &discordgo.InteractionResponseData{
    85  			Files: []*discordgo.File{&dFile},
    86  		},
    87  	}
    88  	return
    89  }
    90  
    91  func badButton() discordgo.InteractionResponse {
    92  	return discordgo.InteractionResponse{
    93  		Type: discordgo.InteractionResponseChannelMessageWithSource,
    94  		Data: &discordgo.InteractionResponseData{
    95  			Content: "It seems this button is no longer valid.",
    96  		},
    97  	}
    98  }
    99  
   100  func errorResponse(err error) discordgo.InteractionResponse {
   101  	logger.Print(err)
   102  	return discordgo.InteractionResponse{
   103  		Type: discordgo.InteractionResponseChannelMessageWithSource,
   104  		Data: &discordgo.InteractionResponseData{
   105  			Content: fmt.Sprintf("Something went wrong. ||%s||", err),
   106  		},
   107  	}
   108  }
   109  

View as plain text