From a672374b532f4d5f4c5d550c596219a30a7d409b Mon Sep 17 00:00:00 2001 From: pixelrazor Date: Thu, 24 Feb 2022 11:56:48 -0700 Subject: [PATCH 1/6] Add Invite Create/Delete --- eventhandlers.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ events.go | 14 ++++++++++++++ structs.go | 45 +++++++++++++++++++++++---------------------- 3 files changed, 85 insertions(+), 22 deletions(-) diff --git a/eventhandlers.go b/eventhandlers.go index 29565a7a0..63b7304b6 100644 --- a/eventhandlers.go +++ b/eventhandlers.go @@ -29,6 +29,8 @@ const ( guildRoleUpdateEventType = "GUILD_ROLE_UPDATE" guildUpdateEventType = "GUILD_UPDATE" interactionCreateEventType = "INTERACTION_CREATE" + inviteCreateEventType = "INVITE_CREATE" + inviteDeleteEventType = "INVITE_DELETE" messageAckEventType = "MESSAGE_ACK" messageCreateEventType = "MESSAGE_CREATE" messageDeleteEventType = "MESSAGE_DELETE" @@ -485,6 +487,46 @@ func (eh interactionCreateEventHandler) Handle(s *Session, i interface{}) { } } +// inviteCreateEventHandler is an event handler for InviteCreate events. +type inviteCreateEventHandler func(*Session, *InviteCreate) + +// Type returns the event type for InviteCreate events. +func (eh inviteCreateEventHandler) Type() string { + return inviteCreateEventType +} + +// New returns a new instance of InviteCreate. +func (eh inviteCreateEventHandler) New() interface{} { + return &InviteCreate{} +} + +// Handle is the handler for InviteCreate events. +func (eh inviteCreateEventHandler) Handle(s *Session, i interface{}) { + if t, ok := i.(*InviteCreate); ok { + eh(s, t) + } +} + +// inviteDeleteEventHandler is an event handler for InviteDelete events. +type inviteDeleteEventHandler func(*Session, *InviteDelete) + +// Type returns the event type for InviteDelete events. +func (eh inviteDeleteEventHandler) Type() string { + return inviteDeleteEventType +} + +// New returns a new instance of InviteDelete. +func (eh inviteDeleteEventHandler) New() interface{} { + return &InviteDelete{} +} + +// Handle is the handler for InviteDelete events. +func (eh inviteDeleteEventHandler) Handle(s *Session, i interface{}) { + if t, ok := i.(*InviteDelete); ok { + eh(s, t) + } +} + // messageAckEventHandler is an event handler for MessageAck events. type messageAckEventHandler func(*Session, *MessageAck) @@ -1108,6 +1150,10 @@ func handlerForInterface(handler interface{}) EventHandler { return guildUpdateEventHandler(v) case func(*Session, *InteractionCreate): return interactionCreateEventHandler(v) + case func(*Session, *InviteCreate): + return inviteCreateEventHandler(v) + case func(*Session, *InviteDelete): + return inviteDeleteEventHandler(v) case func(*Session, *MessageAck): return messageAckEventHandler(v) case func(*Session, *MessageCreate): @@ -1191,6 +1237,8 @@ func init() { registerInterfaceProvider(guildRoleUpdateEventHandler(nil)) registerInterfaceProvider(guildUpdateEventHandler(nil)) registerInterfaceProvider(interactionCreateEventHandler(nil)) + registerInterfaceProvider(inviteCreateEventHandler(nil)) + registerInterfaceProvider(inviteDeleteEventHandler(nil)) registerInterfaceProvider(messageAckEventHandler(nil)) registerInterfaceProvider(messageCreateEventHandler(nil)) registerInterfaceProvider(messageDeleteEventHandler(nil)) diff --git a/events.go b/events.go index c20e90e1b..dd217d9e6 100644 --- a/events.go +++ b/events.go @@ -341,3 +341,17 @@ type InteractionCreate struct { func (i *InteractionCreate) UnmarshalJSON(b []byte) error { return json.Unmarshal(b, &i.Interaction) } + +// InviteCreate is the data for a InviteCreate event +type InviteCreate struct { + *Invite + ChannelID string `json:"channel_id"` + GuildID string `json:"guild_id"` +} + +// InviteDelete is the data for a InviteDelete event +type InviteDelete struct { + ChannelID string `json:"channel_id"` + GuildID string `json:"guild_id"` + Code string `json:"code"` +} diff --git a/structs.go b/structs.go index 15fb30c0e..fc5fdf503 100644 --- a/structs.go +++ b/structs.go @@ -191,36 +191,37 @@ type ICEServer struct { Credential string `json:"credential"` } +// InviteTargetType indicates the type of target of an invite +// https://discord.com/developers/docs/resources/invite#invite-object-invite-target-types +type InviteTargetType uint8 + +// Invite target types +const ( + InviteStream InviteTargetType = 1 + InviteEmbeddedAppliction InviteTargetType = 2 +) + // A Invite stores all data related to a specific Discord Guild or Channel invite. type Invite struct { - Guild *Guild `json:"guild"` - Channel *Channel `json:"channel"` - Inviter *User `json:"inviter"` - Code string `json:"code"` - CreatedAt time.Time `json:"created_at"` - MaxAge int `json:"max_age"` - Uses int `json:"uses"` - MaxUses int `json:"max_uses"` - Revoked bool `json:"revoked"` - Temporary bool `json:"temporary"` - Unique bool `json:"unique"` - TargetUser *User `json:"target_user"` - TargetUserType TargetUserType `json:"target_user_type"` + Guild *Guild `json:"guild"` + Channel *Channel `json:"channel"` + Inviter *User `json:"inviter"` + Code string `json:"code"` + CreatedAt time.Time `json:"created_at"` + MaxAge int `json:"max_age"` + Uses int `json:"uses"` + MaxUses int `json:"max_uses"` + Revoked bool `json:"revoked"` + Temporary bool `json:"temporary"` + Unique bool `json:"unique"` + TargetUser *User `json:"target_user"` + TargetUserType InviteTargetType `json:"target_type"` // will only be filled when using InviteWithCounts ApproximatePresenceCount int `json:"approximate_presence_count"` ApproximateMemberCount int `json:"approximate_member_count"` } -// TargetUserType is the type of the target user -// https://discord.com/developers/docs/resources/invite#invite-object-target-user-types -type TargetUserType int - -// Block contains known TargetUserType values -const ( - TargetUserTypeStream TargetUserType = 1 -) - // ChannelType is the type of a Channel type ChannelType int From b5fb836e7bad50abbcd72897cb924f17f12de1f1 Mon Sep 17 00:00:00 2001 From: pixelrazor Date: Thu, 24 Feb 2022 15:57:24 -0700 Subject: [PATCH 2/6] rename const --- structs.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/structs.go b/structs.go index fc5fdf503..0904c3bb3 100644 --- a/structs.go +++ b/structs.go @@ -197,8 +197,8 @@ type InviteTargetType uint8 // Invite target types const ( - InviteStream InviteTargetType = 1 - InviteEmbeddedAppliction InviteTargetType = 2 + InviteTargetStream InviteTargetType = 1 + InviteTargetEmbeddedAppliction InviteTargetType = 2 ) // A Invite stores all data related to a specific Discord Guild or Channel invite. From 0a7015c07cd8d88a156ea255f8dc20f3af8bb655 Mon Sep 17 00:00:00 2001 From: pixelrazor Date: Fri, 25 Feb 2022 12:43:42 -0700 Subject: [PATCH 3/6] Refactor Application --- message.go | 4 +++- oauth2.go | 31 ++++++------------------------- structs.go | 49 ++++++++++++++++++++++++++++++++++++------------- 3 files changed, 45 insertions(+), 39 deletions(-) diff --git a/message.go b/message.go index 2f2bffde2..23f4aa077 100644 --- a/message.go +++ b/message.go @@ -121,7 +121,7 @@ type Message struct { Activity *MessageActivity `json:"activity"` // Is sent with Rich Presence-related chat embeds - Application *MessageApplication `json:"application"` + Application *Application `json:"application"` // MessageReference contains reference data sent with crossposted or reply messages. // This does not contain the reference *to* this message; this is for when *this* message references another. @@ -433,7 +433,9 @@ const ( MessageActivityTypeJoinRequest MessageActivityType = 5 ) +// TODO: Remove this when compatibility is not required // MessageApplication is sent with Rich Presence-related chat embeds +// Deprecated: See type Application type MessageApplication struct { ID string `json:"id"` CoverImage string `json:"cover_image"` diff --git a/oauth2.go b/oauth2.go index 1d929f58f..2fa2d8d54 100644 --- a/oauth2.go +++ b/oauth2.go @@ -40,23 +40,6 @@ type Team struct { Members []*TeamMember `json:"members"` } -// An Application struct stores values for a Discord OAuth2 Application -type Application struct { - ID string `json:"id,omitempty"` - Name string `json:"name"` - Description string `json:"description,omitempty"` - Icon string `json:"icon,omitempty"` - Secret string `json:"secret,omitempty"` - RedirectURIs *[]string `json:"redirect_uris,omitempty"` - BotRequireCodeGrant bool `json:"bot_require_code_grant,omitempty"` - BotPublic bool `json:"bot_public,omitempty"` - RPCApplicationState int `json:"rpc_application_state,omitempty"` - Flags int `json:"flags,omitempty"` - Owner *User `json:"owner"` - Bot *User `json:"bot"` - Team *Team `json:"team"` -} - // Application returns an Application structure of a specific Application // appID : The ID of an Application func (s *Session) Application(appID string) (st *Application, err error) { @@ -88,10 +71,9 @@ func (s *Session) Applications() (st []*Application, err error) { func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) { data := struct { - Name string `json:"name"` - Description string `json:"description"` - RedirectURIs *[]string `json:"redirect_uris,omitempty"` - }{ap.Name, ap.Description, ap.RedirectURIs} + Name string `json:"name"` + Description string `json:"description"` + }{ap.Name, ap.Description} body, err := s.RequestWithBucketID("POST", EndpointOAuth2Applications, data, EndpointOAuth2Applications) if err != nil { @@ -107,10 +89,9 @@ func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) { data := struct { - Name string `json:"name"` - Description string `json:"description"` - RedirectURIs *[]string `json:"redirect_uris,omitempty"` - }{ap.Name, ap.Description, ap.RedirectURIs} + Name string `json:"name"` + Description string `json:"description"` + }{ap.Name, ap.Description} body, err := s.RequestWithBucketID("PUT", EndpointOAuth2Application(appID), data, EndpointOAuth2Application("")) if err != nil { diff --git a/structs.go b/structs.go index 0904c3bb3..19e711d41 100644 --- a/structs.go +++ b/structs.go @@ -128,6 +128,28 @@ type Session struct { wsMutex sync.Mutex } +// Application stores values for a Discord Application +type Application struct { + ID string `json:"id,omitempty"` + Name string `json:"name"` + Icon string `json:"icon,omitempty"` + Description string `json:"description,omitempty"` + RPCOrigins []string `json:"rpc_origins,omitempty"` + BotPublic bool `json:"bot_public,omitempty"` + BotRequireCodeGrant bool `json:"bot_require_code_grant,omitempty"` + TermsOfServiceURL string `json:"terms_of_service_url"` + PrivacyProxyURL string `json:"privacy_policy_url"` + Owner *User `json:"owner"` + Summary string `json:"summary"` + VerifyKey string `json:"verify_key"` + Team *Team `json:"team"` + GuildID string `json:"guild_id"` + PrimarySKUID string `json:"primary_sku_id"` + Slug string `json:"slug"` + CoverImage string `json:"cover_image"` + Flags int `json:"flags,omitempty"` +} + // UserConnection is a Connection returned from the UserConnections endpoint type UserConnection struct { ID string `json:"id"` @@ -203,19 +225,20 @@ const ( // A Invite stores all data related to a specific Discord Guild or Channel invite. type Invite struct { - Guild *Guild `json:"guild"` - Channel *Channel `json:"channel"` - Inviter *User `json:"inviter"` - Code string `json:"code"` - CreatedAt time.Time `json:"created_at"` - MaxAge int `json:"max_age"` - Uses int `json:"uses"` - MaxUses int `json:"max_uses"` - Revoked bool `json:"revoked"` - Temporary bool `json:"temporary"` - Unique bool `json:"unique"` - TargetUser *User `json:"target_user"` - TargetUserType InviteTargetType `json:"target_type"` + Guild *Guild `json:"guild"` + Channel *Channel `json:"channel"` + Inviter *User `json:"inviter"` + Code string `json:"code"` + CreatedAt time.Time `json:"created_at"` + MaxAge int `json:"max_age"` + Uses int `json:"uses"` + MaxUses int `json:"max_uses"` + Revoked bool `json:"revoked"` + Temporary bool `json:"temporary"` + Unique bool `json:"unique"` + TargetUser *User `json:"target_user"` + TargetType InviteTargetType `json:"target_type"` + TargetApplication *Application `json:"target_application"` // will only be filled when using InviteWithCounts ApproximatePresenceCount int `json:"approximate_presence_count"` From 484a2d64483fa5695c3ca4ac83ce931d5ff242d9 Mon Sep 17 00:00:00 2001 From: pixelrazor Date: Fri, 25 Feb 2022 12:56:24 -0700 Subject: [PATCH 4/6] Feedback & deprecation --- message.go | 6 +++--- oauth2.go | 14 ++++++++------ structs.go | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/message.go b/message.go index 23f4aa077..9d30ac28c 100644 --- a/message.go +++ b/message.go @@ -121,7 +121,7 @@ type Message struct { Activity *MessageActivity `json:"activity"` // Is sent with Rich Presence-related chat embeds - Application *Application `json:"application"` + Application *MessageApplication `json:"application"` // MessageReference contains reference data sent with crossposted or reply messages. // This does not contain the reference *to* this message; this is for when *this* message references another. @@ -433,9 +433,9 @@ const ( MessageActivityTypeJoinRequest MessageActivityType = 5 ) -// TODO: Remove this when compatibility is not required // MessageApplication is sent with Rich Presence-related chat embeds -// Deprecated: See type Application +// This is used instead of Application to reduce the amount of memory consumption +// of message objects type MessageApplication struct { ID string `json:"id"` CoverImage string `json:"cover_image"` diff --git a/oauth2.go b/oauth2.go index 2fa2d8d54..41bf1b72b 100644 --- a/oauth2.go +++ b/oauth2.go @@ -71,9 +71,10 @@ func (s *Session) Applications() (st []*Application, err error) { func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) { data := struct { - Name string `json:"name"` - Description string `json:"description"` - }{ap.Name, ap.Description} + Name string `json:"name"` + Description string `json:"description"` + RedirectURIs *[]string `json:"redirect_uris,omitempty"` + }{ap.Name, ap.Description, ap.RedirectURIs} body, err := s.RequestWithBucketID("POST", EndpointOAuth2Applications, data, EndpointOAuth2Applications) if err != nil { @@ -89,9 +90,10 @@ func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) { data := struct { - Name string `json:"name"` - Description string `json:"description"` - }{ap.Name, ap.Description} + Name string `json:"name"` + Description string `json:"description"` + RedirectURIs *[]string `json:"redirect_uris,omitempty"` + }{ap.Name, ap.Description, ap.RedirectURIs} body, err := s.RequestWithBucketID("PUT", EndpointOAuth2Application(appID), data, EndpointOAuth2Application("")) if err != nil { diff --git a/structs.go b/structs.go index 19e711d41..f06234d97 100644 --- a/structs.go +++ b/structs.go @@ -148,6 +148,10 @@ type Application struct { Slug string `json:"slug"` CoverImage string `json:"cover_image"` Flags int `json:"flags,omitempty"` + + // TODO: Remove this when compatibility is not required + // Deprecated + RedirectURIs *[]string `json:"-"` } // UserConnection is a Connection returned from the UserConnections endpoint @@ -245,6 +249,17 @@ type Invite struct { ApproximateMemberCount int `json:"approximate_member_count"` } +// TODO: Remove this when compatibility is not required +// TargetUserType is the type of the target user +// Deprecated: see InviteTargetType +type TargetUserType int + +// Block contains known TargetUserType values +// Deprecated: see InviteTargetType +const ( + TargetUserTypeStream TargetUserType = 1 +) + // ChannelType is the type of a Channel type ChannelType int From 7a69004558fd2f27c2738a65cb9de73049d25f5e Mon Sep 17 00:00:00 2001 From: pixelrazor Date: Fri, 25 Feb 2022 13:05:36 -0700 Subject: [PATCH 5/6] lint fix for godoc comment --- structs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/structs.go b/structs.go index f06234d97..3511a25f6 100644 --- a/structs.go +++ b/structs.go @@ -249,8 +249,8 @@ type Invite struct { ApproximateMemberCount int `json:"approximate_member_count"` } -// TODO: Remove this when compatibility is not required // TargetUserType is the type of the target user +// TODO: Remove this when compatibility is not required // Deprecated: see InviteTargetType type TargetUserType int From 9a5e62307deaed2c99d3b3750552bdb294c29c4a Mon Sep 17 00:00:00 2001 From: pixelrazor Date: Fri, 25 Feb 2022 14:06:31 -0700 Subject: [PATCH 6/6] review feedback --- message.go | 2 -- oauth2.go | 14 ++++++-------- structs.go | 15 --------------- 3 files changed, 6 insertions(+), 25 deletions(-) diff --git a/message.go b/message.go index 9d30ac28c..2f2bffde2 100644 --- a/message.go +++ b/message.go @@ -434,8 +434,6 @@ const ( ) // MessageApplication is sent with Rich Presence-related chat embeds -// This is used instead of Application to reduce the amount of memory consumption -// of message objects type MessageApplication struct { ID string `json:"id"` CoverImage string `json:"cover_image"` diff --git a/oauth2.go b/oauth2.go index 41bf1b72b..2fa2d8d54 100644 --- a/oauth2.go +++ b/oauth2.go @@ -71,10 +71,9 @@ func (s *Session) Applications() (st []*Application, err error) { func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) { data := struct { - Name string `json:"name"` - Description string `json:"description"` - RedirectURIs *[]string `json:"redirect_uris,omitempty"` - }{ap.Name, ap.Description, ap.RedirectURIs} + Name string `json:"name"` + Description string `json:"description"` + }{ap.Name, ap.Description} body, err := s.RequestWithBucketID("POST", EndpointOAuth2Applications, data, EndpointOAuth2Applications) if err != nil { @@ -90,10 +89,9 @@ func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) { data := struct { - Name string `json:"name"` - Description string `json:"description"` - RedirectURIs *[]string `json:"redirect_uris,omitempty"` - }{ap.Name, ap.Description, ap.RedirectURIs} + Name string `json:"name"` + Description string `json:"description"` + }{ap.Name, ap.Description} body, err := s.RequestWithBucketID("PUT", EndpointOAuth2Application(appID), data, EndpointOAuth2Application("")) if err != nil { diff --git a/structs.go b/structs.go index 3511a25f6..19e711d41 100644 --- a/structs.go +++ b/structs.go @@ -148,10 +148,6 @@ type Application struct { Slug string `json:"slug"` CoverImage string `json:"cover_image"` Flags int `json:"flags,omitempty"` - - // TODO: Remove this when compatibility is not required - // Deprecated - RedirectURIs *[]string `json:"-"` } // UserConnection is a Connection returned from the UserConnections endpoint @@ -249,17 +245,6 @@ type Invite struct { ApproximateMemberCount int `json:"approximate_member_count"` } -// TargetUserType is the type of the target user -// TODO: Remove this when compatibility is not required -// Deprecated: see InviteTargetType -type TargetUserType int - -// Block contains known TargetUserType values -// Deprecated: see InviteTargetType -const ( - TargetUserTypeStream TargetUserType = 1 -) - // ChannelType is the type of a Channel type ChannelType int