diff --git a/api/guild.go b/api/guild.go index d705aa9..8df7e0d 100644 --- a/api/guild.go +++ b/api/guild.go @@ -62,7 +62,7 @@ type CreateGuildData struct { // AFKChannelID is the id for the afk channel. AFKChannelID discord.ChannelID `json:"afk_channel_id,omitempty"` // AFKTimeout is the afk timeout in seconds. - AFKTimeout option.Seconds `json:"afk_timeout,omitempty"` + AFKTimeout discord.OptionalSeconds `json:"afk_timeout,omitempty"` // SystemChannelID is the id of the channel where guild notices such as // welcome messages and boost events are posted. @@ -268,7 +268,7 @@ type ModifyGuildData struct { // This field is nullable. AFKChannelID discord.ChannelID `json:"afk_channel_id,string,omitempty"` // AFKTimeout is the afk timeout in seconds. - AFKTimeout option.Seconds `json:"afk_timeout,omitempty"` + AFKTimeout discord.OptionalSeconds `json:"afk_timeout,omitempty"` // Icon is the base64 1024x1024 png/jpeg/gif image for the guild icon // (can be animated gif when the server has the ANIMATED_ICON feature). Icon *Image `json:"icon,omitempty"` diff --git a/api/role.go b/api/role.go index e63654c..4bb0a55 100644 --- a/api/role.go +++ b/api/role.go @@ -124,7 +124,9 @@ type ModifyRoleData struct { // Permissions is the bitwise value of the enabled/disabled permissions. Permissions *discord.Permissions `json:"permissions,string,omitempty"` // Permissions is the bitwise value of the enabled/disabled permissions. - Color option.NullableColor `json:"color,omitempty"` + // + // This value is nullable. + Color discord.Color `json:"color,omitempty"` // Hoist specifies whether the role should be displayed separately in the // sidebar. Hoist option.NullableBool `json:"hoist,omitempty"` diff --git a/discord/message_embed.go b/discord/message_embed.go index 63fefb6..ff49683 100644 --- a/discord/message_embed.go +++ b/discord/message_embed.go @@ -1,11 +1,16 @@ package discord -import "fmt" +import ( + "fmt" + "strconv" +) -type Color uint32 +type Color int32 var DefaultEmbedColor Color = 0x303030 +const NullColor Color = -1 + func (c Color) Uint32() uint32 { return uint32(c) } @@ -27,6 +32,26 @@ func (c Color) RGB() (uint8, uint8, uint8) { return r, g, b } +func (c Color) MarshalJSON() ([]byte, error) { + if c < 0 { + return []byte("null"), nil + } + return []byte(strconv.Itoa(c.Int())), nil +} + +func (c *Color) UnmarshalJSON(json []byte) error { + s := string(json) + + if s == "null" { + *c = NullColor + return nil + } + + v, err := strconv.ParseInt(s, 10, 32) + *c = Color(v) + return err +} + type Embed struct { Title string `json:"title,omitempty"` Type EmbedType `json:"type,omitempty"` diff --git a/discord/time.go b/discord/time.go index 8312d09..6758bc1 100644 --- a/discord/time.go +++ b/discord/time.go @@ -131,6 +131,18 @@ func (s Seconds) Duration() time.Duration { // +// OptionalSeconds is the option type for Seconds. +type OptionalSeconds = *Seconds + +// ZeroOptionalSeconds are 0 OptionalSeconds. +var ZeroOptionalSeconds = NewOptionalSeconds(0) + +// NewOptionalSeconds creates a new OptionalSeconds using the value of the +// passed Seconds. +func NewOptionalSeconds(s Seconds) OptionalSeconds { return &s } + +// + // Milliseconds is in float64 because some Discord events return time with a // trailing decimal. type Milliseconds float64 diff --git a/utils/json/option/custom.go b/utils/json/option/custom.go deleted file mode 100644 index 0698b41..0000000 --- a/utils/json/option/custom.go +++ /dev/null @@ -1,71 +0,0 @@ -package option - -import ( - "strconv" - - "github.com/diamondburned/arikawa/v3/discord" -) - -// ================================ Seconds ================================ - -// Seconds is the option type for discord.Seconds. -type Seconds = *discord.Seconds - -// ZeroSeconds are 0 Seconds. -var ZeroSeconds = NewSeconds(0) - -// NewString creates a new Seconds with the value of the passed discord.Seconds. -func NewSeconds(s discord.Seconds) Seconds { return &s } - -// ================================ Color ================================ - -// Color is the option type for discord.Color. -type Color = *discord.Color - -// NewString creates a new Color with the value of the passed discord.Color. -func NewColor(s discord.Color) Color { return &s } - -// ================================ NullableColor ================================ - -// Nullable is a nullable version of discord.Color. -type NullableColor = *NullableColorData - -type NullableColorData struct { - Val discord.Color - Init bool -} - -// NullColor serializes to JSON null. -var NullColor = &NullableColorData{} - -// NewNullableColor creates a new non-null NullableColor using the value of the -// passed discord.Color. -func NewNullableColor(v discord.Color) NullableColor { - return &NullableColorData{ - Val: v, - Init: true, - } -} - -func (i NullableColorData) MarshalJSON() ([]byte, error) { - if !i.Init { - return []byte("null"), nil - } - return []byte(strconv.FormatUint(uint64(i.Val), 10)), nil -} - -func (i *NullableColorData) UnmarshalJSON(json []byte) error { - s := string(json) - - if s == "null" { - *i = *NullColor - return nil - } - - v, err := strconv.ParseUint(s, 10, 32) - - i.Val = discord.Color(v) - i.Init = true - - return err -}