Skip to content

Commit

Permalink
Merge branch 'master' into refactor/oauth-params
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian authored Apr 7, 2024
2 parents 3d72aa0 + 7039b07 commit a65b3b4
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 45 deletions.
4 changes: 2 additions & 2 deletions _examples/handler/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func main() {
r.Use(middleware.Print("group2"))
r.Command("/ping", handlePing)
r.Command("/ping2", handleContent("pong2"))
r.Component("button1/{data}", handleComponent)
r.Component("/button1/{data}", handleComponent)
})
r.NotFound(handleNotFound)

Expand Down Expand Up @@ -126,7 +126,7 @@ func handlePing(event *handler.CommandEvent) error {
Content: "pong",
Components: []discord.ContainerComponent{
discord.ActionRowComponent{
discord.NewPrimaryButton("button1", "button1/testData"),
discord.NewPrimaryButton("button1", "/button1/testData"),
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion discord/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,5 @@ const (
type ApplicationIntegrationTypesConfig map[ApplicationIntegrationType]ApplicationIntegrationTypeConfiguration

type ApplicationIntegrationTypeConfiguration struct {
OAuth2InstallParams InstallParams `json:"oauth2_install_params"`
OAuth2InstallParams *InstallParams `json:"oauth2_install_params"`
}
14 changes: 14 additions & 0 deletions discord/ban.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package discord

import "github.com/disgoorg/snowflake/v2"

// Ban represents a banned User from a Guild (https://discord.com/developers/docs/resources/guild#ban-object)
type Ban struct {
Reason *string `json:"reason,omitempty"`
Expand All @@ -10,3 +12,15 @@ type Ban struct {
type AddBan struct {
DeleteMessageSeconds int `json:"delete_message_seconds,omitempty"`
}

// BulkBan is used to bulk ban Users
type BulkBan struct {
UserIDs []snowflake.ID `json:"user_ids"`
DeleteMessageSeconds int `json:"delete_message_seconds,omitempty"`
}

// BulkBanResult is the result of a BulkBan request
type BulkBanResult struct {
BannedUsers []snowflake.ID `json:"banned_users"`
FailedUsers []snowflake.ID `json:"failed_users"`
}
2 changes: 1 addition & 1 deletion discord/cdn_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (

MemberAvatar = NewCDN("/guilds/{guild.id}/users/{user.id}/avatars/{member.avatar.hash}", FileFormatPNG, FileFormatJPEG, FileFormatWebP, FileFormatGIF)

UserAvatarDecoration = NewCDN("/avatar-decorations/{user.id}/{user.avatar.decoration.hash}", FileFormatPNG)
AvatarDecoration = NewCDN("/avatar-decoration-presets/{user.avatar.decoration.hash}", FileFormatPNG)

ApplicationIcon = NewCDN("/app-icons/{application.id}/{icon.hash}", FileFormatPNG, FileFormatJPEG, FileFormatWebP)
ApplicationCover = NewCDN("/app-assets/{application.id}/{cover.image.hash}", FileFormatPNG, FileFormatJPEG, FileFormatWebP)
Expand Down
1 change: 1 addition & 0 deletions discord/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ConnectionType string

const (
ConnectionTypeBattleNet ConnectionType = "battlenet"
ConnectionTypeBungie ConnectionType = "bungie"
ConnectionTypeEbay ConnectionType = "ebay"
ConnectionTypeEpicGames ConnectionType = "epicgames"
ConnectionTypeFacebook ConnectionType = "facebook"
Expand Down
4 changes: 2 additions & 2 deletions discord/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type rawInteraction struct {
AppPermissions *Permissions `json:"app_permissions,omitempty"`
Entitlements []Entitlement `json:"entitlements"`
AuthorizingIntegrationOwners map[ApplicationIntegrationType]snowflake.ID `json:"authorizing_integration_owners"`
Context *InteractionContextType `json:"context"`
Context InteractionContextType `json:"context"`
}

// Interaction is used for easier unmarshalling of different Interaction(s)
Expand All @@ -66,7 +66,7 @@ type Interaction interface {
AppPermissions() *Permissions
Entitlements() []Entitlement
AuthorizingIntegrationOwners() map[ApplicationIntegrationType]snowflake.ID
Context() *InteractionContextType
Context() InteractionContextType
CreatedAt() time.Time

interaction()
Expand Down
4 changes: 2 additions & 2 deletions discord/interaction_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type baseInteraction struct {
appPermissions *Permissions
entitlements []Entitlement
authorizingIntegrationOwners map[ApplicationIntegrationType]snowflake.ID
context *InteractionContextType
context InteractionContextType
}

func (i baseInteraction) ID() snowflake.ID {
Expand Down Expand Up @@ -75,7 +75,7 @@ func (i baseInteraction) AuthorizingIntegrationOwners() map[ApplicationIntegrati
return i.authorizingIntegrationOwners
}

func (i baseInteraction) Context() *InteractionContextType {
func (i baseInteraction) Context() InteractionContextType {
return i.context
}

Expand Down
4 changes: 2 additions & 2 deletions discord/interaction_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func (PingInteraction) AuthorizingIntegrationOwners() map[ApplicationIntegration
return nil
}

func (PingInteraction) Context() *InteractionContextType {
return nil
func (PingInteraction) Context() InteractionContextType {
return 0
}

func (PingInteraction) interaction() {}
32 changes: 21 additions & 11 deletions discord/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ var _ Mentionable = (*Member)(nil)

// Member is a discord GuildMember
type Member struct {
User User `json:"user"`
Nick *string `json:"nick"`
Avatar *string `json:"avatar"`
RoleIDs []snowflake.ID `json:"roles,omitempty"`
JoinedAt time.Time `json:"joined_at"`
PremiumSince *time.Time `json:"premium_since,omitempty"`
Deaf bool `json:"deaf,omitempty"`
Mute bool `json:"mute,omitempty"`
Flags MemberFlags `json:"flags"`
Pending bool `json:"pending"`
CommunicationDisabledUntil *time.Time `json:"communication_disabled_until"`
User User `json:"user"`
Nick *string `json:"nick"`
Avatar *string `json:"avatar"`
RoleIDs []snowflake.ID `json:"roles,omitempty"`
JoinedAt time.Time `json:"joined_at"`
PremiumSince *time.Time `json:"premium_since,omitempty"`
Deaf bool `json:"deaf,omitempty"`
Mute bool `json:"mute,omitempty"`
Flags MemberFlags `json:"flags"`
Pending bool `json:"pending"`
CommunicationDisabledUntil *time.Time `json:"communication_disabled_until"`
AvatarDecorationData *AvatarDecorationData `json:"avatar_decoration_data"`

// This field is not present everywhere in the API and often populated by disgo
GuildID snowflake.ID `json:"guild_id"`
Expand Down Expand Up @@ -67,6 +68,15 @@ func (m Member) AvatarURL(opts ...CDNOpt) *string {
return &url
}

// AvatarDecorationURL returns the avatar decoration URL if set or nil
func (m Member) AvatarDecorationURL(opts ...CDNOpt) *string {
if m.AvatarDecorationData == nil {
return nil
}
url := formatAssetURL(AvatarDecoration, opts, m.AvatarDecorationData.Asset)
return &url
}

func (m Member) CreatedAt() time.Time {
return m.User.CreatedAt()
}
Expand Down
31 changes: 18 additions & 13 deletions discord/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ var _ Mentionable = (*User)(nil)

// User is a struct for interacting with discord's users
type User struct {
ID snowflake.ID `json:"id"`
Username string `json:"username"`
Discriminator string `json:"discriminator"`
GlobalName *string `json:"global_name"`
Avatar *string `json:"avatar"`
Banner *string `json:"banner"`
AccentColor *int `json:"accent_color"`
Bot bool `json:"bot"`
System bool `json:"system"`
PublicFlags UserFlags `json:"public_flags"`
AvatarDecoration *string `json:"avatar_decoration"`
ID snowflake.ID `json:"id"`
Username string `json:"username"`
Discriminator string `json:"discriminator"`
GlobalName *string `json:"global_name"`
Avatar *string `json:"avatar"`
Banner *string `json:"banner"`
AccentColor *int `json:"accent_color"`
Bot bool `json:"bot"`
System bool `json:"system"`
PublicFlags UserFlags `json:"public_flags"`
AvatarDecorationData *AvatarDecorationData `json:"avatar_decoration_data"`
}

// String returns a mention of the user
Expand Down Expand Up @@ -145,10 +145,10 @@ func (u User) BannerURL(opts ...CDNOpt) *string {

// AvatarDecorationURL returns the avatar decoration URL if set or nil
func (u User) AvatarDecorationURL(opts ...CDNOpt) *string {
if u.AvatarDecoration == nil {
if u.AvatarDecorationData == nil {
return nil
}
url := formatAssetURL(UserAvatarDecoration, opts, u.ID, *u.AvatarDecoration)
url := formatAssetURL(AvatarDecoration, opts, u.AvatarDecorationData.Asset)
return &url
}

Expand Down Expand Up @@ -198,3 +198,8 @@ type ApplicationRoleConnectionUpdate struct {
PlatformUsername *string `json:"platform_username,omitempty"`
Metadata *map[string]string `json:"metadata,omitempty"`
}

type AvatarDecorationData struct {
Asset string `json:"asset"`
SkuID snowflake.ID `json:"sku_id"`
}
20 changes: 10 additions & 10 deletions gateway/gateway_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ type gatewayImpl struct {
closeHandlerFunc CloseHandlerFunc
token string

conn *websocket.Conn
connMu sync.Mutex
heartbeatChan chan struct{}
status Status
conn *websocket.Conn
connMu sync.Mutex
heartbeatCancel context.CancelFunc
status Status

heartbeatInterval time.Duration
lastHeartbeatSent time.Time
Expand Down Expand Up @@ -132,9 +132,9 @@ func (g *gatewayImpl) Close(ctx context.Context) {
}

func (g *gatewayImpl) CloseWithCode(ctx context.Context, code int, message string) {
if g.heartbeatChan != nil {
if g.heartbeatCancel != nil {
g.config.Logger.Debug("closing heartbeat goroutines...")
g.heartbeatChan <- struct{}{}
g.heartbeatCancel()
}

g.connMu.Lock()
Expand Down Expand Up @@ -234,16 +234,16 @@ func (g *gatewayImpl) reconnect() {
}

func (g *gatewayImpl) heartbeat() {
if g.heartbeatChan == nil {
g.heartbeatChan = make(chan struct{})
}
ctx, cancel := context.WithCancel(context.Background())
g.heartbeatCancel = cancel

heartbeatTicker := time.NewTicker(g.heartbeatInterval)
defer heartbeatTicker.Stop()
defer g.config.Logger.Debug("exiting heartbeat goroutine")

for {
select {
case <-g.heartbeatChan:
case <-ctx.Done():
return

case <-heartbeatTicker.C:
Expand Down
8 changes: 7 additions & 1 deletion handlers/message_reaction_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
)
Expand Down Expand Up @@ -37,6 +38,11 @@ func gatewayHandlerMessageReactionAdd(client bot.Client, sequenceNumber int, sha
MessageAuthorID: event.MessageAuthorID,
})
} else {
var member discord.Member
// sometimes the member is nil for some reason
if event.Member != nil {
member = *event.Member
}
client.EventManager().DispatchEvent(&events.GuildMessageReactionAdd{
GenericGuildMessageReaction: &events.GenericGuildMessageReaction{
GenericEvent: genericEvent,
Expand All @@ -48,7 +54,7 @@ func gatewayHandlerMessageReactionAdd(client bot.Client, sequenceNumber int, sha
BurstColors: event.BurstColors,
Burst: event.Burst,
},
Member: *event.Member,
Member: member,
MessageAuthorID: event.MessageAuthorID,
})
}
Expand Down
6 changes: 6 additions & 0 deletions rest/guilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Guilds interface {
GetBan(guildID snowflake.ID, userID snowflake.ID, opts ...RequestOpt) (*discord.Ban, error)
AddBan(guildID snowflake.ID, userID snowflake.ID, deleteMessageDuration time.Duration, opts ...RequestOpt) error
DeleteBan(guildID snowflake.ID, userID snowflake.ID, opts ...RequestOpt) error
BulkBan(guildID snowflake.ID, ban discord.BulkBan, opts ...RequestOpt) (*discord.BulkBanResult, error)

GetIntegrations(guildID snowflake.ID, opts ...RequestOpt) ([]discord.Integration, error)
DeleteIntegration(guildID snowflake.ID, integrationID snowflake.ID, opts ...RequestOpt) error
Expand Down Expand Up @@ -210,6 +211,11 @@ func (s *guildImpl) DeleteBan(guildID snowflake.ID, userID snowflake.ID, opts ..
return s.client.Do(DeleteBan.Compile(nil, guildID, userID), nil, nil, opts...)
}

func (s *guildImpl) BulkBan(guildID snowflake.ID, ban discord.BulkBan, opts ...RequestOpt) (result *discord.BulkBanResult, err error) {
err = s.client.Do(BulkBan.Compile(nil, guildID), ban, &result, opts...)
return
}

func (s *guildImpl) GetIntegrations(guildID snowflake.ID, opts ...RequestOpt) (integrations []discord.Integration, err error) {
err = s.client.Do(GetIntegrations.Compile(nil, guildID), nil, &integrations, opts...)
return
Expand Down
1 change: 1 addition & 0 deletions rest/rest_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var (
GetBan = NewEndpoint(http.MethodGet, "/guilds/{guild.id}/bans/{user.id}")
AddBan = NewEndpoint(http.MethodPut, "/guilds/{guild.id}/bans/{user.id}")
DeleteBan = NewEndpoint(http.MethodDelete, "/guilds/{guild.id}/bans/{user.id}")
BulkBan = NewEndpoint(http.MethodPost, "/guilds/{guild.id}/bulk-ban")

GetMember = NewEndpoint(http.MethodGet, "/guilds/{guild.id}/members/{user.id}")
GetMembers = NewEndpoint(http.MethodGet, "/guilds/{guild.id}/members")
Expand Down

0 comments on commit a65b3b4

Please sign in to comment.