option: Move conflicting types into package discord

This commit is contained in:
Maximilian von Lindern 2021-08-30 17:41:54 +02:00 committed by diamondburned
parent 2647267460
commit 53c72c1e16
5 changed files with 44 additions and 76 deletions

View File

@ -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"`

View File

@ -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"`

View File

@ -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"`

View File

@ -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

View File

@ -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
}