From d09e8cd7a9318a2f05150660db15b7b4bc95a681 Mon Sep 17 00:00:00 2001 From: topi314 Date: Sun, 16 Jun 2024 17:48:01 +0200 Subject: [PATCH] add resolve command & update dependencies --- commands/commands.go | 1 + commands/resolve.go | 87 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 +-- go.sum | 10 ++--- main.go | 1 + 5 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 commands/resolve.go diff --git a/commands/commands.go b/commands/commands.go index cbed9db..371d2be 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -7,6 +7,7 @@ import ( var CommandCreates = []discord.ApplicationCommandCreate{ decode, + resolve, info, latest, music, diff --git a/commands/resolve.go b/commands/resolve.go new file mode 100644 index 0000000..c74dbe3 --- /dev/null +++ b/commands/resolve.go @@ -0,0 +1,87 @@ +package commands + +import ( + "bytes" + "context" + "fmt" + "time" + + "github.com/disgoorg/disgo/discord" + "github.com/disgoorg/disgo/handler" + "github.com/disgoorg/disgolink/v3/lavalink" + "github.com/disgoorg/json" +) + +var resolve = discord.SlashCommandCreate{ + Name: "resolve", + Description: "Resolve an identifier to it's result", + Options: []discord.ApplicationCommandOption{ + discord.ApplicationCommandOptionString{ + Name: "identifier", + Description: "The identifier to resolve", + Required: true, + }, + }, +} + +func (c *Commands) Resolve(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error { + identifier := data.String("identifier") + + if err := e.DeferCreateMessage(false); err != nil { + return err + } + + ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second) + defer cancel() + result, err := c.Lavalink.BestNode().Rest().LoadTracks(ctx, identifier) + if err != nil { + _, err = e.UpdateInteractionResponse(discord.MessageUpdate{ + Content: json.Ptr(fmt.Sprintf("failed to resolve identifier: %s", err)), + }) + return err + } + + var ( + content string + files []*discord.File + ) + switch result.LoadType { + case lavalink.LoadTypeTrack, lavalink.LoadTypePlaylist, lavalink.LoadTypeSearch: + decodedData, err := json.MarshalIndent(result.Data, "", " ") + if err != nil { + _, err = e.UpdateInteractionResponse(discord.MessageUpdate{ + Content: json.Ptr(fmt.Sprintf("failed to resolve identifier: %s", err)), + }) + return err + } + + if len(decodedData) > 2000 { + files = append(files, &discord.File{ + Name: "result.json", + Reader: bytes.NewReader(decodedData), + }) + content = "result is too long, see attached file" + } else { + content = fmt.Sprintf("```json\n%s\n```", decodedData) + } + content = fmt.Sprintf("LoadType: `%s`\n%s", result.LoadType, content) + + case lavalink.LoadTypeEmpty: + content = "LoadType: `empty`" + case lavalink.LoadTypeError: + ex, _ := result.Data.(lavalink.Exception) + if ex.Cause != nil { + files = append(files, &discord.File{ + Name: "cause.txt", + Reader: bytes.NewReader([]byte(*ex.Cause)), + }) + } + content = fmt.Sprintf("LoadType: `error`\nMessage: %s\nSeverity: %s", ex.Message, ex.Severity) + } + + _, err = e.UpdateInteractionResponse(discord.MessageUpdate{ + Content: &content, + Files: files, + }) + return err +} diff --git a/go.mod b/go.mod index 8d8ca0b..245d7b6 100644 --- a/go.mod +++ b/go.mod @@ -21,14 +21,13 @@ require ( require ( github.com/ProtonMail/go-crypto v1.0.0 // indirect - github.com/cloudflare/circl v1.3.8 // indirect + github.com/cloudflare/circl v1.3.9 // indirect github.com/google/go-querystring v1.1.0 // indirect - github.com/gorilla/websocket v1.5.1 // indirect + github.com/gorilla/websocket v1.5.3 // indirect github.com/magefile/mage v1.15.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad // indirect golang.org/x/crypto v0.24.0 // indirect - golang.org/x/net v0.26.0 // indirect golang.org/x/oauth2 v0.21.0 // indirect golang.org/x/sys v0.21.0 // indirect golang.org/x/text v0.16.0 // indirect diff --git a/go.sum b/go.sum index fab496f..a6441ab 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0k github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= -github.com/cloudflare/circl v1.3.8 h1:j+V8jJt09PoeMFIu2uh5JUyEaIHTXVOHslFoLNAKqwI= -github.com/cloudflare/circl v1.3.8/go.mod h1:PDRU+oXvdD7KCtgKxW95M5Z8BpSCJXQORiZFnBQS5QU= +github.com/cloudflare/circl v1.3.9 h1:QFrlgFYf2Qpi8bSpVPK1HBvWpx16v/1TZivyo7pGuBE= +github.com/cloudflare/circl v1.3.9/go.mod h1:PDRU+oXvdD7KCtgKxW95M5Z8BpSCJXQORiZFnBQS5QU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -30,8 +30,8 @@ github.com/google/go-github/v52 v52.0.0 h1:uyGWOY+jMQ8GVGSX8dkSwCzlehU3WfdxQ7Gwe github.com/google/go-github/v52 v52.0.0/go.mod h1:WJV6VEEUPuMo5pXqqa2ZCZEdbQqua4zAk2MZTIo+m+4= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= -github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= -github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4= github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4= github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= @@ -69,8 +69,6 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= -golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/main.go b/main.go index 33f3da8..9854e51 100644 --- a/main.go +++ b/main.go @@ -60,6 +60,7 @@ func main() { r.SlashCommand("/latest", cmds.Latest) r.Autocomplete("/latest", cmds.LatestAutocomplete) r.SlashCommand("/decode", cmds.Decode) + r.SlashCommand("/resolve", cmds.Resolve) r.Route("/music", func(r handler.Router) { r.SlashCommand("/play", cmds.Play)