...
1 package slashrecords
2
3 import (
4 "context"
5 "log"
6 "os"
7 "time"
8
9 "github.com/bwmarrin/discordgo"
10
11 dContext "go.formulabun.club/discord/context"
12 ingest "go.formulabun.club/replays/ingest/client"
13
14 "go.formulabun.club/discord/env"
15 "go.formulabun.club/discord/slashrecords/download"
16 "go.formulabun.club/discord/slashrecords/search"
17 "go.formulabun.club/discord/slashrecords/upload"
18 )
19
20 var ingestClient *ingest.APIClient
21 var request ingest.ApiRootPostRequest
22 var logger = log.New(os.Stdout, "/records ", log.LstdFlags)
23
24 var command = &discordgo.ApplicationCommand{
25 Name: "records",
26 Description: "add, search and get time attack records",
27 Options: []*discordgo.ApplicationCommandOption{search.CommandOption, upload.CommandOption},
28 }
29
30 var localContext *dContext.DiscordContext
31
32 func Start(c *dContext.DiscordContext) {
33 config := ingest.NewConfiguration()
34 ingestClient = ingest.NewAPIClient(config)
35 localContext = c
36 request = ingestClient.DefaultApi.RootPost(context.Background())
37
38 logger.Println("Trying to get a connection to the replays ingest service.")
39 get := ingestClient.DefaultApi.ListGet(context.Background())
40 _, _, err := get.Execute()
41 for err != nil {
42 <-time.After(time.Second * 5)
43 _, _, err = get.Execute()
44 logger.Println(err)
45 }
46 logger.Println("Connection gotten, registering command.")
47
48 command, err = c.S.ApplicationCommandCreate(env.APPLICATIONID, env.TESTGUILD, command)
49 if err != nil {
50 logger.Fatal("Could not create command: ", err)
51 }
52
53 destroySlash := c.S.AddHandler(reply)
54 destroyButton := c.S.AddHandler(download.ReplyFunction(c))
55
56 logger.Println("I'm ready")
57 for _ = range c.Cancel {
58 }
59
60 logger.Println("Destroying handlers")
61 destroySlash()
62 destroyButton()
63 }
64
65 func reply(s *discordgo.Session, interaction *discordgo.InteractionCreate) {
66 if interaction.Type != discordgo.InteractionApplicationCommand {
67 return
68 }
69 if interaction.ApplicationCommandData().ID != command.ID {
70 return
71 }
72
73 logger.Println("interaction received")
74 data := interaction.ApplicationCommandData()
75 option := data.Options[0]
76 subcommand := option.Name
77
78 var response discordgo.InteractionResponse
79 if subcommand == search.CommandOption.Name {
80 response = search.Reply(localContext, option.Options)
81 } else if subcommand == upload.CommandOption.Name {
82 response = upload.Reply(option.Options, data.Resolved, request)
83 }
84
85 logger.Println("Sending response")
86 err := s.InteractionRespond(interaction.Interaction, &response)
87 if err != nil {
88 logger.Print(err)
89 }
90 logger.Println("Interaction ended")
91 }
92
View as plain text