Skip to content

Commit

Permalink
feat: ResolveInteraction func
Browse files Browse the repository at this point in the history
  • Loading branch information
liy77 committed Jul 9, 2022
1 parent a47cdc8 commit 0efcf81
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 3 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func httpHandler(publicKey ed25519.PublicKey, token string) http.HandlerFunc {
panic("Error on get interaction: " + err.Error())
}

interaction = ResolveInteraction(&interaction)

if interaction.Type == PingInteraction {
w.Header().Set("Content-Type", "application/json")
err := je.Encode(InteractionResponse{
Expand Down Expand Up @@ -211,4 +213,4 @@ func (ctx *ConnectionContext) DeleteReply() {
ctx.Interaction.Token, "@original",
), fasthttp.MethodDelete, nil, ctx.clientToken,
)
}
}
47 changes: 46 additions & 1 deletion interactions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package httpcord

import (
"encoding/json"

"github.com/JustAWaifuHunter/httpcord/permissions"
)

Expand Down Expand Up @@ -263,7 +265,7 @@ func (i *Interaction) ModalSubmitData() ModalSubmitInteractionData {
}

func (i *Interaction) ApplicationCommandData() ApplicationCommandInteractionData {
if i.Type != ApplicationCommandInteraction {
if i.Type != ApplicationCommandInteraction && i.Type != AutoCompleteInteraction {
panic("The Interaction is not a ApplicationCommand")
}

Expand Down Expand Up @@ -663,3 +665,46 @@ func (c *ApplicationCommand) SetDescriptionLocalizations(DescriptionLocalization
c.DescriptionLocalizations = DescriptionLocalizations
return c
}

func ResolveInteraction(interaction *Interaction) Interaction {
marshaledData, err := json.Marshal(interaction.Data)

if err != nil {
panic(err)
}

switch interaction.Type {
case MessageComponentInteraction:
{
var data ComponentInteractionData
err = json.Unmarshal(marshaledData, &data)
if err != nil {
panic(err)
}

interaction.Data = data
}
case ModalSubmitInteraction:
{
var data ModalSubmitInteractionData
err = json.Unmarshal(marshaledData, &data)
if err != nil {
panic(err)
}

interaction.Data = data
}
case ApplicationCommandInteraction:
{
var data ApplicationCommandInteractionData
err = json.Unmarshal(marshaledData, &data)
if err != nil {
panic(err)
}

interaction.Data = data
}
}

return *interaction
}

0 comments on commit 0efcf81

Please sign in to comment.