Discord: Rename Snowflake and Timestamp Valid methods (#128)

* Discord: rename Snowflake.Valid() to IsValid()

* Discord: rename Timestamp.Valid() to IsValid()
This commit is contained in:
Maximilian von Lindern 2020-07-29 22:10:17 +02:00 committed by diamondburned
parent e1d9685cdb
commit 78c36f13cd
12 changed files with 39 additions and 39 deletions

View File

@ -86,7 +86,7 @@ func (ctx *Context) callCmd(ev interface{}) (bottomError error) {
msc = &gateway.MessageCreateEvent{Message: *m, Member: up.Member}
// Fill up member, if available.
if m.GuildID.Valid() && up.Member == nil {
if m.GuildID.IsValid() && up.Member == nil {
if mem, err := ctx.Store.Member(m.GuildID, m.Author.ID); err == nil {
msc.Member = mem
}

View File

@ -64,7 +64,7 @@ func reflectID(v reflect.Value, thing string) discord.Snowflake {
switch fType.Kind() {
case reflect.Struct:
if chID := reflectID(v.Field(i), thing); chID.Valid() {
if chID := reflectID(v.Field(i), thing); chID.IsValid() {
return chID
}
case reflect.Int64:
@ -175,7 +175,7 @@ func applyInstructions(v reflect.Value, instructions []step) discord.Snowflake {
if ins.ptr {
value = value.Elem()
}
if id := applyInstructions(value, instructions); id.Valid() {
if id := applyInstructions(value, instructions); id.IsValid() {
return id
}
}
@ -238,7 +238,7 @@ func (r *reflector) _id(v reflect.Value, t reflect.Type) (chID discord.Snowflake
switch fType.Kind() {
case reflect.Struct:
if chID = r._id(value, fType); chID.Valid() {
if chID = r._id(value, fType); chID.IsValid() {
ins.field = i
ins.ptr = ptr
}

View File

@ -9,12 +9,12 @@ import (
func AdminOnly(ctx *bot.Context) func(interface{}) error {
return func(ev interface{}) error {
var channelID = infer.ChannelID(ev)
if !channelID.Valid() {
if !channelID.IsValid() {
return bot.Break
}
var userID = infer.UserID(ev)
if !userID.Valid() {
if !userID.IsValid() {
return bot.Break
}
@ -30,17 +30,17 @@ func AdminOnly(ctx *bot.Context) func(interface{}) error {
func GuildOnly(ctx *bot.Context) func(interface{}) error {
return func(ev interface{}) error {
// Try and infer the GuildID.
if guildID := infer.GuildID(ev); guildID.Valid() {
if guildID := infer.GuildID(ev); guildID.IsValid() {
return nil
}
var channelID = infer.ChannelID(ev)
if !channelID.Valid() {
if !channelID.IsValid() {
return bot.Break
}
c, err := ctx.Channel(channelID)
if err != nil || !c.GuildID.Valid() {
if err != nil || !c.GuildID.IsValid() {
return bot.Break
}

View File

@ -46,7 +46,7 @@ func (e Emoji) EmojiURLWithType(t ImageType) string {
// APIString returns a string usable for sending over to the API.
func (e Emoji) APIString() string {
if !e.ID.Valid() {
if !e.ID.IsValid() {
return e.Name // is unicode
}

View File

@ -58,7 +58,7 @@ type Message struct {
// have a GuildID, it will generate a URL with the guild "@me".
func (m Message) URL() string {
var guildID = "@me"
if m.GuildID.Valid() {
if m.GuildID.IsValid() {
guildID = m.GuildID.String()
}

View File

@ -52,7 +52,7 @@ func (s *Snowflake) UnmarshalJSON(v []byte) error {
func (s Snowflake) MarshalJSON() ([]byte, error) {
// This includes 0 and null, because MarshalJSON does not dictate when a
// value gets omitted.
if !s.Valid() {
if !s.IsValid() {
return []byte("null"), nil
} else {
return []byte(`"` + strconv.FormatInt(int64(s), 10) + `"`), nil
@ -62,14 +62,14 @@ func (s Snowflake) MarshalJSON() ([]byte, error) {
// String returns the ID, or nothing if the snowflake isn't valid.
func (s Snowflake) String() string {
// Check if negative.
if !s.Valid() {
if !s.IsValid() {
return ""
}
return strconv.FormatUint(uint64(s), 10)
}
// Valid returns whether or not the snowflake is valid.
func (s Snowflake) Valid() bool {
// IsValid returns whether or not the snowflake is valid.
func (s Snowflake) IsValid() bool {
return int64(s) > 0
}
@ -100,7 +100,7 @@ type AppID Snowflake
func (s AppID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *AppID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s AppID) String() string { return Snowflake(s).String() }
func (s AppID) Valid() bool { return Snowflake(s).Valid() }
func (s AppID) IsValid() bool { return Snowflake(s).IsValid() }
func (s AppID) IsNull() bool { return Snowflake(s).IsNull() }
func (s AppID) Time() time.Time { return Snowflake(s).Time() }
func (s AppID) Worker() uint8 { return Snowflake(s).Worker() }
@ -112,7 +112,7 @@ type AttachmentID Snowflake
func (s AttachmentID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *AttachmentID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s AttachmentID) String() string { return Snowflake(s).String() }
func (s AttachmentID) Valid() bool { return Snowflake(s).Valid() }
func (s AttachmentID) IsValid() bool { return Snowflake(s).IsValid() }
func (s AttachmentID) IsNull() bool { return Snowflake(s).IsNull() }
func (s AttachmentID) Time() time.Time { return Snowflake(s).Time() }
func (s AttachmentID) Worker() uint8 { return Snowflake(s).Worker() }
@ -124,7 +124,7 @@ type AuditLogEntryID Snowflake
func (s AuditLogEntryID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *AuditLogEntryID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s AuditLogEntryID) String() string { return Snowflake(s).String() }
func (s AuditLogEntryID) Valid() bool { return Snowflake(s).Valid() }
func (s AuditLogEntryID) IsValid() bool { return Snowflake(s).IsValid() }
func (s AuditLogEntryID) IsNull() bool { return Snowflake(s).IsNull() }
func (s AuditLogEntryID) Time() time.Time { return Snowflake(s).Time() }
func (s AuditLogEntryID) Worker() uint8 { return Snowflake(s).Worker() }
@ -136,7 +136,7 @@ type ChannelID Snowflake
func (s ChannelID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *ChannelID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s ChannelID) String() string { return Snowflake(s).String() }
func (s ChannelID) Valid() bool { return Snowflake(s).Valid() }
func (s ChannelID) IsValid() bool { return Snowflake(s).IsValid() }
func (s ChannelID) IsNull() bool { return Snowflake(s).IsNull() }
func (s ChannelID) Time() time.Time { return Snowflake(s).Time() }
func (s ChannelID) Worker() uint8 { return Snowflake(s).Worker() }
@ -148,7 +148,7 @@ type EmojiID Snowflake
func (s EmojiID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *EmojiID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s EmojiID) String() string { return Snowflake(s).String() }
func (s EmojiID) Valid() bool { return Snowflake(s).Valid() }
func (s EmojiID) IsValid() bool { return Snowflake(s).IsValid() }
func (s EmojiID) IsNull() bool { return Snowflake(s).IsNull() }
func (s EmojiID) Time() time.Time { return Snowflake(s).Time() }
func (s EmojiID) Worker() uint8 { return Snowflake(s).Worker() }
@ -160,7 +160,7 @@ type IntegrationID Snowflake
func (s IntegrationID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *IntegrationID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s IntegrationID) String() string { return Snowflake(s).String() }
func (s IntegrationID) Valid() bool { return Snowflake(s).Valid() }
func (s IntegrationID) IsValid() bool { return Snowflake(s).IsValid() }
func (s IntegrationID) IsNull() bool { return Snowflake(s).IsNull() }
func (s IntegrationID) Time() time.Time { return Snowflake(s).Time() }
func (s IntegrationID) Worker() uint8 { return Snowflake(s).Worker() }
@ -172,7 +172,7 @@ type GuildID Snowflake
func (s GuildID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *GuildID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s GuildID) String() string { return Snowflake(s).String() }
func (s GuildID) Valid() bool { return Snowflake(s).Valid() }
func (s GuildID) IsValid() bool { return Snowflake(s).IsValid() }
func (s GuildID) IsNull() bool { return Snowflake(s).IsNull() }
func (s GuildID) Time() time.Time { return Snowflake(s).Time() }
func (s GuildID) Worker() uint8 { return Snowflake(s).Worker() }
@ -184,7 +184,7 @@ type MessageID Snowflake
func (s MessageID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *MessageID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s MessageID) String() string { return Snowflake(s).String() }
func (s MessageID) Valid() bool { return Snowflake(s).Valid() }
func (s MessageID) IsValid() bool { return Snowflake(s).IsValid() }
func (s MessageID) IsNull() bool { return Snowflake(s).IsNull() }
func (s MessageID) Time() time.Time { return Snowflake(s).Time() }
func (s MessageID) Worker() uint8 { return Snowflake(s).Worker() }
@ -196,7 +196,7 @@ type RoleID Snowflake
func (s RoleID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *RoleID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s RoleID) String() string { return Snowflake(s).String() }
func (s RoleID) Valid() bool { return Snowflake(s).Valid() }
func (s RoleID) IsValid() bool { return Snowflake(s).IsValid() }
func (s RoleID) IsNull() bool { return Snowflake(s).IsNull() }
func (s RoleID) Time() time.Time { return Snowflake(s).Time() }
func (s RoleID) Worker() uint8 { return Snowflake(s).Worker() }
@ -208,7 +208,7 @@ type UserID Snowflake
func (s UserID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *UserID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s UserID) String() string { return Snowflake(s).String() }
func (s UserID) Valid() bool { return Snowflake(s).Valid() }
func (s UserID) IsValid() bool { return Snowflake(s).IsValid() }
func (s UserID) IsNull() bool { return Snowflake(s).IsNull() }
func (s UserID) Time() time.Time { return Snowflake(s).Time() }
func (s UserID) Worker() uint8 { return Snowflake(s).Worker() }
@ -220,7 +220,7 @@ type WebhookID Snowflake
func (s WebhookID) MarshalJSON() ([]byte, error) { return Snowflake(s).MarshalJSON() }
func (s *WebhookID) UnmarshalJSON(v []byte) error { return (*Snowflake)(s).UnmarshalJSON(v) }
func (s WebhookID) String() string { return Snowflake(s).String() }
func (s WebhookID) Valid() bool { return Snowflake(s).Valid() }
func (s WebhookID) IsValid() bool { return Snowflake(s).IsValid() }
func (s WebhookID) IsNull() bool { return Snowflake(s).IsNull() }
func (s WebhookID) Time() time.Time { return Snowflake(s).Time() }
func (s WebhookID) Worker() uint8 { return Snowflake(s).Worker() }

View File

@ -7,7 +7,7 @@ import (
"time"
)
// Timestamp has a valid zero-value, which can be checked using the Valid()
// Timestamp has a valid zero-value, which can be checked using the IsValid()
// method. This is useful for optional timestamps such as EditedTimestamp.
type Timestamp time.Time
@ -45,14 +45,14 @@ func (t *Timestamp) UnmarshalJSON(v []byte) error {
// MarshalJSON returns null if Timestamp is not valid (zero). It returns the
// time formatted in RFC3339 otherwise.
func (t Timestamp) MarshalJSON() ([]byte, error) {
if !t.Valid() {
if !t.IsValid() {
return []byte("null"), nil
}
return []byte(`"` + t.Format(TimestampFormat) + `"`), nil
}
func (t Timestamp) Valid() bool {
func (t Timestamp) IsValid() bool {
return !t.Time().IsZero()
}

View File

@ -150,7 +150,7 @@ func (s *State) WithContext(ctx context.Context) *State {
//// Helper methods
func (s *State) AuthorDisplayName(message *gateway.MessageCreateEvent) string {
if !message.GuildID.Valid() {
if !message.GuildID.IsValid() {
return message.Author.Username
}
@ -183,7 +183,7 @@ func (s *State) MemberDisplayName(guildID discord.GuildID, userID discord.UserID
}
func (s *State) AuthorColor(message *gateway.MessageCreateEvent) (discord.Color, error) {
if !message.GuildID.Valid() { // this is a dm
if !message.GuildID.IsValid() { // this is a dm
return discord.DefaultMemberColor, nil
}
@ -593,7 +593,7 @@ func (s *State) Presence(
}
// If there's no guild ID, look in all guilds
if !guildID.Valid() {
if !guildID.IsValid() {
g, err := s.Guilds()
if err != nil {
return nil, err

View File

@ -103,7 +103,7 @@ func DiffMessage(src discord.Message, dst *discord.Message) {
if src.Content != "" {
dst.Content = src.Content
}
if src.EditedTimestamp.Valid() {
if src.EditedTimestamp.IsValid() {
dst.EditedTimestamp = src.EditedTimestamp
}
if src.Mentions != nil {
@ -115,10 +115,10 @@ func DiffMessage(src discord.Message, dst *discord.Message) {
if src.Attachments != nil {
dst.Attachments = src.Attachments
}
if src.Timestamp.Valid() {
if src.Timestamp.IsValid() {
dst.Timestamp = src.Timestamp
}
if src.Author.ID.Valid() {
if src.Author.ID.IsValid() {
dst.Author = src.Author
}
if src.Reactions != nil {

View File

@ -76,7 +76,7 @@ func (s *DefaultStore) Me() (*discord.User, error) {
s.mut.RLock()
defer s.mut.RUnlock()
if !s.self.ID.Valid() {
if !s.self.ID.IsValid() {
return nil, ErrStoreNotFound
}
@ -161,7 +161,7 @@ func (s *DefaultStore) ChannelSet(channel discord.Channel) error {
s.mut.Lock()
defer s.mut.Unlock()
if !channel.GuildID.Valid() {
if !channel.GuildID.IsValid() {
s.privates[channel.ID] = channel
} else {

View File

@ -138,7 +138,7 @@ func (s *Session) JoinChannelCtx(ctx context.Context, gID discord.GuildID, cID d
// Ensure that if `cID` is zero that it passes null to the update event.
var channelID discord.ChannelID = -1
if cID.Valid() {
if cID.IsValid() {
channelID = cID
}

View File

@ -153,7 +153,7 @@ func (c *Gateway) ResumeCtx(ctx context.Context) error {
sessionID := c.state.SessionID
token := c.state.Token
if !guildID.Valid() || sessionID == "" || token == "" {
if !guildID.IsValid() || sessionID == "" || token == "" {
return ErrMissingForResume
}