Skip to content

Commit

Permalink
add nonce field to message struct (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 authored Dec 28, 2024
1 parent 68e1953 commit 3bdb88c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions discord/message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package discord

import (
"bytes"
"fmt"
"strconv"
"time"

"github.com/disgoorg/json"
Expand Down Expand Up @@ -119,6 +121,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 +555,26 @@ 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 {
if bytes.Equal(b, []byte("null")) {
return nil
}

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 3bdb88c

Please sign in to comment.