Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IconURL for GuildPreview and GuildWithCounts #885

Merged
merged 27 commits into from Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions interactions.go
Expand Up @@ -89,6 +89,19 @@ type Interaction struct {
Version int `json:"version"`
}

// InGuild is a utility function for checking if the interaction is sent in a guild or in a direct message
func (i Interaction) InGuild() bool {
return i.Member != nil
}

// GetUser is a utility function for retrieving the User struct
func (i Interaction) GetUser() *User {
if i.InGuild() {
return i.Member.User
}
return i.User
}

Jleagle marked this conversation as resolved.
Show resolved Hide resolved
// ApplicationCommandInteractionData contains data received in an interaction event.
type ApplicationCommandInteractionData struct {
ID string `json:"id"`
Expand Down
19 changes: 15 additions & 4 deletions restapi.go
Expand Up @@ -577,9 +577,20 @@ func memberPermissions(guild *Guild, channel *Channel, userID string, roles []st
// ------------------------------------------------------------------------------------------------

// Guild returns a Guild structure of a specific Guild.
// guildID : The ID of a Guild
func (s *Session) Guild(guildID string) (st *Guild, err error) {
body, err := s.RequestWithBucketID("GET", EndpointGuild(guildID), nil, EndpointGuild(guildID))
// guildID : The ID of a Guild
// withCounts : When true, will return approximate member and presence counts for the guild
// uses variadic for backwards compatibility
func (s *Session) Guild(guildID string, withCounts ...bool) (st *Guild, err error) {
Jleagle marked this conversation as resolved.
Show resolved Hide resolved

uri := EndpointGuild(guildID)

if len(withCounts) > 0 && withCounts[0] {
queryParams := url.Values{}
queryParams.Set("with_counts", "true")
uri += "?" + queryParams.Encode()
Jleagle marked this conversation as resolved.
Show resolved Hide resolved
}

body, err := s.RequestWithBucketID("GET", uri, nil, EndpointGuild(guildID))
if err != nil {
return
}
Expand Down Expand Up @@ -631,7 +642,7 @@ func (s *Session) GuildEdit(guildID string, g GuildParams) (st *Guild, err error
}
}

//Bounds checking for regions
// Bounds checking for regions
if g.Region != "" {
isValid := false
regions, _ := s.VoiceRegions()
Expand Down
43 changes: 28 additions & 15 deletions structs.go
Expand Up @@ -572,6 +572,19 @@ type Guild struct {
Permissions int64 `json:"permissions,string"`
}

// IconURL returns a URL to the guild's icon.
func (g Guild) IconURL() string {
Jleagle marked this conversation as resolved.
Show resolved Hide resolved
if g.Icon == "" {
return ""
}

if strings.HasPrefix(g.Icon, "a_") {
return EndpointGuildIconAnimated(g.ID, g.Icon)
}

return EndpointGuildIcon(g.ID, g.Icon)
}

// A GuildPreview holds data related to a specific public Discord Guild, even if the user is not in the guild.
type GuildPreview struct {
// The ID of the guild.
Expand All @@ -596,16 +609,29 @@ type GuildPreview struct {
// The list of enabled guild features
Features []string `json:"features"`

// Approximate number of members in this guild, returned from the GET /guild/<id> endpoint when with_counts is true
// Approximate number of members in this guild
Jleagle marked this conversation as resolved.
Show resolved Hide resolved
ApproximateMemberCount int `json:"approximate_member_count"`

// Approximate number of non-offline members in this guild, returned from the GET /guild/<id> endpoint when with_counts is true
// Approximate number of non-offline members in this guild
Jleagle marked this conversation as resolved.
Show resolved Hide resolved
ApproximatePresenceCount int `json:"approximate_presence_count"`

// the description for the guild
Description string `json:"description"`
}

// IconURL returns a URL to the guild's icon.
func (g GuildPreview) IconURL() string {
Jleagle marked this conversation as resolved.
Show resolved Hide resolved
if g.Icon == "" {
return ""
}

if strings.HasPrefix(g.Icon, "a_") {
return EndpointGuildIconAnimated(g.ID, g.Icon)
}

return EndpointGuildIcon(g.ID, g.Icon)
}

// MessageNotifications is the notification level for a guild
// https://discord.com/developers/docs/resources/guild#guild-object-default-message-notification-level
type MessageNotifications int
Expand All @@ -626,19 +652,6 @@ const (
SystemChannelFlagsSuppressPremium
)

// IconURL returns a URL to the guild's icon.
func (g *Guild) IconURL() string {
if g.Icon == "" {
return ""
}

if strings.HasPrefix(g.Icon, "a_") {
return EndpointGuildIconAnimated(g.ID, g.Icon)
}

return EndpointGuildIcon(g.ID, g.Icon)
}

// A UserGuild holds a brief version of a Guild
type UserGuild struct {
ID string `json:"id"`
Expand Down