Skip to content

Commit

Permalink
add nonce field to message struct
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Dec 18, 2024
1 parent e1313eb commit 858db16
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions discord/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package discord

import (
"fmt"
"strconv"
"time"

"github.com/disgoorg/json"
Expand Down Expand Up @@ -119,6 +120,7 @@ type Message struct {
Resolved *ResolvedData `json:"resolved,omitempty"`
Poll *Poll `json:"poll,omitempty"`
Call *MessageCall `json:"call,omitempty"`
Nonce Nonce `json:"nonce,omitempty"`
}

func (m *Message) UnmarshalJSON(data []byte) error {
Expand Down Expand Up @@ -552,3 +554,22 @@ func unmarshalComponents(components []UnmarshalComponent) []ContainerComponent {
}
return containerComponents
}

// Nonce is a string or int used when sending a message to discord.
type Nonce string

// UnmarshalJSON unmarshals the Nonce from a string or int.
func (n *Nonce) UnmarshalJSON(b []byte) error {
unquoted, err := strconv.Unquote(string(b))
if err != nil {
i, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
*n = Nonce(strconv.FormatInt(i, 10))
} else {
*n = Nonce(unquoted)
}

return nil
}

0 comments on commit 858db16

Please sign in to comment.