Skip to content

add nonce field to message struct #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading